2

是否可以有多个 Wcf 服务类。因为我有两个合约接口并且想在两个不同的服务类中实现。可能吗?

4

2 回答 2

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>
于 2012-11-27T13:13:12.547 回答
0

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

 <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>
于 2012-11-27T12:46:37.663 回答