简体   繁体   English

WCF 4.5-多个Web服务

[英]WCF 4.5 - multiple web services

I would like to consume 2 WCF web services located on 2 different hosts (the first accessible in HTTPS and the second one in HTTP). 我想使用位于2个不同主机上的2个WCF Web服务(第一个可在HTTPS中访问,第二个可在HTTP中访问)。 Both present the same methods, but the first one is on the production server, while the second one is on a test server. 两种方法都具有相同的方法,但是第一种方法在生产服务器上,而第二种方法在测试服务器上。

When I add the references in my Visual Studio 2012 (express), I get 2 namespaces ; 当我在Visual Studio 2012(速成版)中添加引用时,得到2个名称空间; for now, the only way I found to consume these services is to use the interfaces and classes generated in these namespaces. 目前,我发现使用这些服务的唯一方法是使用在这些命名空间中生成的接口和类。 I would like to use only one web service, depending of a configuration item, indicating if I'm in debug or release mode. 根据配置项的不同,我只想使用一个Web服务,指示我是处于调试还是发布模式。 How can I do that by code ? 我该如何通过代码做到这一点?

Here is my config file : 这是我的配置文件:

<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>

I tried to change the endpoint's address : 我试图更改端点的地址:

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);

but the exception generated while running the client indicate that the links between client and service probably do not correspond. 但是运行客户端时生成的异常表明客户端与服务之间的链接可能不对应。

I also tried to indicate the name of the endpoint when I call the proxy's contructor : 当我调用代理的构造函数时,我还尝试指出端点的名称:

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

but again it does not work because of the namespaces which are different (the contract's name contains the namespace's name, and can therefore not be found in the endpoint as defined in the config file). 但是由于命名空间不同(合约的名称包含命名空间的名称,因此无法在配置文件中定义的终结点中找到),它仍然无法工作。

So, if someone has a good idea ... 所以,如果有人有一个好主意...

Thanks ! 谢谢 ! Chris 克里斯

If you have two services with the exact same contract, you should share it's contract as a contract assembly between all three parties and use a ChannelFactory class to create the proxy yourself. 如果您具有完全相同的合同的两个服务,则应在所有三个参与方之间共享该合同作为合同程序集 ,并使用ChannelFactory类自己创建代理。 That way you don't have two service references that although they represent the same contract are incompatible from a compiler perspective. 这样,您就不会有两个服务引用,尽管它们表示相同的合同,但它们从编译器的角度来看是不兼容的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM