unhandled exception will make WCF service crash?

為{幸葍}努か 提交于 2019-12-03 07:29:59

Yes, an unhandled exception in a thread will take the process down.

This process will crash:

static void Main(string[] args)
{
    Thread t = new Thread(() =>
    {
        throw new NullReferenceException();
    });
    t.Start();
    Console.ReadKey();
}

This one will not:

static void Main(string[] args)
{
    Thread t = new Thread(() =>
    {
        try
        {
            throw new NullReferenceException();
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.ToString());
        }
    });
    t.Start();
    Console.ReadKey();
}

An unhandled exception on the service side will cause the channel (the connection between the client and the server) to "fault" - e.g. to be torn down.

From that point on, you cannot call from the client using the same proxy client object instance anymore - you'll have to re-create the proxy client.

Your best bet is to handle all error on the server side whenever possible. Check out the IErrorHandler interface, which you should implement on your service implementation class, to turn all unhandled .NET exceptions into either SOAP faults (which will NOT cause the channel to fault), or to report / swallow them entirely.

Marc

The default behavior of the WCF runtime is to swallow all but a few types exceptions. So if your code throws an exception down the stack to the WCF runtime (such as if you throw from a WCF operation), it will NOT crash the app (unless it is deemed a "fatal" exception, such as OOM, SEHException, etc.). If the exception is not part of the operation's fault contract, then the channel will be faulted, otherwise not.

If the WCF runtime is not under your code on the stack, then the exception /will/ crash the process.

This is similar to the ASP.NET runtime.

If you would like to screen for exceptions flying out of WCF operations in a general way, I recommend using the IOperationInvoker interface. You can also use IErrorHandler, but your IErrorHandler implementation will be notified of exceptions other than those thrown from "user code" (WCF operations), such as SocketAbortedExceptions on WCF internal I/O threads, which are probably not interesting to you.

If you don't handle an exception it gets passed on the operating system and it will respond by killing what ever application caused the exception.

Why don't you just add a try/catch to handle the exceptions so that your service is not killed ?

If you don't have proper error handling it will make the program crash. Its good practise to put a

try{//do something
}
catch{ //handle errors
}
finally{//final clean up
}

block in your code to make sure that if it does throw an exception is to handle it gracefully. examples at http://msdn.microsoft.com/en-us/library/fk6t46tz(VS.71).aspx

Juan Pablo 1982

You can make use of FaultException to communicate errors to the client side and keep the logic in the service.

Check this example, hope it helps you.

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