How to manually create wcf service fault exception?

98 views Asked by At

I want to manually make the wcf service channel to enter to faulted state to test a singleton object. I tried many solutions like changing 1. consuming incorrect service host name, 2. Incorrect host ip, 3. reducing exeution time to trigger timeout exception, 4.divide by zero exception, 5.throwing fault exception, 6.null reference exception

But nothing make the service channel faulted. Kindly advice. Thanks in advance

1

There are 1 answers

0
Aansari On

what type of binding you are using? I think you need to use binding which support session. have you tried using WsHttpBinding? Please have a look at the code sample below and try to pass 0 value for b to generate DivideByZeroException. This will cause the channel to be in fault state

My service contract: `

[ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        int Division(int a, int b);
    }

` My Service Contract implementation:

`

public class TestService : ITestService
{
    public int Division(int a, int b)
    {
        return a / b;
    }   
    
}

`

EndPoint:

`<endpoint address="TestService" binding="wsHttpBinding" 
          contract="ServiceFaultExceptionTest.ITestService">
        </endpoint>

`