2011-10-12 57 views
2

我IDispatchMessageFormatter實施dispatchOperation.Formatter NULL時調用SOAP服務

class ServerMessageFormatter : IDispatchMessageFormatter 
{ 
    private IDispatchMessageFormatter Formatter; 

    public ServerMessageFormatter(IDispatchMessageFormatter formatter) 
    { 
     this.Formatter = formatter; 
    } 

    public void DeserializeRequest(System.ServiceModel.Channels.Message message, object[] parameters) 
    { 
     Formatter.DeserializeRequest(message, parameters); 
    } 
} 

和OperationBegavior

 public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) 
    { 
     ServerMessageFormatter Formatter = new ServerMessageFormatter(dispatchOperation.Formatter); 
     dispatchOperation.Formatter = Formatter; 
    } 

,並調用SOAP服務

GetInfoRequest message = CheckedFields; 
    string soap = @"<?xml version=""1.0"" encoding=""utf-8""?> 
      <soap12:Envelope xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope""> 
       <soap12:Header> 
       <Action soap12:mustUnderstand=""1"" xmlns=""http://www.w3.org/2005/08/addressing"">ServiceModel/IService/GetSoapData</Action> 
       </soap12:Header> 
      <soap12:Body> 
     <GetInfoRequest xmlns=""ServiceModel""> 
      <Data xmlns:d4p1=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/> 
     </GetInfoRequest> 
     </soap12:Body> 
     </soap12:Envelope>"; 
    XmlSerializer serializer = new XmlSerializer(typeof(GetInfoRequest)); 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://dev.add.renault.com/Service.svc/soap"); 
    MemoryStream stream1 = new MemoryStream(); 
    serializer.Serialize(stream1, message); 
    stream1.Position = 0; 
    StreamReader sr = new StreamReader(stream1); 
    string t = sr.ReadToEnd(); 
    t = t.Remove(0, 22).Trim(); 
    t = string.Format(soap, t); 
    ASCIIEncoding encoding = new ASCIIEncoding(); 
    request.Timeout = 99999999; 
    request.ContentLength = t.Length; 
    request.Method = "POST"; 
    request.ContentType = "application/soap+xml; charset=utf-8"; 
    request.Accept = "application/soap+xml; charset=utf-8"; 

    using (Stream stm = request.GetRequestStream()) 
    { 
     using (StreamWriter stmw = new StreamWriter(stm)) 
     { 
      stmw.Write(t); 
     } 
    } 

    var response = (HttpWebResponse)request.GetResponse(); 
    var abc = new StreamReader(response.GetResponseStream()); 

問題是,當我打電話給我REST服務並在DeserializeRequest中設置斷點我看到Formatter已經設置了val來自Operation Behavior。但是,當調用soap服務我的格式化程序有空值和反序列化被中止。爲什麼在撥打肥皂時我有這個問題?任何想法?

可惜的是,我不能在火行爲Operaration斷點,看看什麼樣的價值有dispatchOperation ...

回答

2

你不告訴你如何配置你的服務來添加自定義IDispatchMessageFormatter擴展。因此,只需在此猜測,您可能只會將其添加到webHttpBinding端點,而不是添加到基於soap的綁定端點。如果您正在使用WebHttpBehavior方法(GetRequestDispatchFormatter & GetReplyDispatchFormatter),那麼這將不適用於您的soap端點。這個blog post很好的概述瞭如何在webHttpBinding和basicHttpBinding中使用IDispatchMessageFormatter。

編輯: 本文中介紹瞭如何將自定義消息格式化程序添加到basicHttpBinding的具體代碼如下。在該部分之前,他解釋了爲什麼這種方法是必要的。

//--- snipped ---// 

    string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
    ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
    ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); 
    endpoint.Contract.Operations.Find("Add").Behaviors.Add(new MyOperationBehaviorAttribute()); 
    host.Open(); 

    //--- snipped ---// 
+0

是的我使用webHttpBinding休息和customBinding肥皂。爲了休息我在WebHttpBehavior中添加格式化程序。有可能我不會將它添加到肥皂綁定中。哪些部分的代碼ID後u鏈接負責將格式化程序添加到肥皂綁定?我有問題找到它:/ – netmajor

+0

你說得對。我必須添加到我的customEndpoint webhttpBehavior中 – netmajor