C# 如何在 WCF 中配置多个服务终结点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1167916/
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
How Do I configure Multiple Service Endpoints in WCF?
提问by
I'd like some basic guidance on setting up my WCF configuration. This is my first serious effort with WCF (and first post on stackoverflow).
我想要一些关于设置 WCF 配置的基本指导。这是我对 WCF 的第一次认真努力(也是关于 stackoverflow 的第一篇文章)。
I have a WCF class library (APILibrary) that I am referncing in my web project. In the wcf library, I currently have two services - IAuthService and ITradeService.
我有一个 WCF 类库 (APILibrary),我在我的 Web 项目中引用了它。在 wcf 库中,我目前有两个服务 - IAuthService 和 ITradeService。
Along these lines, I have three questions:
沿着这些思路,我有三个问题:
1) My problem (and the original reason for this post) isthat when I compile my application I am able to call TradeServiceCient but not AuthServiceClient in my web app. The latter does not appear in the intellisense. I have a feeling that it has to do with the fact that they are sharing the same port (and only one endpoint got included) but I am obviously unclear.
1)我的问题(以及这篇文章的最初原因)是,当我编译我的应用程序时,我可以在我的 Web 应用程序中调用 TradeServiceCient 但不能调用 AuthServiceClient。后者不会出现在智能感知中。我有一种感觉,这与他们共享同一个端口(并且只包含一个端点)这一事实有关,但我显然不清楚。
2) I am attempting to expose two service endpoints at the same time (and, likely a few more) while I develop and test. When I move over to staging and hosting, each endpoint will have its own address. Until then, how do I do this (I have a feeling this relates to my question above)?
2) 我试图在开发和测试时同时公开两个服务端点(并且可能更多)。当我转向登台和托管时,每个端点都有自己的地址。在那之前,我该怎么做(我觉得这与我上面的问题有关)?
3) I am noticing in a lot of posts people have examples of the "client" and "server" "system.serviceModel" code. Are these unique files or tags in the App.config file that resides in my WCF library? What is each doing? Currently, I think I just have the server side?
3)我在很多帖子中注意到人们有“客户端”和“服务器”“system.serviceModel”代码的例子。这些独特的文件或标记是否位于驻留在我的 WCF 库中的 App.config 文件中?每个人在做什么?目前,我想我只有服务器端?
Here is what I currently have in my App.config file (in my WCF Library):
这是我目前在 App.config 文件(在我的 WCF 库中)中的内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<client />
<services>
<service behaviorConfiguration="ApiLibrary.ApiBehavior" name="SpoonSys.Api.Trade.TradeService">
<endpoint address="" binding="wsHttpBinding" contract="SpoonSys.Api.Trade.ITradeService">
<identity>
<dns value="localhost:8731" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/ApiLibrary/Trade/" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="ApiLibrary.ApiBehavior" name="SpoonSys.Api.Authentication.AuthService">
<endpoint address="" binding="wsHttpBinding" contract="SpoonSys.Api.Authentication.IAuthService">
<identity>
<dns value="localhost:8731" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/ApiLibrary/Authentication/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ApiLibrary.ApiBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
My configuration is ASP.NET / Framework 3.5 / VS 2008 / C#
我的配置是 ASP.NET / Framework 3.5 / VS 2008 / C#
回答by marc_s
Yes, in your case, you're only dealing with the server side - so your config looks quite okay, actually.
是的,就您而言,您只是在处理服务器端 - 实际上,您的配置看起来还不错。
The only thing I would change in your config is the split between "baseAddress" and the actual service address. You currently define the entire complete address in your base address - that kinda defeats the purpose of the base address. I would split it like so:
我唯一要在您的配置中更改的是“baseAddress”和实际服务地址之间的拆分。您当前在基地址中定义了整个完整地址 - 这有点违背了基地址的目的。我会像这样拆分它:
<service name="SpoonSys.Api.Services"
behaviorConfiguration="ApiLibrary.ApiBehavior" >
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/ApiLibrary/" />
</baseAddresses>
</host>
<endpoint
address="Trade"
binding="wsHttpBinding"
contract="SpoonSys.Api.Trade.ITradeService">
<identity>
<dns value="localhost:8731" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
That way, you could basically collapse your two endpoints into one - the base address defines just that - a common basefor all other addresses - while the endpoint defines the details of the full address:
这样,您基本上可以将两个端点合并为一个 - 基地址仅定义了该地址 - 所有其他地址的公共基址- 而端点定义了完整地址的详细信息:
<service name="SpoonSys.Api.Services"
behaviorConfiguration="ApiLibrary.ApiBehavior" >
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/ApiLibrary/" />
</baseAddresses>
</host>
<endpoint
address="Trade"
binding="wsHttpBinding"
contract="SpoonSys.Api.Trade.ITradeService">
<identity>
<dns value="localhost:8731" />
</identity>
</endpoint>
<endpoint
address="Authentication"
binding="wsHttpBinding"
contract="SpoonSys.Api.Authentication.IAuthService">
<identity>
<dns value="localhost:8731" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
This works if you have a single Service class "SpoonSys.Api.Services" that implements both of these service contracts - that's totally fine and quite useful at times.
如果您有一个实现这两个服务合同的服务类“SpoonSys.Api.Services”,这将起作用 - 这非常好,有时非常有用。
IF you have to have two separate service classes, implementing one interface each, then you won't be able to collapse your config like this - then you'll have to use the full config for two separate service classes in your original post (which seemed fine to me).
如果您必须有两个单独的服务类,每个类实现一个接口,那么您将无法像这样折叠您的配置 - 那么您必须在原始帖子中为两个单独的服务类使用完整配置(其中对我来说似乎很好)。
The other question is why you only get to see one endpoint in your intellisense - did you create your client proxy for both endpoints? Since it's two separate contracts, you'll need two separate client proxies. How did you create your client endpoint? Can you post the client config, too?
另一个问题是为什么您只能在智能感知中看到一个端点 - 您是否为两个端点创建了客户端代理?由于它是两个单独的合同,因此您需要两个单独的客户端代理。您是如何创建客户端端点的?您也可以发布客户端配置吗?
Marc
马克
回答by marc_s
I tried shortening the base address per your suggestion. I was then getting an error where one service would start, but not the other. I was being told that the metadata endpoint was already being used. I then tried changing my namespaces from SpoonSys.ApiLibrary.Authentication and SpoonSys.ApiLibrary.Trade to just SpoonSys.ApiLibary for all class files. Still - same error. When I changed back to my original server configuration it compiled. Still, the intellisense is only picking up one service, and not the other.
我尝试根据您的建议缩短基址。然后我收到一个错误,其中一项服务会启动,但另一项不会。有人告诉我元数据端点已经被使用。然后,我尝试将所有类文件的命名空间从 SpoonSys.ApiLibrary.Authentication 和 SpoonSys.ApiLibrary.Trade 更改为 SpoonSys.ApiLibary。仍然 - 同样的错误。当我改回我的原始服务器配置时,它会编译。尽管如此,智能感知只接受一项服务,而不是另一项服务。
I am not sure what you mean about my client config file. I have not done anything particular with regard to WCF in my client app project (except inport the WCF class library as a web service reference). Maybe this is where I am going wrong? Could you tell me more here?
我不确定您对我的客户端配置文件的意思。在我的客户端应用程序项目中,我没有对 WCF 做任何特别的事情(除了将 WCF 类库作为 Web 服务引用导入)。也许这是我出错的地方?你能在这里告诉我更多吗?
I have noticed a Client.dll in the WCF editor for each endpoint and have posted those below:
我注意到每个端点的 WCF 编辑器中都有一个 Client.dll,并在下面发布了这些:
ENDPOINT:localhost:8731/Design_Time_Addresses/Authentication/mex
终点:本地主机:8731/Design_Time_Addresses/Authentication/mex
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ITradeService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="localhost:8731/Design_Time_Addresses/Trade/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITradeService"
contract="ITradeService" name="WSHttpBinding_ITradeService">
<identity>
<dns value="localhost:8731" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
ENDPOINT:http://localhost:8731/Design_Time_Addresses/Trade/mex
终点:http://localhost:8731/Design_Time_Addresses/Trade/mex
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IAuthService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="localhost:8731/Design_Time_Addresses/Authentication/"
binding="wsHttp binding" bindingConfiguration="WSHttpBinding_IAuthService"
contract="IAuthService" name="WSHttpBinding_IAuthService">
<identity>
<dns value="localhost:8731" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
AND Finally, here is my APP.CONFIG AGAIN:
最后,这是我的 APP.CONFIG AGAIN:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service behaviorConfiguration="ApiLibrary.ApiBehavior"
name="SpoonSys.ApiLibrary.Trade.TradeService">
<endpoint address="" binding="wsHttpBinding" contract="SpoonSys.Api.Trade.ITradeService">
<identity>
<dns value="localhost:8731" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/Trade/" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="ApiLibrary.ApiBehavior"
name="SpoonSys.Api.Authentication.AuthService">
<endpoint address="" binding="wsHttpBinding" contract="SpoonSys.ApiLibrary.Authentication.IAuthService">
<identity>
<dns value="localhost:8731" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="localhost:8731/Design_Time_Addresses/Authentication/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ApiLibrary.ApiBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
PS - I am getting "new users can only post a maximum of one hyperlink" when i try to submit my response so, i took out all "http://" references in my post. Its not a code error.
PS - 当我尝试提交回复时,我收到“新用户最多只能发布一个超链接”的提示,因此,我删除了帖子中的所有“http://”引用。它不是代码错误。