2013-08-27 134 views
0

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

当我在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"); 

但同样不会因为它们是不同的命名空间的工作(合同的名称中包含命名空间的名字,因此不能在配置文件中定义的端点中找到)。

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

谢谢! 克里斯

+1

这可能是手动创建代理可能是您的最佳选择,而不是使用内置于Visual Studio的自动工具。 –

+0

您应该有1个服务部署到两个不同的位置。然后你只需要1个服务引用,你可以更新app.config文件来指向你正在做的事情。 –

回答

0

如果你有完全相同的合同两项服务,你应该分享它的合同三方之间的合同装配并使用ChannelFactory类来创建自己的代理。这样,您没有两个服务引用,尽管它们代表相同的合约,但从编译器的角度来看,它们是不兼容的。

+0

非常感谢,我会试试这个。 – user2721868