Wcf 调用soap服务时,dispatchOperation.Formatter为NULL

Wcf 调用soap服务时,dispatchOperation.Formatter为NULL,wcf,Wcf,我有IDispatchMessageFormatter实现 class ServerMessageFormatter : IDispatchMessageFormatter { private IDispatchMessageFormatter Formatter; public ServerMessageFormatter(IDispatchMessageFormatter formatter) { this.Formatter = forma

我有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);
    }
}
并且在运行中

        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());
GetInfoRequest消息=CheckedFields;
字符串soap=@”
ServiceModel/IService/GetSoapData
";
XmlSerializer serializer=新的XmlSerializer(typeof(GetInfoRequest));
HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(“http://dev.add.renault.com/Service.svc/soap");
MemoryStream stream1=新的MemoryStream();
serializer.Serialize(流1,消息);
1.位置=0;
StreamReader sr=新的StreamReader(stream1);
字符串t=sr.ReadToEnd();
t=t.移除(0,22).修剪();
t=string.Format(soap,t);
ascienceoding encoding=新的ascienceoding();
请求超时=9999999;
request.ContentLength=t.Length;
request.Method=“POST”;
request.ContentType=“application/soap+xml;charset=utf-8”;
request.Accept=“application/soap+xml;charset=utf-8”;
使用(Stream stm=request.GetRequestStream())
{
使用(StreamWriter stmw=新StreamWriter(stm))
{
stmw.Write(t);
}
}
var response=(HttpWebResponse)request.GetResponse();
var abc=newstreamreader(response.GetResponseStream());
问题是,当我调用REST服务并在DeserializeRequest中设置断点时,我看到格式化程序已经从操作行为中设置了值。但当调用soap服务时,我的格式化程序有空值,反序列化被中止。为什么打电话给soap时我会有这个问题?有什么想法吗


不幸的是,我无法在操作行为中触发断点,并查看具有dispatchOperation的值…

您没有显示如何配置服务以添加自定义IDispatchMessageFormatter扩展。因此,这里只是猜测一下,您可能只是将其添加到webHttpBinding端点,而不是添加到基于soap的绑定端点。如果您正在使用WebHttpBehavior方法(GetRequestDispatchFormatter和GetReplyDispatchFormatter),那么这将不适用于soap端点。本文很好地概述了如何将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 ---//

是的,我对rest使用webHttpBinding,对soap使用customBinding。对于rest,我在WebHttpBehavior中添加了格式化程序。我可能不为soap绑定添加它。代码id post u链接的哪一部分负责将格式化程序添加到soap绑定?我找不到它:/你是对的。我必须添加到我的customEndpoint WebHttpBehaviorI我没有使用自托管选项,但看起来我遇到了相同的问题,我将如何添加您在此建议的行为?