2

我想要一些关于设置我的 WCF 配置的基本指导。这是我对 WCF 的第一次认真努力(也是关于 stackoverflow 的第一篇文章)。

我有一个在我的 Web 项目中引用的 WCF 类库 (APILibrary)。在 wcf 库中,我目前有两个服务 - IAuthService 和 ITradeService。

沿着这些思路,我有三个问题:

1)我的问题(以及这篇文章的最初原因)是当我编译我的应用程序时,我可以在我的网络应用程序中调用 TradeServiceCient 但不能调用 AuthServiceClient。后者不会出现在智能感知中。我有一种感觉,这与它们共享同一个端口(并且只包括一个端点)这一事实有关,但我显然不清楚。

2) 我在开发和测试时尝试同时公开两个服务端点(并且可能更多)。当我转到暂存和托管时,每个端点都有自己的地址。在那之前,我该怎么做(我觉得这与我上面的问题有关)?

3)我注意到在很多帖子中人们都有“客户端”和“服务器”“system.serviceModel”代码的示例。这些独特的文件或标签是否位于我的 WCF 库中的 App.config 文件中?每个人都在做什么?目前,我想我只有服务器端?

这是我目前在我的 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>

我的配置是 ASP.NET / Framework 3.5 / VS 2008 / C#

4

2 回答 2

4

是的,在你的情况下,你只处理服务器端 - 所以你的配置看起来还不错,实际上。

我在您的配置中唯一要更改的是“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>

这样,您基本上可以将两个端点合并为一个-基地址仅定义了-所有其他地址的公共基础-而端点定义了完整地址的详细信息:

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

如果您有一个服务类“SpoonSys.Api.Services”来实现这两个服务合同,则此方法有效 - 这完全没问题,有时非常有用。

如果您必须有两个独立的服务类,每个服务类实现一个接口,那么您将无法像这样折叠您的配置 - 那么您必须在原始帖子中为两个独立的服务类使用完整配置(其中对我来说似乎很好)。

另一个问题是为什么您只能在智能感知中看到一个端点——您是否为两个端点创建了客户端代理?由于它是两个独立的合同,因此您需要两个独立的客户端代理。您是如何创建客户端端点的?你也可以发布客户端配置吗?

马克

于 2009-07-22T20:38:19.677 回答
0

我尝试根据您的建议缩短基址。然后我收到一个错误,其中一个服务将启动,但另一个服务却没有。有人告诉我,元数据端点已被使用。然后,我尝试将所有类文件的命名空间从 SpoonSys.ApiLibrary.Authentication 和 SpoonSys.ApiLibrary.Trade 更改为 SpoonSys.ApiLibary。仍然 - 同样的错误。当我改回原来的服务器配置时,它编译了。尽管如此,智能感知只接受一项服务,而不是另一项服务。

我不确定您对我的客户端配置文件的意思。在我的客户端应用程序项目中,我没有对 WCF 做任何特别的事情(除了将 WCF 类库作为 Web 服务引用导入)。也许这就是我要去错的地方?你能在这里告诉我更多吗?

我注意到每个端点的 WCF 编辑器中有一个 Client.dll,并在下面发布了这些:

ENDPOINT:localhost: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>

端点: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>

最后,这是我的 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 - 当我尝试提交我的回复时,我收到“新用户最多只能发布一个超链接”,所以我在我的帖子中删除了所有“http://”引用。它不是代码错误。

于 2009-07-23T20:37:26.380 回答