在服务器反序列化WCF消息 [英] Deserializing WCF Message at Server

查看:158
本文介绍了在服务器反序列化WCF消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现(通过IDispatchMessageInspector)的自定义消息检查拦截那些在WCF服务的服务器端收到的消息,所以我可以尝试反序列化消息并应用一些具体的业务逻辑。我现在遇到的问题是,当我写的MessageBuffer的内容到一个新的MemoryStream,然后尝试反序列化,我得到一个错误,指出在根级别的数据无效。行1,位置1 。我不知道数据传递的是跳过检查员使一切工作正常有效

I implemented a custom message inspector (via IDispatchMessageInspector) to intercept messages that are received on the server side of a WCF service so I can attempt to deserialize the message and apply some specific business logic. The problem I'm encountering is when I write the MessageBuffer's contents to a new MemoryStream and then try to deserialize, I get an error that says "The data at the root level is invalid. Line 1, position 1." I do know the data being passed in is valid as skipping over the inspector makes everything work fine.

示例代码:

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
    {
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
        request = buffer.CreateMessage();
        string msg = buffer.CreateMessage().ToString();

        var dc = new DataContractSerializer(typeof(Adder));

        using (var stream = new MemoryStream())
        {
            buffer.WriteMessage(stream);

            stream.Position = 0;

            //deserializing error occurs here
            var c = dc.ReadObject(stream);
        }

        return null;
    }

下面是加法类/接口:

    [DataContract(Name = "adder", Namespace = "http://test.com")]
public class Adder
{
    [DataMember(Name = "first")]
    public int First { get; set; }

    [DataMember(Name = "second")]
    public int Second { get; set; }
}

    [ServiceContract(Namespace = "http://test.com")]
public interface ITestSvc
{
    [OperationContract(Name = "add")]
    int Add(Adder adder);
}



任何建议或是否有这个一个更好的选择?我的主要目标是阅读上来到我的服务好每一位WCF请求XML(在反序列化对象)。

Any suggestions or is there a better option for this? My main goal is to read the XML (in a deserialized object) on every WCF request that comes into my service.

推荐答案

我最终会在邮件头的路线,像迈克帕克希尔建议。

I ended up going the message header route, like Mike Parkhill suggested.

这篇关于在服务器反序列化WCF消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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