使用包含 System.Exception 的自定义 FaultContract 对象会导致 'Add Service Reference'失败 [英] Using custom FaultContract object containing System.Exception causes 'Add Service Reference' to fail

查看:30
本文介绍了使用包含 System.Exception 的自定义 FaultContract 对象会导致 'Add Service Reference'失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注意到一些特别的事情.我有一个通过 basicHttpBinding 发布的内部库存服务,以及一个启用元数据的 customBinding(http+binary).我还为 http 包含了一个 mex 端点.我们使用 Visual Studio 2008 &VB.NET

I just noticed something particular. I have an internal stock service which is published through basicHttpBinding, and a customBinding (http+binary) for which metadata is enabled. I also included a mex endpoint for http. We use Visual Studio 2008 & VB.NET

就在最近,我们注意到我们无法在其他项目中成功添加对此服务的服务引用.它会生成的只是我们通过 FaultContract 包含的第一个自定义异常(实际上,只有一种类型).如果我添加一个简单的网络参考,它也能正常工作.此外,WcfClient.exe 在加载服务方面也没有问题.只是 VS.NET 添加服务引用是行不通的.

Just recently we noticed that we were unable to succesfully add a service reference to this service in our other projects. All that it would generate was the first custom exception we included through a FaultContract (actually, there was only 1 type). if I'd add a simple web reference it would work correctly as well. Also, the WcfClient.exe had no problems either in loading the services. Just VS.NET add service reference wouldn't work.

在服务中,此异常继承自 Exception 并标记为可序列化.这就是你应该做的,不是吗?

In the service this exception inherits from Exception and is marked as serializable. That's all you're supposed to do, no?

无论如何,这让我感到困惑.如果我删除此自定义异常的 FaultContract 一切正常.我可以添加服务引用,没问题.但是有没有办法我仍然可以拥有我的自定义例外?这是一个已知问题吗?

Anyway, this had me baffled. If I remove the FaultContract for this custom exception everything works fine. I can add a service reference, no problem. But is there a way I can still have my custom exceptions? Is this a known problem?

推荐答案

我今天自己也遇到了这个问题.解决方案是使用一个不是从 FaultContract 中的 Exception 继承的对象.如果您查看 FaultExceptionFaultContract 你会看到官方的例子使用了普通的类(带有 DataContact 属性),而不是为 FaultException.Detail 扩展 Exception 的类.我不确定为什么 Exception 会导致 Add Service Reference 失败,但我怀疑它与序列化或检索自定义异常的类型信息有关.我在示例实现之前和之后都包含了示例实现来演示工作方法.

I ran into this myself today. The solution was to use an object not inheriting from Exception in the FaultContract. If you look at the MSDN docs for FaultException and FaultContract you will see that the official examples use plain classes (with DataContact attributes) rather than classes extending Exception for FaultException.Detail. I'm not sure why Exception causes the Add Service Reference to fail, but I suspect it has to do with the serializing or retrieving the type information for custom exceptions. I have included before and after example implementations to demonstrate the working approach.

之前(无效):

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [FaultContract(typeof(MyException))]
    MyResults MyServiceOperation(string myParameter);
}

[Serializable]
public class MyException : Exception
{
    public string CustomData { get; set; }
}

[ErrorHandlerBehavior(typeof(MyErrorHandler))]
public class MyService : IMyService
{
    public MyResults MyServiceOperation(string myParameter)
    {
        ...
        throw new MyModelException { CustomData = "42" };
        ...
    }
}

public class MyErrorHandler : IErrorHandler
{
    public bool HandleError(Exception error) { return false; }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        try { throw error; }
        catch (MyModelException ex)
        {
            var faultEx = new FaultException<MyException>(new MyException { CustomData = ex.CustomData });
            fault = Message.CreateMessage(version, faultEx.CreateMessageFault(), faultEx.Action);
        }
        catch { /* Supress all others */ }
    }
}

之后(工作):

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [FaultContract(typeof(MyFault))]
    MyResults MyServiceOperation(string myParameter);
}

[DataContract]
public class MyFault
{
    [DataMember]
    public string CustomData { get; set; }
}

[ErrorHandlerBehavior(typeof(MyErrorHandler))]
public class MyService : IMyService
{
    public MyResults MyServiceOperation(string myParameter)
    {
        ...
        throw new MyModelException { CustomData = "42" };
        ...
    }
}

public class MyErrorHandler : IErrorHandler
{
    public bool HandleError(Exception error) { return false; }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        try { throw error; }
        catch (MyModelException ex)
        {
            var faultEx = new FaultException<MyFault>(new MyFault { CustomData = ex.CustomData });
            fault = Message.CreateMessage(version, faultEx.CreateMessageFault(), faultEx.Action);
        }
        catch { /* Supress all others */ }
    }
}

来源:Max Strini 使用他的代码并帮助找到此问题的解决方案.

Source: Max Strini for the use of his code and help in finding the solution to this issue.

这篇关于使用包含 System.Exception 的自定义 FaultContract 对象会导致 &amp;#39;Add Service Reference&amp;#39;失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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