WCF 将对象从 MessageInspector 发送到操作方法 [英] WCF Sending object from MessageInspector to operation method

查看:23
本文介绍了WCF 将对象从 MessageInspector 发送到操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个非常复杂的嵌套网络服务.消息并不总是在操作中正确反序列化,供应商建议我使用 MessageInspectors 来正确获取数据.这已经正常工作了一段时间,但我开始看到问题,因为在 MessageInspector 中,我在类中设置了一个静态 XElement 变量作为无法将 XElement 对象传递给实例的变通方法为该调用启动的类.

I'm working with a very complicated and nested web service. The messages don't always deserialize properly at the operation and the vendor has suggested I use MessageInspectors to correctly get the data. This has been working correctly for some time but I'm starting to see issues because in the MessageInspector I'm setting a static XElement variable in the class as a work-around for not being able to pass the XElement object to the instance of the class that gets started for this call.

我会在调用操作后立即将静态变量复制到实例,但我遇到了这个问题.

I'm immediately copying the static variable to an instance as soon as the operation is called but I have had issues with this.

在 MessageInspector 反序列化 SOAP 并将其传递给操作方法的正确方法是什么?

What is the correct way to deserialize the SOAP at the MessageInspector and pass that to the operation method?

推荐答案

根据你的描述,需要自己序列化SOAP消息.Messageinspector 只是一个消息拦截器,可以用来修改消息的内容.messageinspector 中的序列化是不合适的.我建议您使用 DataContractSerializerOperationBehavior.在继承 DataContractSerializerOperationBehavior 的类中序列化 SOAP 消息.

According to your description, you need to serialize the SOAP message by yourself. Messageinspector is just a message interceptor, which can be used to modify the content of message. Serialization in messageinspector is not appropriate. I suggest that you use DataContractSerializerOperationBehavior. Serialize the SOAP message in a class that inherits DataContractSerializerOperationBehavior.

这是一个演示:

    public class NetDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior

{

    public NetDataContractSerializerOperationBehavior(OperationDescription operationDescription)

        : base(operationDescription)

    {

    }

    public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)

    {

        return new NetDataContractSerializer(name, ns);

    }

    public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)

    {

        return new NetDataContractSerializer(name, ns);

    }

}

这是一个继承DataContractSerializerOperationBehavior的类,你可以在其中编写自己的序列化方法.

This is a class that inherits DataContractSerializerOperationBehavior,in which you can write your own serialization methods.

     ServiceHost selfHost = new ServiceHost(typeof(Service1));
        foreach (ServiceEndpoint serviceEndpoint in selfHost.Description.Endpoints)

        {

            foreach (OperationDescription operation in serviceEndpoint.Contract.Operations)

            {
                operation.Behaviors.Remove<DataContractSerializerOperationBehavior>();

                operation.Behaviors.Add(new NetDataContractSerializerOperationBehavior(operation));

            }

        }

将您自己的序列化行为添加到服务行为并删除默认序列化行为.

Add your own serialization behavior to the service behavior and remove the default serialization behavior.

这是一个关于 DataContractSerializerOperationBehavior 的链接:

This is a link about DataContractSerializerOperationBehavior:

https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.description.datacontractserializeroperationbehavior?view=dotnet-plat-ext-3.1

这篇关于WCF 将对象从 MessageInspector 发送到操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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