简体   繁体   English

如何在WCF中配置多个服务端点?

[英]How Do I configure Multiple Service Endpoints in WCF?

I'd like some basic guidance on setting up my WCF configuration. 我想要一些有关设置WCF配置的基本指导。 This is my first serious effort with WCF (and first post on stackoverflow). 这是我对WCF的首次认真努力(也是关于stackoverflow的第一篇文章)。

I have a WCF class library (APILibrary) that I am referncing in my web project. 我在Web项目中引用了WCF类库(APILibrary)。 In the wcf library, I currently have two services - IAuthService and ITradeService. 在wcf库中,我目前有两个服务-IAuthService和ITradeService。

Along these lines, I have three questions: 按照这些思路,我有三个问题:

1) My problem (and the original reason for this post) is that when I compile my application I am able to call TradeServiceCient but not AuthServiceClient in my web app. 1) 我的问题(也是这篇文章的原始原因)是 ,当我编译我的应用程序时,我能够在我的Web应用程序中调用TradeServiceCient而不是AuthServiceClient。 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. 我觉得这与它们共享同一端口(并且仅包含一个端点)有关,但是我不清楚。

2) I am attempting to expose two service endpoints at the same time (and, likely a few more) while I develop and test. 2)在开发和测试时,我试图同时公开两个服务端点(可能还会公开两个服务端点)。 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)? 在那之前,我该如何做(我感觉这与上面的问题有关)?

3) I am noticing in a lot of posts people have examples of the "client" and "server" "system.serviceModel" code. 3)我注意到很多帖子中都有“客户端”和“服务器”“ system.serviceModel”代码的示例。 Are these unique files or tags in the App.config file that resides in my WCF library? 这些唯一文件或标记是否位于WCF库中的App.config文件中? What is each doing? 每个在做什么? Currently, I think I just have the server side? 目前,我想我只是在服务器端?

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#

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. 我将在您的配置中唯一更改的是“ baseAddress”和实际服务地址之间的分配。 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: 我会这样分割它:

 <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 base for 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

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. 然后,我尝试将所有类文件的名称空间从SpoonSys.ApiLibrary.Authentication和SpoonSys.ApiLibrary.Trade更改为SpoonSys.ApiLibary。 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. 尽管如此,智能感知仅在提供一项服务,而没有提供另一项服务。

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). 对于我的客户端应用程序项目中的WCF,我没有做任何特别的事情(除了将WCF类库作为Web服务参考导入)。 Maybe this is where I am going wrong? 也许这就是我要去的地方? Could you tell me more here? 你能在这里告诉我更多吗?

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 /认证/ 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 ENDPOINT: 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再次:

<?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. PS-当我尝试提交响应时,我得到“新用户最多只能发布一个超链接”,因此,我删除了帖子中的所有“ http://”引用。 Its not a code error. 它不是代码错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM