WCF Sending object from MessageInspector to operation method

白昼怎懂夜的黑 提交于 2021-02-11 14:37:48

问题


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.

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


回答1:


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.

Here is a demo:

    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);

    }

}

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.

This is a link about DataContractSerializerOperationBehavior:

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



来源:https://stackoverflow.com/questions/62026546/wcf-sending-object-from-messageinspector-to-operation-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!