如何在1个servicehost中托管2个wcf服务

时间:2013-04-10 13:53:36

标签: wcf service host

我正在开发一个wcf项目 我有1个服务,2个合同(2个端点) 1个服务,1个合同(1个终点) 我想为我的服务制作一个单一的ServiceHost。 我可以为2个服务提供2个主机,但我只需要1个主机。

ServiceHost myService =
            new ServiceHost(typeof(CustomerOrder),
            new Uri("net.tcp://localhost:9191/T1Flondor_Antal"));

            ServiceHost myService2 =
            new ServiceHost(typeof(ReportServiceCO),
            new Uri("net.tcp://localhost:9191/T1Flondor_Antal"));

和配置:

<system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="T1Flondor_Antal.CustomerOrder"
     behaviorConfiguration="T1Flondor_Antal.MessageBehavior">
        <endpoint address ="net.tcp://localhost:9191/T1Flondor_Antal/Customer"
       binding="netTcpBinding"
       contract="T1Flondor_Antal.ICustomer">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address ="net.tcp://localhost:9191/T1Flondor_Antal/Order"
       binding="netTcpBinding"
       contract="T1Flondor_Antal.IOrder">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex"
       binding="mexTcpBinding"
       contract="IMetadataExchange"/>
      </service>


      <service name="T1Flondor_Antal.ReportServiceCO"
     behaviorConfiguration="T1Flondor_Antal.MessageBehavior">
        <endpoint address ="net.tcp://localhost:9191/T1Flondor_Antal/Report"
       binding="netTcpBinding"
       contract="T1Flondor_Antal.IReport">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>   
        <endpoint address="mex"
       binding="mexTcpBinding"
       contract="IMetadataExchange"/>
      </service>



    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="T1Flondor_Antal.MessageBehavior">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

1 个答案:

答案 0 :(得分:2)

请参阅Run WCF ServiceHost with multiple contracts

您需要在单个对象中实现这两个接口,并在构建ServiceHost时使用该对象。

否则:不,你不能这样做。