我有IDispatchMessageFormatter实现
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;
}
并致电肥皂服务
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已经从Operation Behavior设置了值。但是当调用soap服务时,我的Formatter具有null值并且反序列化被中止。为什么在打肥皂时我有这个问题?有什么想法吗?
不幸的是,我不能在操作行为中触发断点并查看dispatchOperation的值...
答案 0 :(得分:2)
您没有显示如何配置服务以添加自定义IDispatchMessageFormatter扩展。所以在这里猜一下,你可能只是将它添加到webHttpBinding端点而不是基于soap的绑定端点。如果您使用的是WebHttpBehavior方法(GetRequestDispatchFormatter&amp; GetReplyDispatchFormatter),那么这将不适用于您的soap端点。这个blog post很好地概述了如何将IDispatchMessageFormatter与webHttpBinding和basicHttpBinding一起使用。
编辑: 本文中的特定代码显示了如何将自定义消息格式化程序添加到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 ---//