调用soap服务时dispatchOperation.Formatter NULL [英] dispatchOperation.Formatter NULL when call soap service
问题描述
我有 IDispatchMessageFormatter 实现
I have IDispatchMessageFormatter implementation
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中
and in 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 已从操作行为中设置了值.但是当调用soap服务时,我的格式化程序具有空值并且反序列化被中止.为什么在调用肥皂时我有这个问题?有什么想法吗?
Problem is that when I call my REST service and set breakpoint in DeserializeRequest I see that Formatter has set value from Operation Behavior. But when call soap service my Formatter has null value and deserialization was aborted. Why when calling soap I have that problem? Any idea?
不幸的是,我无法在操作行为中触发断点并查看 dispatchOperation 的值...
Unfortunatelly I can not fire breakpoint in Operaration Behavior and see what value have dispatchOperation...
推荐答案
您没有展示如何配置服务以添加自定义 IDispatchMessageFormatter 扩展.所以只是在这里猜测一下,您可能只将它添加到 webHttpBinding 端点,而不是基于soap的绑定端点.如果您正在使用 WebHttpBehavior 方法(GetRequestDispatchFormatter & GetReplyDispatchFormatter),那么这将不适用于您的soap 端点.此博文很好地概述了如何将 IDispatchMessageFormatter 与 webHttpBinding 和 basicHttpBinding 结合使用.
You don't show how you configure your service to add your custom IDispatchMessageFormatter extension. So just taking a guess here, you may be adding it only to the webHttpBinding endpoint and not to the soap based binding endpoint. If you are using the WebHttpBehavior methods (GetRequestDispatchFormatter & GetReplyDispatchFormatter) then this won't apply to your soap endpoints. This blog post has a good overview of how to use IDispatchMessageFormatter with webHttpBinding and basicHttpBinding.
文章中显示如何将自定义消息格式化程序添加到 basicHttpBinding 的具体代码如下.在该部分之前,他解释了为什么需要这种方法.
The specific code in the article that shows how to add the a custom message formatter to the basicHttpBinding is below. Just prior to that section, he explains why this approach is necessary.
//--- 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 ---//
这篇关于调用soap服务时dispatchOperation.Formatter NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!