NetDataContractSerializer 和 WCF

发布于 2024-08-04 16:29:27 字数 2520 浏览 4 评论 0 原文

我有一个场景,我需要使用 NetDataContractSerializer 而不是 DataContractSerializer。

到目前为止,我一直在使用这种方法 - http ://www.pluralsight.com/community/blogs/aaron/archive/2006/04/21/22284.aspx - 看起来很简单,但根据此 http://social.msdn.microsoft.com/forums/en-US /wcf/thread/cb0c56c0-3016-4cda-a3c7-8826f8cc5bb0/ 方法不正确。

环顾四周,我发现了以下内容 - http://social.msdn.microsoft.com/forums/en-US/wcf/thread/f30ecd17-cac0-4cdc-8142-90b5f411936b/

基本上,您需要运行以下命令:

这就是我在客户端的内容:

ChannelFactory<IPersonService> factory = new ChannelFactory<IPersonService>("WSHttpBinding_IPersonService");

foreach (OperationDescription desc in factory.Endpoint.Contract.Operations)
{
    DataContractSerializerOperationBehavior dcsOperationBehavior = desc.Behaviors.Find<DataContractSerializerOperationBehavior>();
    if (dcsOperationBehavior != null)
    {
        int idx = desc.Behaviors.IndexOf(dcsOperationBehavior);
        desc.Behaviors.Remove(dcsOperationBehavior);
        desc.Behaviors.Insert(idx, new NetDataContractSerializerOperationBehavior(desc));
        //return true;

    }       
}


IPersonService svc = factory.CreateChannel();

在服务器端:

myServiceHost = new ServiceHost(typeof(PersonService), baseAddress);


foreach (ServiceEndpoint endPoint in myServiceHost.Description.Endpoints)
{
    foreach (OperationDescription desc in endPoint.Contract.Operations)
    {
        DataContractSerializerOperationBehavior dcsOperationBehavior = desc.Behaviors.Find<DataContractSerializerOperationBehavior>();
        if (dcsOperationBehavior != null)
        {
            int idx = desc.Behaviors.IndexOf(dcsOperationBehavior);
            desc.Behaviors.Remove(dcsOperationBehavior);
            desc.Behaviors.Insert(idx, new NetDataContractSerializerOperationBehavior(desc));
            //return true;
        }
    }
}

myServiceHost.Open();

问题是我正在使用 VS 生成的客户端服务代理和标准 VS .svc 服务。因此,我不控制客户端代理或服务的创建,并且上面的代码假设了这一点。

帖子中的评论之一提到您将能够实现“IServiceBehavior,另一个是 IClientBehavior”,但我不知道如何获取上述代码并更改它。有人有什么想法吗???

干杯 安东尼

I have a scenario where I need to use the NetDataContractSerializer instead of the DataContractSerializer.

Up until now I have been using this method - http://www.pluralsight.com/community/blogs/aaron/archive/2006/04/21/22284.aspx - which appears quite simple but according to this http://social.msdn.microsoft.com/forums/en-US/wcf/thread/cb0c56c0-3016-4cda-a3c7-8826f8cc5bb0/ the approach is incorrect.

Looking around a bit more I found the following - http://social.msdn.microsoft.com/forums/en-US/wcf/thread/f30ecd17-cac0-4cdc-8142-90b5f411936b/

Basically you need to run the following:

So this is what I have on the client side:

ChannelFactory<IPersonService> factory = new ChannelFactory<IPersonService>("WSHttpBinding_IPersonService");

foreach (OperationDescription desc in factory.Endpoint.Contract.Operations)
{
    DataContractSerializerOperationBehavior dcsOperationBehavior = desc.Behaviors.Find<DataContractSerializerOperationBehavior>();
    if (dcsOperationBehavior != null)
    {
        int idx = desc.Behaviors.IndexOf(dcsOperationBehavior);
        desc.Behaviors.Remove(dcsOperationBehavior);
        desc.Behaviors.Insert(idx, new NetDataContractSerializerOperationBehavior(desc));
        //return true;

    }       
}


IPersonService svc = factory.CreateChannel();

And on the server side:

myServiceHost = new ServiceHost(typeof(PersonService), baseAddress);


foreach (ServiceEndpoint endPoint in myServiceHost.Description.Endpoints)
{
    foreach (OperationDescription desc in endPoint.Contract.Operations)
    {
        DataContractSerializerOperationBehavior dcsOperationBehavior = desc.Behaviors.Find<DataContractSerializerOperationBehavior>();
        if (dcsOperationBehavior != null)
        {
            int idx = desc.Behaviors.IndexOf(dcsOperationBehavior);
            desc.Behaviors.Remove(dcsOperationBehavior);
            desc.Behaviors.Insert(idx, new NetDataContractSerializerOperationBehavior(desc));
            //return true;
        }
    }
}

myServiceHost.Open();

The problem is that I am using the VS generated client service proxies and the standard VS .svc services. So I'm not controlling the creation of the client proxy or the service and the above code assumes that.

One of the comments in the post mentions that you would be able to implement "IServiceBehavior and another one is IClientBehavior" but I have no idea how to go about taking the above code and changing it. Does anyone have any ideas???

Cheers
Anthony

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文