2011-11-11 78 views
0

在SOAP信封另一個命名空間我連接到一個SOAP1.1服務。這是一個使用行業標準接口定義(名爲MultiSpeak,在公用程序空間中非常常見)的預先存在的服務包括WCF客戶

MultiSpeak標準不包括傳遞供應商要求的CustomerID的條款,因此他們修改了SoapEvenelope略微。我的問題是我無法確定如何說服WCF發出正確的XML。

我現在的信封看起來是這樣的:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <h:MultiSpeakMsgHeader UserID="****" Pwd="****" Company="****" 
     xmlns="http://www.multispeak.org/Version_3.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:h="http://www.multispeak.org/Version_3.0" /> 
    </s:Header> 
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <GetAMRSupportedMeters xmlns="http://www.multispeak.org/Version_3.0" /> 
    </s:Body> 
</s:Envelope> 

這裏是什麼,我需要它看起來像工作:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <h:MultiSpeakMsgHeader UserID="****" Pwd="****" Company="****" 
     vendor:CustomerID="StringValue" 
     xmlns="http://www.multispeak.org/Version_3.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:h="http://www.multispeak.org/Version_3.0" 
     xmlns:vendor="http://www.MyVendor.com/Multispeak3"/> 
    </s:Header> 
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <GetAMRSupportedMeters xmlns="http://www.multispeak.org/Version_3.0" /> 
    </s:Body> 

所以這是引入了新的命名空間(我的供應商的自定義命名空間),並在現有的「MultiSpeakMsgHeader」對象,引入了新的特性,被稱爲「客戶ID」,但在XML屬性,表示屬性是不同的命名空間

他們提供給我的WSDL(標準MultiSPeak WSDL)不會生成此消息。

我覺得很容易將「CustomerID」作爲字符串屬性添加到reference.cs中的MultiSpeakMsgHeader對象,但它不會被正確的xmlns裝飾發出,因此不會工作(是的,我測試過那......沒有名字空間,沒有愛)。

我不知所措。我嘗試調整他們的WSDL並重新生成以使其工作,但沒有運氣。

任何提示,我就肯定是感激。我已經爲這個問題丟了太多時間了。

謝謝大家。

回答

1

您可以嘗試建立自己的SOAP頭。一些示例代碼如下:

using (OperationContextScope scope = new OperationContextScope(objService.InnerChannel)) 

     { 
      UsernameToken objUsernameToken = new UsernameToken() { Username = "rajesh", Password = "rajesh" }; 
      List<Type> obj = new List<Type>(); 
      obj.Add(typeof(UsernameToken)); 

      //XmlObjectSerializer ser = new DataContractSerializer(typeof(Security), "Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", obj); 

      XmlObjectSerializer ser = new CustomXmlSerializer(typeof(Security), "Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); 

      Security security = new Security(); 
      security.UsernameToken = objUsernameToken; 

      MessageHeader header = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", 
                   security, ser, false); 

      OperationContext.Current.OutgoingMessageHeaders.Add(header); 

      try 
      { 
       //Would get a exception but the response was successful. You can see that in fiddler. 
       //The cause for the exception is that the response has the security elements mustUnderstand set to 1 chagning that to 0 would resolve the problem. Need to find on how to do that 
       string response = objService.GetInformation(); 
      } 
      catch (Exception ex) 
      { 
       OperationContext.Current.IncomingMessageHeaders.RemoveAll("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); 
       //throw; 
      } 

     } 

希望你可以通過上面的代碼來適應你的要求。