Using custom FaultContract object containing System.Exception causes 'Add Service Reference' to fail

◇◆丶佛笑我妖孽 提交于 2019-12-10 20:00:32

问题


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

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.

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

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?


回答1:


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.

Before (didn't work):

[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 */ }
    }
}

After (worked):

[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 */ }
    }
}

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




回答2:


I found the following article on how to create a fault contract using objected inherited from System.Exception: http://blog.clauskonrad.net/2008/06/wcf-and-custom-exceptions.html

However, it didn't work for me. I suspect the reason it didn't work for me is that I'm using a BasicHttp binding and not a .NET-.NET binding.




回答3:


I've also hit this issue. I ended up using the svcutil.exe to generate the proxy which doesn't appear to suffer the same issue.




回答4:


I had the same problem and solved it by generating the proxy using SVCUTIL.exe. I had the custom fault setup exactly the way MSDN recommends but "add service reference" was not including the fault contract in the proxy. Then I used the SVCUTIL and it worked like magic :)



来源:https://stackoverflow.com/questions/4656257/using-custom-faultcontract-object-containing-system-exception-causes-add-servic

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