Verwenden von benutzerdefinierten FaultContract Objekt enthält System.Exception Ursachen ‚Add Service Reference‘ zum Scheitern verurteilt

StackOverflow https://stackoverflow.com/questions/4656257

Frage

Ich habe gerade bemerkt, etwas Besonderes. Ich habe einen internes Lager Dienst, der durch Basichttpbinding veröffentlicht wird, und ein custom (http + binär), für die Metadaten aktiviert sind. Ich habe auch einen mex Endpunkt für http. Wir verwenden Visual Studio 2008 & VB.NET

Erst kürzlich bemerkten wir, dass wir nicht in der Lage waren erfolgreich zu diesem Dienst in unseren anderen Projekten einen Dienstverweis hinzuzufügen. Alles, was es die erste benutzerdefinierte Ausnahme erzeugen würde, war wir durch eine FaultContract enthalten (eigentlich war es nur 1-Typ). wenn ich eine einfache Web-Referenz hinzufügen würde wäre es richtig, wie gut funktionieren. Außerdem hatte die WcfClient.exe keine Probleme entweder in die Dienste geladen. Nur VS.NET Add Dienstverweis nicht funktionieren würde.

Im Dienst dieser Ausnahme erbt von Ausnahme und wird als serialisierbar markiert. Das ist alles, was Sie angeblich sind zu tun, nicht wahr?

Wie auch immer, hatte ich das verwirrte. Wenn ich die FaultContract für diese benutzerdefinierte Ausnahme funktioniert alles einwandfrei entfernen. Ich kann einen Dienstverweis, kein Problem hinzuzufügen. Aber ist es eine Möglichkeit, ich kann immer noch meine benutzerdefinierten Ausnahmen hat? Ist das ein bekanntes Problem?

War es hilfreich?

Lösung

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.

Andere Tipps

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.

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.

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 :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top