是否可以拥有多个Wcf服务类?如果是,那怎么样?

时间:2012-11-27 12:12:57

标签: asp.net wcf web-services

是否可以拥有多个Wcf服务类。因为我有两个契约接口,并希望在两个不同的服务类中实现。有可能吗?

2 个答案:

答案 0 :(得分:1)

如果你有2个合同说IService1和IService2,你有两个选择:

  • 两个服务合同都是由一个服务实现的,该服务实现了IService1和IServe2的所有方法。然后您的配置文件将如下所示:

    <services>
        <service name="MyNamespace.Service">
            <host>
                <baseAddresses>
                    <add baseAddress="http:localhost:8080" />
                </baseAddresses>
            </host>
            <endpoint address="Service1" binding="basicHttpBinding" contract="MyNameSpace.IService1" />
            <endpoint address="Service2" binding="basicHttpBinding" contract="MyNameSpace.IService2" />
        </service>
    </services>
  • 每个合同都是通过单独的服务实现的,比如说Service1和Service2,那么你的配置将是这样的:

    <services>
        <service name="MyNamespace.Service1">
            <host>
                <baseAddresses>
                    <add baseAddress="http:localhost:8081" />
                </baseAddresses>
            </host>
            <endpoint address="Service1" binding="basicHttpBinding" contract="MyNameSpace.IService1" />
        </service>
        <service name="MyNamespace.Service2">
            <host>
                <baseAddresses>
                    <add baseAddress="http:localhost:8082" />
                </baseAddresses>
            </host>
            <endpoint address="Service2" binding="basicHttpBinding" contract="MyNameSpace.IService2" />
        </service>
    </services>

答案 1 :(得分:0)

是的,但是暴露这些服务是另一回事,您不能在单个地址上公开两个合同(除非您将它们包装在一个单独的接口中,不推荐)。您需要为web.config中的两个服务合同提供不同的端点。 Forexample,

 <services>
  <service name="TwoContracts.Service1">
    <endpoint address="" binding="basicHttpBinding" contract="TwoContracts.IService1">
    </endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://yoururl.com/TwoContracts/Service1" />
      </baseAddresses>
    </host>
  </service>
  <service name="TwoContracts.Service2">
    <host>
      <baseAddresses>
        <add baseAddress="http://yoururl.com/TwoContracts/Service2" />
      </baseAddresses>
    </host>
    <endpoint address="" binding="basicHttpBinding"
      contract="TwoContracts.IService2" />
  </service>
</services>