WCF - 具有多个接口的不同 IIS .svc 端点上的相同服务

发布于 2024-12-14 07:10:12 字数 1822 浏览 1 评论 0原文

我正在尝试实现以下目标:

  • 拥有一组“完整”服务供内部应用程序使用
  • 将这些方法的子集公开给第三方

我尝试实现此目的的方法是创建一个实现两个接口的服务

例如:

公共服务接口

[ServiceContract(Namespace = "http://www.myurl.com/public/2011/10")]
public partial interface IPublicService
{
    [OperationContract]
    ResponseObjOne OperationAvailableToEveryone(RequestObjOne request);
}

私有服务接口

[ServiceContract(Namespace = "http://www.myurl.com/private/2011/10")]
public partial interface IPrivateService
{
    [OperationContract]
    ResponseObjOne OperationAvailableToEveryone(RequestObjOne request);

    [OperationContract]
    ResponseObjTwo OperationAvailableInternally(RequestObjTwo request);
}

实现两个接口的服务类

public class Service : IPrivateService, IPublicService
{
    ResponseObjOne OperationAvailableToEveryone(RequestObjOne request)
    { }

    ResponseObjTwo OperationAvailableInternally(RequestObjTwo request)
    { }
}

我现在希望能够将其配置为作为两个单独的端点运行在 IIS 中。因此,我有一个包含以下内容的 .svc 文件:

<%@ ServiceHost Language="C#" Debug="true"  Service="Adactus.Pulse.SOAServices.Service, Adactus.Pulse.SOAServices"  %> 

并在 web.config 中添加了以下内容:

  <service name="Service">
    <endpoint address="/public" binding="basicHttpBinding" contract="IPublicService" />
    <endpoint address="/private" binding="basicHttpBinding" contract="IPrivateService" />
  </service>

但是,如果我浏览到 .svc 文件,我现在会看到 WSDL 中的所有操作,如果我将 /public 添加到 URL,我会看到404. 那么我怎样才能做到这一点呢?

理想情况下,我想添加另一个 .svc 端点,并能够在这些 svc 文件中指定接口以及服务实现类。然后我可以在 IIS 中锁定对 svc 的访问以保护内部服务。

关键是一些操作在两个合约中都公开了,我不想重复它们的实现。

有什么想法吗?我是否以错误的方式处理这件事?

干杯, 抢

I am trying to achieve the following:

  • Have one 'full' set of services for consumption by internal apps
  • Expose a subset of these methods to 3rd parties

The way I have tried to go about this is to create one service that implements two interfaces

For example:

Public Service Interface

[ServiceContract(Namespace = "http://www.myurl.com/public/2011/10")]
public partial interface IPublicService
{
    [OperationContract]
    ResponseObjOne OperationAvailableToEveryone(RequestObjOne request);
}

Private Service Interface

[ServiceContract(Namespace = "http://www.myurl.com/private/2011/10")]
public partial interface IPrivateService
{
    [OperationContract]
    ResponseObjOne OperationAvailableToEveryone(RequestObjOne request);

    [OperationContract]
    ResponseObjTwo OperationAvailableInternally(RequestObjTwo request);
}

Service class to implement both interfaces

public class Service : IPrivateService, IPublicService
{
    ResponseObjOne OperationAvailableToEveryone(RequestObjOne request)
    { }

    ResponseObjTwo OperationAvailableInternally(RequestObjTwo request)
    { }
}

I would now like to be able to configure this to run as two separate endpoints in IIS. So I have an .svc file with the following:

<%@ ServiceHost Language="C#" Debug="true"  Service="Adactus.Pulse.SOAServices.Service, Adactus.Pulse.SOAServices"  %> 

And added the following in the web.config:

  <service name="Service">
    <endpoint address="/public" binding="basicHttpBinding" contract="IPublicService" />
    <endpoint address="/private" binding="basicHttpBinding" contract="IPrivateService" />
  </service>

But if I browse to the .svc file I now see all operations in the WSDL and if I add /public to the URL I see a 404. So how can I achieve this?

Ideally I would like to add another .svc endpoint and be able to specify the interface as well as the service implementation class in these svc files. Then I can lock down access to the svc in IIS to secure the internal service.

the key is that some of the operations are exposed in both contracts and I don't want to duplicate their implementation.

Any ideas? Am I going about this in the wrong way?

Cheers,
Rob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你丑哭了我 2024-12-21 07:10:12

虽然它没有回答你的问题,但我绝对不会这样设计。我将创建一个包含接口及其实现的单个类库,然后创建公开不同接口的单独的 WCF 项目。

While it doesn't answer your question, I would definitely not design it this way. I would create a single class library that includes both interfaces and the implementations for them and then I would create separate WCF projects that expose the different interfaces.

缪败 2024-12-21 07:10:12

观察:您实现这两个接口的 Service 类似乎是错误的。两个接口具有相同的方法名称 OperationAvailableToEveryone 事实上,您必须显式实现您的接口。

我什至有相同的查询。事实上,您无法使用 http://localhost:8001/service.svc/public 浏览,而是使用 http://localhost:8001/public/service.svc 进行浏览。您仍然可以使用 http://localhost:8001/service.svc 创建代理,并正常使用它,您的客户端 enpoint 地址看起来像

    </client>
        <endpoint address="http://localhost:8001/SOAService.svc/public"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPublicService"
            contract="SOAService.IPublicService" name="BasicHttpBinding_IPublicService" />
        <endpoint address="http://localhost:8001/SOAService.svc/private"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPrivateService"
            contract="SOAService.IPrivateService" name="BasicHttpBinding_IPrivateService" />
    </client>

希望这有帮助。

Observations: Your Service class to implement both interfaces seems wrong. both interfaces have same method name OperationAvailableToEveryone infact you have to implement your interfaces explicitly.

I even have same query. Infact you cannnot browse with http://localhost:8001/service.svc/public instead http://localhost:8001/public/service.svc. still you can create proxy with http://localhost:8001/service.svc and you use it as normal and your client enpoint address looks like

    </client>
        <endpoint address="http://localhost:8001/SOAService.svc/public"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPublicService"
            contract="SOAService.IPublicService" name="BasicHttpBinding_IPublicService" />
        <endpoint address="http://localhost:8001/SOAService.svc/private"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPrivateService"
            contract="SOAService.IPrivateService" name="BasicHttpBinding_IPrivateService" />
    </client>

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文