0

我想使用位于 2 个不同主机上的 2 个 WCF Web 服务(第一个在 HTTPS 中可访问,第二个在 HTTP 中)。两者都提供相同的方法,但第一个是在生产服务器上,而第二个是在测试服务器上。

当我在 Visual Studio 2012 (express) 中添加引用时,我得到 2 个命名空间;目前,我发现使用这些服务的唯一方法是使用这些命名空间中生成的接口和类。我只想使用一项 Web 服务,具体取决于配置项,指示我是处于调试模式还是发布模式。我怎样才能通过代码做到这一点?

这是我的配置文件:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_ILotWebContract">
                <security mode="Transport" />
            </binding>
            <binding name="BasicHttpBinding_ILotWebContract1" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://xxx/Services/WebService/LotWebService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ILotWebContract"
            contract="eMol.ILotWebContract" name="BasicHttpBinding_ILotWebContract" />
        <endpoint address="http://yyy/Services/WebService/LotWebService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ILotWebContract1"
            contract="eMolTest.ILotWebContract" name="BasicHttpBinding_ILotWebContract1" />
    </client>
</system.serviceModel>

我试图更改端点的地址:

eMolTest.LotWebContractClient client = new eMolTest.LotWebContractClient();
client.Endpoint.Address = new System.ServiceModel.EndpointAddress("https://xxx/Services/WebService/LotWebService.svc");
client.Endpoint.Binding = new System.ServiceModel.WSHttpBinding(System.ServiceModel.SecurityMode.Transport);

但是运行客户端时产生的异常表明客户端和服务之间的链接可能不对应。

当我调用代理的构造函数时,我还尝试指出端点的名称:

eMolTest.LotWebContractClient client = new eMolTest.LotWebContractClient("BasicHttpBinding_ILotWebContract");

但是由于命名空间不同,它再次不起作用(合同的名称包含命名空间的名称,因此无法在配置文件中定义的端点中找到)。

所以,如果有人有一个好主意......

谢谢 !克里斯

4

1 回答 1

0

如果您有两个具有完全相同合同的服务,您应该在所有三方之间将其合同作为合同程序集共享,并使用ChannelFactory类自己创建代理。这样,您就没有两个服务引用,尽管它们代表相同的合同,但从编译器的角度来看是不兼容的。

于 2013-08-27T14:37:17.147 回答