2010-08-09 74 views
14

我有一个可在多个服务器上使用的SOAP Webservice,因此具有多个端点。我想避免添加具有不同名称的多个服务引用(C#SOAP端口客户端),以便与此服务交谈,因为API完全相同。使用具有不同端点URI的C#服务参考SOAP客户端

有没有办法在运行时配置端点URI?

回答

4

我也很难找到这一个。我最后只是借来的配置结合,并这样做:

private static wsXXXX.IwsXXXXClient wsXXXXClientByServer(string sServer) 
{ 
    // strangely, these two are equivalent 
    WSHttpBinding binding = new WSHttpBinding("WSHttpBinding_IwsXXXX"); 
    // WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message, false); 

    EndpointAddress remoteAddress = new EndpointAddress(new Uri(string.Format("http://{0}:8732/wsXXXX/", sServer)), new UpnEndpointIdentity("[email protected]")); 

    return new wsXXXX.IwsXXXXClient(binding, remoteAddress); 
} 
23

我用伟大的工程如下:

 ServiceReference1.wsSoapClient ws= new ServiceReference1.wsSoapClient(); 
     ws.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://xxx/myservice.asmx"); 
+0

我碰到下面的错误,当我尝试这与不同的端点:“无法识别的消息版本“。你知道为什么吗? – Preexo 2013-04-04 09:30:10

+0

另外一个问题,我应该**(A)**创建'wsSoapClient()'的类实例并在每次调用WebMethod或**(B)时使用它**使用'using'语句按需实例化并尽快释放它? – dialex 2016-02-24 17:54:36