Wcf and ExceptionDetail Issue

885 views Asked by At

I have created my own class like this:

[DataContract]
public class MyOperationFault : ExceptionDetail
{


    /// <summary>
    /// Contructor
    /// </summary>
    /// <param name="ex"></param>
    public MyOperationFault(Exception ex) : base(ex)
    {
    }
}

then my wcf service interface looks like this:

   [OpearationContract()]
   [FaultContract(typeof(MyOperationFault))]
   void DoWork();

Now everything works as expected in the dev environment - when I raise the FaultException:

   throw new FaultException<MyOperationFault>(new MyOperationFault(new Exception("Failed")));

It gets caught on the client's side no problem.

The problem appears when I move to test my service with wcftestclient.exe tool. I am getting this error:

Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata. Error: Cannot obtain Metadata from http://localhost:33620/MyService.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:33620/MyService.svc Metadata contains a reference that cannot be resolved: 'http://localhost:33620/MyService.svc'. Could not connect to http://localhost:33620/MyService.svc. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:33620. Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:33620

As soon as I comment out the [FaultContract(typeof(MyOperationFault))] from the service's methods - the wcftestclient starts working without a glitch. How to tackle this?

1

There are 1 answers

0
nozzleman On

I was facing the same issue. Turns out the classes used as detail parameter need a parameterless constructor in order to work...

The TestClient probably failed because MyOperationFault was lacking a parameterless constructor.

The solution i came up with was adding a private parameterless constructor as in

[DataContract]
public class MyOperationFault : ExceptionDetail
{
    /// i dont know why but this fixed the issue
    private MyOperationFault()
    {
    }

    /// <summary>
    /// Contructor
    /// </summary>
    /// <param name="ex"></param>
    public MyOperationFault(Exception ex) : base(ex)
    {
    }
}