启用IDispatchMessageFormatter后,我在服务中看到可以为空的参数

问题描述 投票:0回答:2

将我的格式化程序添加到操作行为后:

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

在Formatter中我有空Deserialize方法因为我想使用默认行为

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

但在序列化

    public System.ServiceModel.Channels.Message SerializeReply(System.ServiceModel.Channels.MessageVersion messageVersion, object[] parameters, object result)
            {
//some code
             }

问题是在启用此类之后,service方法中的参数始终显示为null,但在IDispatchMessageInspector类中,我看到参数是正确发送的。我不知道为什么会发生这种情况,我只添加了这个消息格式化程序代码,Deserialize的空类可能会导致这种情况吗?

wcf
2个回答
1
投票

当我们实现IDispatchMessageFormatter接口时,通常我们不认为DeserializeRequest方法是重要的,因为它没有返回任何数据。这是误导,因为方法需要做某事。

正确传递参数的最简单方法是使用基本DispatchMessageFormatter。将其添加到构造函数中。

public class ResponseJsonFormatter : IDispatchMessageFormatter
{
    IDispatchMessageFormatter basicDispatchMessageFormatter;
    OperationDescription Operation;
    public ResponseJsonFormatter(OperationDescription operation, IDispatchMessageFormatter inner)
    {
        this.Operation = operation;
        this.basicDispatchMessageFormatter = inner;
    }

    public void DeserializeRequest(Message message, object[] parameters)
    {
        basicDispatchMessageFormatter.DeserializeRequest(message, parameters);
    }

    public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
    {
        string json=Newtonsoft.Json.JsonConvert.SerializeObject(result);
        byte[] bytes = Encoding.UTF8.GetBytes(json);
        Message replyMessage = Message.CreateMessage(messageVersion, Operation.Messages[1].Action, new RawDataWriter(bytes));
        replyMessage.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
        return replyMessage;
    }
}

并在行为中启动它:

public class ClientJsonDateFormatterBehavior : IOperationBehavior
{
    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {
        // throw new NotImplementedException();
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
    }

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

    public void Validate(OperationDescription operationDescription)
    {
        // throw new NotImplementedException();
    }
}

您可以在github分支DateTimeFormatterWithParams中查看此处的工作示例


2
投票

如果您没有在DeserializeRequest上提供自己的逻辑,则没有默认行为。您需要引用现有的格式化程序并在ServerMessageFormater中手动委派或提供自己的逻辑。

© www.soinside.com 2019 - 2024. All rights reserved.