Manually produce System.ServiceModel.Channels.CommunicationObject Faulted event

319 Views Asked by At

I have WCF service that sometimes rise Faulted event on production for yet unknown reasons. I want to reproduce and test such situation in development environment, but I don't understand how to achieve that.

Faulted event belongs to System.ServiceModel.Channels.CommunicationObject and inherit by System.ServiceModel.ServiceHost that I use to host my WCF service.

I there any way to force my ServiceHost produce Faulted event? I tried to manually corrupt client side wcf contract, but it just failed on client side due contract mismatch and service still works properly. Any ideas?

1

There are 1 best solutions below

0
On

The Failed state does not exist. But, if you are talking about a generic way to close the host you can try:

System.ServiceModel.OperationContext.Current.Host.Close();

If a failure is mandatory for you, than the state you're looking for is Faulted. The Host object (whose type is System.ServiceModel.Channels.CommunicationObject) has a method named Fault(), which isn't public, but you'll probably be able to call it with a bit of reflection. That should make the Host transitioning into the Faulted state.

You can also use a host derived from WebServiceHost:

class MyWebServiceHost : System.ServiceModel.Web.WebServiceHost
{
    public MyWebServiceHost(Type serviceType, params Uri[] baseAddresses)
    : base (serviceType, baseAddresses)
    {

    }

    public void Fail()
    {
        base.Fault();
    }
}

source:Force WCF Host into Faulted State