unhandled exception will make WCF service crash?
.net, c#, visual-studio-2008, wcf, windows-services
Solution
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();
}
Problem
I want to know whether unhandled exception will make WCF service crash. I have written the following program which shows unhandled exception in a thread started by WCF service will make the whole WCF service crash. My question is, I want to confirm whether unhandled exception in threads (started by WCF service) will make WCF crash? My confusion is I think WCF should be stable service which should not crash because of unhandled exception. I am using VSTS 2008 + C# + .Net 3.5 to develop a self-hosted Windows Service based WCF service. Here are the related parts of code, ``` namespace Foo { // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config. [ServiceContract] public interface IFoo { [OperationContract] string Submit(string request); } } namespace Foo { // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file. public class FooImpl : IFoo { public string Submit(string request) { return String.Empty; } } } namespace Foo { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } ServiceHost host = new ServiceHost(typeof(FooImpl)); protected override void OnStart(string[] args) { host.Open(); // start a thread which will throw unhandled exception Thread t = new Thread(Workerjob); t.Start(); } protected override void OnStop() { host.Close(); } public static void Workerjob() { Thread.Sleep(5000); throw new Exception("unhandled"); } } } ```