指定自定义序列化时,故障的细节不反序列化 [英] Fault detail not deserialized when specifying custom serializer

查看:227
本文介绍了指定自定义序列化时,故障的细节不反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义的 XmlObjectSerializer 在我的应用程序。要做到这一点,我将 XmlSerializerOperationBehavior MyOperationBehavior 这看起来是这样的:

I'm using a custom XmlObjectSerializer in my application. To do so, I replace XmlSerializerOperationBehavior with MyOperationBehavior which looks something like this:

public class MyOperationBehavior : DataContractSerializerOperationBehavior
{
    public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
    {
        return new MySerializer();
    }

    public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
    {
        return new MySerializer();
    }
}

现在的问题是,如果这样做,任何故障都反序列化到非通用的FaultException ,而不是的FaultException&LT; TDetail&GT; ,我无法访问故障的详细信息。

The problem is that by doing so, any faults are deserialized to the non-generic FaultException rather than FaultException<TDetail> and I'm unable to access the fault's details.

做了一些调查,我发现,问题的根源是,由 DataContractSerializerOperationBehavior ,.NET继承内部设置FaultFormatter到之后DataContractSerializerFaultFormatter 不知道如何反序列化故障的详细信息(而非 XmlSerializerFaultFormatter )。这个问题绝对不是在 MySerializer ,因为的FaultException 它到达之前抛出我的的readObject 方法。

After doing some investigating I found that the root of the problem is the fact that by inheriting from DataContractSerializerOperationBehavior, .NET internally sets the FaultFormatter to DataContractSerializerFaultFormatter which doesn't know how to deserialize the fault's detail (rather than XmlSerializerFaultFormatter). The problem is definitely not in MySerializer, since the FaultException is thrown before it gets to my ReadObject method.

所以我的问题是,我该怎么做才能让WCF正确反序列化我的错细节? 我试图寻找一种方法来设置FaultFormatter自己,但没有运气,特别是因为所有这些格式化是内部的。

So my question is what can I do to make WCF deserialize my fault details correctly? I tried looking for a way to set the FaultFormatter myself, but had no luck, especially because all these formatters are internal.

推荐答案

我已经找到一种方法来获得FaultExceptions反序列化正确。这是非常hackish的,虽然我觉得有点不舒服使用它,但我想我会分享的情况下,其他人正在解决这个问题。如果一个更好的答案可我很乐意听到的。

I've found a way to get the FaultExceptions to deserialize properly. It's very hackish though and I feel a bit uncomfortable using it, but I thought I'd share in case anyone else is looking to solve this issue. If a better answer is available I'd be happy to hear.

为了与自己的自定义序列化取代串行什么,我所做的就是更换 XmlSerializerOperationBehavior 用我自己的 MyOperationBehavior (它的工作方式,我到处都看到在网络上)。我发现尽管这不是替换的行为,如果我只需要添加我的新行为的行为清单,该故障将被反序列化预期。有一点要注意的是,虽然 MyOperationBehavior 应放置在行为列表前 XmlSerializerOperationBehavior - 否则,自定义的串行韩元'T使用。

In order to replace the serializer with my own custom serializer what I done is replace the XmlSerializerOperationBehavior with my own MyOperationBehavior (the way it's done everywhere I saw on the web). I found though that if instead of replacing the behavior I just add my new behavior to the list of behaviors, the faults will be deserialized as expected. One thing to note though is that MyOperationBehavior should be placed in the list of behaviors before XmlSerializerOperationBehavior - otherwise the custom serializer won't be used.

description.Behaviors.Insert(0, new MyOperationBehavior());

正如我所说的,有这两种行为一起听起来像它可能会产生问题,我不知道,所以我很高兴获得该输入,以及如果你知道的任何冲突,这种解决方案可能会提高。

As I said, having these two behaviors together sounds like it can cause problems I'm not aware of, so I'd be happy to get input on that as well if you know of any conflicts that this solution may raise.

这篇关于指定自定义序列化时,故障的细节不反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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