【问题标题】:Can add one Service Reference for multiple WCF Service Contracts可以为多个 WCF 服务合同添加一个服务参考
【发布时间】:2010-12-12 06:55:56
【问题描述】:

我在一个托管在 Windows 服务下的 WCF 库中定义了多个服务合同。 这些服务在 Windows 服务配置文件中公开如下:

<services>
  <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
    name="ReportingComponentLibrary.TemplateService">
    <endpoint address="" binding="wsHttpBinding" contract="ReportingComponentLibrary.ITemplateService" bindingConfiguration="wsHttp" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8080/ReportingComponentLibrary/TemplateService/" />
      </baseAddresses>
    </host>
  </service>
  <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
    name="ReportingComponentLibrary.TemplateReportService">
    <endpoint address="" binding="wsHttpBinding" contract="ReportingComponentLibrary.ITemplateReportService" bindingConfiguration="wsHttp" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8080/ReportingComponentLibrary/TemplateReportService/" />
      </baseAddresses>
    </host>
  </service>
</services>

现在,当我在客户端应用程序中添加服务引用时,

是否可以为以上两项服务仅添加一项服务参考或

我需要为每个服务/服务合同单独引用。

更新:

这是我的申请详情:

我有三个不同的项目:

  1. WCF 服务库
  2. 用于在 WCF 服务之上托管的 Windows 服务
  3. 客户端 - 用于测试的控制台应用程序

现在,WCF 服务库中的 App.Config 如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>     

  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttp" maxReceivedMessageSize="50000000" maxBufferPoolSize="50000000" messageEncoding="Mtom">
          <readerQuotas maxDepth="500" maxStringContentLength="500000000" maxArrayLength="500000000"
          maxBytesPerRead="500000000" maxNameTableCharCount="500000000" />
        </binding>
      </wsHttpBinding>
    </bindings>

    <services>
      <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
        name="ReportingComponentLibrary.TemplateService">
        <endpoint address="" binding="wsHttpBinding" contract="ReportingComponentLibrary.ITemplateService" bindingConfiguration="wsHttp" >
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/ReportingComponentLibrary/TemplateService/" />
          </baseAddresses>
        </host>
      </service>

      <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
        name="ReportingComponentLibrary.TemplateReportService">
        <endpoint address="" binding="wsHttpBinding" contract="ReportingComponentLibrary.ITemplateReportService" bindingConfiguration="wsHttp" >
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/ReportingComponentLibrary/TemplateReportService/" />
          </baseAddresses>
        </host>
      </service>

    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ReportingComponentLibrary.TemplateServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Windows Service中的App.Config同上。

现在在我的客户端应用程序中,我需要使用来自 TemplateService 和 TemplateReportService 的方法。

所以,我总是可以将两个不同的服务引用作为:

http://localhost:8080/ReportingComponentLibrary/TemplateService/ 和

http://localhost:8080/ReportingComponentLibrary/TemplateReportService/

一切正常。

但我想知道是否有任何方法(除了您建议的包装解决方法) 我只需要添加一个参考和 我可以从这两个服务中调用方法。

【问题讨论】:

    标签: wcf wcf-client


    【解决方案1】:

    就目前而言,我认为这是不可能的,您将需要 2 个服务引用,因为它们都实现了单独的合同。假设您可以控制服务代码,您可以实现一种解决方法,您可以创建一个服务包装器,通过指向两个单独的服务来实现这两个合同。这将允许您拥有一个服务参考。关于为什么您希望它们都在一项服务中而不是两项服务中是否存在特定问题?

    编辑: 这个Blog Article - Meineck.Net 向您展示了如何配置您的服务以实现您所追求的目标,但同样,它几乎是一种变通方法。祝你好运。

    【讨论】:

    • 感谢您的回复。好吧,没有什么特别的原因——因为这两个服务都将在一个应用程序中使用,所以希望客户端应用程序只添加一个引用而不是两个/多个。
    • +1 正好 - 一个服务引用 = 引用 一个 服务 - 不能一次引用多个服务
    • 即使两者都属于一个 WCF 库?
    • 在网上快速搜索了一下,我发现了一些可能对您有用的链接:aspfree.com/c/a/ASP.NET/…msdn.microsoft.com/en-us/library/bb924405.aspxmsdn.microsoft.com/en-us/library/bb332338.aspx 如果可能,您能否提供有关您的方案和配置的更多详细信息.
    【解决方案2】:

    一份服务参考 = 一份服务合同

    您的 Windows 服务内部有许多服务,每个服务都有自己的合同。

    然而,没有什么能阻止您创建一个包含所有服务功能的单一契约,然后创建一个实现该契约的类,但它只是传递给其他服务的层。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 2015-04-09
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 2023-03-31
      • 2013-12-21
      相关资源
      最近更新 更多