启用 IDispatchMessageFormatter 后,我在服务中看到可为空的参数 [英] After Enable IDispatchMessageFormatter I see nullable parameters in service

查看:20
本文介绍了启用 IDispatchMessageFormatter 后,我在服务中看到可为空的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

after add my formatter to operation behavior :

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

在格式化程序中,我有空的反序列化方法,因为我想使用默认行为

In Formatter I have empty Deserialize method cause I want to use default behavior

 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
             }

问题是启用此类后,服务方法中的参数始终显示为空,但在 IDispatchMessageInspector 类中,我看到参数已正确发送.我不知道为什么会这样,我只添加了此消息格式化程序代码,反序列化的空类可能会导致此问题?

Problem is that after enable this class, parameters in service method always was show as null, but in IDispatchMessageInspector class I see that parameters is send properly. I don't know why it's happening, I only add this message formatter code , it is possible that empty class for Deserialize causes this ?

推荐答案

当我们实现 IDispatchMessageFormatter 接口时,通常我们并不认为 DeserializeRequest 方法很重要,因为它不返回任何数据.这是一种误导,因为方法需要做一些事情.

When we are implementing IDispatchMessageFormatter interface usually we are not thinking that method DeserializeRequest is something important as it is not returning any data. This is misleading as method needs to do something.

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

The easiest way to make parameters correctly passed is to use base DispatchMessageFormatter. Add it to the constructor.

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 中查看工作示例

这篇关于启用 IDispatchMessageFormatter 后,我在服务中看到可为空的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆