可以为多个WCF服务合同添加一个服务引用

ini*_*iki 1 wcf wcf-client

我在一个WCF库中定义了多个服务契约,这些托管在Windows服务下托管.这些服务在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>
Run Code Online (Sandbox Code Playgroud)

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

是否可以为上述两个服务添加一个服务引用或

我需要为每个服务/服务合同分开参考.

更新:

以下是我的申请详情:

我有三个不同的项目:

  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>
Run Code Online (Sandbox Code Playgroud)

Windows Service中的App.Config与上面相同.

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

所以,我总是可以使用两种不同的服务引用:

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

HTTP://本地主机:8080/ReportingComponentLibrary/TemplateReportService /

这一切都很好.

但我想知道是否有任何方法(除了你建议的包装解决方法)我只需要添加一个引用,我可以从这两个服务调用方法.

Tan*_*ner 5

按照目前的情况,我不认为这是可能的,你需要2个服务参考,因为它们都实现了单独的合同.假设您可以控制服务代码,您可以实现一种解决方法,您可以通过指向两个单独的服务来创建服务包装器来实现两个合同.这将允许您有一个服务参考.是否有一个特定的问题,为什么你想要他们在一个服务而不是两个服务?

编辑:这篇博客文章 - Meineck.Net向您展示如何配置您的服务以实现您所追求的目标,但同样,它几乎是一个解决方案.祝好运.