多个类实现相同的服务合同

时间:2012-09-07 07:27:34

标签: wcf

我创建了一个WCF服务,其中我有一个服务合同和多个实现相同合同的服务类。 你能告诉我在app.config中编辑什么以及如何在控制台应用程序中托管这项服务。 我有一份服务合同,我在wcf中的三个* .cs文件中实现了这个合同。

感谢。

这是我写的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 name="ProductService.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/Design_Time_Addresses/ProductService/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="wsHttpBinding" contract="ProductService.IService1">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
        <service name="ProductService.RegistrationService">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:7777/Design_Time_Addresses/ProductService/RegistrationService/"/>
                </baseAddresses>
            </host>
            <endpoint address="" binding="wsHttpBinding" contract="ProductService.IService1">
                <identity>
                    <dns value ="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
        <service name="ProductService.ProductService">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8888/Design_Time_Addresses/ProductService/ProductService/"/>
                </baseAddresses>
            </host>
            <endpoint address="" binding="wsHttpBinding" contract="ProductService.IService1">
                <identity>
                    <dns value ="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
        <service name="ProductService.CatogeryService">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8080/Design_Time_Addresses/ProductService/CatogeryService/"/>
                </baseAddresses>
            </host>
            <endpoint address="" binding="wsHttpBinding" contract="ProductService.IService1">
                <identity>
                    <dns value ="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 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="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

1 个答案:

答案 0 :(得分:0)

我想你已经在服务合同上定义了几个服务操作?你想要做的是不可能的,也不是可取的。

由于逻辑服务只与一个服务合同相关联,因此您无法为同一合同添加多个服务。

可以为每个服务合同添加多个服务端点,但不能基于每个操作。您可能希望这样做以跨两个或更多不同的传输提供服务,例如HTTP和HTTPS。

您可以从单个服务中消耗所有操作(您定义了4个服务,因此只需删除最后三个服务),但我不认为这是您想要做的。

要在单独的服务端点上托管每个服务操作,您需要将服务合同分解为多个合同。

这是更理想的,因为它减少了端点之间的耦合,使得更容易理解正在发生的事情。