C# WCF 服务在 2 个不同的服务合同上公开 2 个端点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13742065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF service exposing 2 endpoints on 2 different service contracts
提问by Coral Doe
I have an WCF service which I am trying to configure so that it exposes 2 endpoints, refering to different functionalities, under different URLs.
我有一个 WCF 服务,我正在尝试对其进行配置,以便它在不同的 URL 下公开 2 个端点,引用不同的功能。
What I want to have is Service1, exposing methods A, B, C, and Service2, exposing methods D, E. I want to be able to browse both localhost/WebServiceName/Service1/Service.svcand localhost/WebServiceName/Service2/Service.svc.
我想要的是Service1,公开方法 A、B、C 和Service2,公开方法 D、E。我希望能够浏览localhost/WebServiceName/Service1/Service.svc和localhost/WebServiceName/Service2/Service .svc。
Other applications referencing localhost/WebServiceName/Service1/Service.svcshould see only the interface containing the methods A, B and C. They should not see anything regarding Service2interface. And for Service2likewise.
其他引用localhost/WebServiceName/Service1/Service.svc 的应用程序应该只看到包含方法 A、B 和 C 的接口。他们不应该看到任何关于Service2接口的内容。对于Service2也是如此。
So far I have defined two interfaces in my WCF service, I_Service1and I_Service2.
到目前为止,我已经在我的 WCF 服务中定义了两个接口,I_Service1和I_Service2。
I have added two endpoints in my web.config like so:
我在 web.config 中添加了两个端点,如下所示:
<endpoint address="http://localhost/WebServiceName/Service1/" binding="wsHttpBinding" contract="WebServiceName.I_Service1" bindingConfiguration="Binding1" />
<endpoint address="http://localhost/WebServiceName/Service2/" binding="wsHttpBinding" contract="WebServiceName.I_Service2" bindingConfiguration="Binding2" />
The suggestion of using full address in the enpoint comes from here: Multiple endpoints under IIS
enpoint中使用全地址的建议来自这里:IIS下的多个端点
But still, I can't browse localhost/WebServiceName/Service1/Service.svc. I receive:
但是,我仍然无法浏览localhost/WebServiceName/Service1/Service.svc。我收到:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
I can successfully browse localhost/WebServiceName/Service.svcand the wsdl includes methods A, B, C, D, E. But this should be wrong in the behaviour I want.
我可以成功浏览localhost/WebServiceName/Service.svc并且 wsdl 包括方法 A、B、C、D、E。但这在我想要的行为中应该是错误的。
Is there something that I have missed?
有什么我错过了吗?
UPDATE: Following this article http://allen-conway-dotnet.blogspot.ro/2011/09/exposing-multiple-binding-types-for.htmlI created two different contract services for those endpoints. But currently I am seing only Service1when I browse it. Service2apparently does not exist (HTTP 404 error related issues appear).
更新:在这篇文章http://allen-conway-dotnet.blogspot.ro/2011/09/exposing-multiple-binding-types-for.html 之后,我为这些端点创建了两个不同的合同服务。但是目前我在浏览它时只看到Service1。Service2显然不存在(出现 HTTP 404 错误相关问题)。
The configuration looks like:
配置如下:
<services>
<service behaviorConfiguration="WebServiceName.ServiceBehavior1" name="WebServiceName.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1"
contract="WebServiceName.I_Service1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/WebServiceName/Service1/Service.svc" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="WebServiceName.ServiceBehavior2" name="WebServiceName.Service2">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1"
contract="WebServiceName.I_Service2" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/WebServiceName/Service2/Service.svc" />
</baseAddresses>
</host>
</service>
</services>
采纳答案by Coral Doe
For the moment my solution to this problem was incorporating two .svc files in my webservice to separate the two interfaces. Such, I have localhost/WebServiceName/Service1.svcand localhost/WebServiceName/Service2.svc.
目前我对这个问题的解决方案是在我的 web 服务中合并两个 .svc 文件来分隔两个接口。这样,我有localhost/WebServiceName/Service1.svc和localhost/WebServiceName/Service2.svc。
With the endpoint configuration
使用端点配置
<services>
<service behaviorConfiguration="WebServiceName.ServiceBehavior1" name="WebServiceName.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1"
contract="WebServiceName.I_Service1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/WebServiceName/Service1.svc" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="WebServiceName.ServiceBehavior2" name="WebServiceName.Service2">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding2"
contract="WebServiceName.I_Service2" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/WebServiceName/Service1.svc" />
</baseAddresses>
</host>
</service>
</services>
This solution is not necessary the best one (if a client really wants, it can find that this service exposes 2 different interfaces, but I can secure them with different credentials/tokens). But at the moment I will go with it.
这个解决方案不是最好的解决方案(如果客户真的想要,它会发现这个服务公开了 2 个不同的接口,但我可以用不同的凭据/令牌保护它们)。但此刻我会接受它。
回答by Piotr Czarnecki
I tried to make the thing which you described. All of this succeed. Please do'nt be angry if some of steps are obvious for you. So:
我试图制作你描述的东西。这一切都成功了。如果某些步骤对您来说很明显,请不要生气。所以:
- Create WCF Service Application Project.
- Add two WCF Service items (by default will be create interface and svc file).
- Create Web Application project.
- Add two Service References (Right click on Web App Project ==> Add Service Reference) by using 'Discover' button (two web services should be visible).
- That's all.
- 创建 WCF 服务应用程序项目。
- 添加两个 WCF 服务项(默认情况下将创建接口和 svc 文件)。
- 创建 Web 应用程序项目。
- 使用“发现”按钮添加两个服务引用(右键单击 Web 应用程序项目 ==> 添加服务引用)(两个 Web 服务应该可见)。
- 就这样。
By default I got the following Web.config in Web Application project:
默认情况下,我在 Web 应用程序项目中获得了以下 Web.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
<binding name="BasicHttpBinding_IService2" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:3597/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
<endpoint address="http://localhost:3597/Service2.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService2" contract="ServiceReference2.IService2"
name="BasicHttpBinding_IService2" />
</client>
You can try to rebuild WCF project and Update WebReferences.
您可以尝试重建 WCF 项目并更新 WebReferences。
回答by Martin
The way I've always done this is as follows: set a single "base address" for the service and specify the endpoint addresses as different text to append onto that base address...
我一直这样做的方式如下:为服务设置一个“基地址”,并将端点地址指定为不同的文本,以附加到该基地址...
<service name="MyNamespace.MyService">
<endpoint address="FirstEndpointAddress" binding="netTcpBinding"
name="FirstEndpointName"
contract="MyNamespace.FirstEndpointContract" />
<endpoint address="SecondEndpointAddress" binding="netTcpBinding"
name="SecondEndpointName"
contract="MyNamespace.SecondEndpointContract" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8733/MyBaseAddress" />
</baseAddresses>
</host>
</service>
So in your case the base address might be localhost/WebServiceNameand the endpoint address for endpoint 1 might be Service1/Service.svc. Likewise for endpoint 2 it might be Service2/Service.svc. I can see that you've followed advice to put the full address in the endpoint address, but all I can say is I've done it this way with success.
因此,在您的情况下,基地址可能是localhost/WebServiceName,端点 1 的端点地址可能是Service1/Service.svc. 同样,对于端点 2,它可能是Service2/Service.svc。我可以看到您已经按照建议将完整地址放在端点地址中,但我只能说我已经成功地做到了这一点。

