2013-07-17 24 views
2

嗨, 我有2個客戶端與2個不同的服務器。 生成wsdl類後,我相應地在SoapHttpClientProtocol consructor中更改客戶端的url地址。更改c#WebReference的網址

this.Url = "http://10.0.3.5:88/SomeName/dish 

this.Url = "http://192.168.20.5:88/SomeOtherName/dish 

但我不能在運行時改變SoapDocumentMethodAttribute。不改變它,我的方法不會返回DataSet。在改變屬性的所有地址後,一切正常。

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://10.0.3.5:88/SomeName/EuroSoft/ProductTransferExecute", RequestNamespace = "http://10.0.3.5:88/SomeName/dish", ResponseNamespace = "http://10.0.3.5:88/SomeName/dish", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = 

System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] 
public System.Data.DataSet ProductTransferExecute([System.Xml.Serialization.XmlElementAttribute(IsNullable = true)] string department, [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)] string XMLproducts, out int sqlcode) {} 

服務由Sybase Anywhere 9數據庫生成。是否有可能改變它的動態?什麼需要是相同的才能工作?

+0

我的問題是WSA中的SOAPAction問題隨服務器而變化。我會嘗試這個解決方案[鏈接](http://social.msdn.microsoft.com/Forums/vstudio/en-US/0e9874a5-ce16-4d69-936c-4af46d6a02a2/soap-action-override-at-runtime- from-imported-wsdl) – koziol

+0

另一個類似的問題[stack link](http://stackoverflow.com/questions/8504820/dynamically-changing-attributes-for-properties) – koziol

回答

0

創建CustomSoapHttpClientProtocol:

public class CustomSoapHttpClientProtocol : SoapHttpClientProtocol 
{ 
    public string SoapActionUrl { get; private set; } 

    public CustomSoapHttpClientProtocol(string soapActionUrl) 
    { 
     this.SoapActionUrl = soapActionUrl; 
    } 
    protected override WebResponse GetWebResponse(WebRequest request) 
    { 
     const string soapAction = "SOAPAction"; 
     if (request.Headers.Count > 0 && request.Headers.AllKeys.Contains(soapAction)) 
     { 
      request.Headers[soapAction] = SoapActionUrl; 
     } 
     WebResponse response = base.GetWebResponse(request); 
     return response; 
    } 

然後在您的代理類與CustomSoapHttpClientProtocol替換SoapHttpClientProtocol。