我想知道未处理的异常是否会导致WCF服务崩溃。我编写了以下程序,该程序显示由WCF服务启动的线程中的未处理的异常将使整个WCF服务崩溃。
我的问题是,我想确认在WCF服务启动的线程中的未处理异常是否会使WCF崩溃? 我感到困惑的是,我认为WCF应该是一个稳定的服务,不应因未处理的异常而崩溃。
我正在使用VSTS 2008 + C# + .Net 3.5开发基于自托管Windows服务的WCF服务。
以下是相关代码部分:
我的问题是,我想确认在WCF服务启动的线程中的未处理异常是否会使WCF崩溃? 我感到困惑的是,我认为WCF应该是一个稳定的服务,不应因未处理的异常而崩溃。
我正在使用VSTS 2008 + C# + .Net 3.5开发基于自托管Windows服务的WCF服务。
以下是相关代码部分:
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");
}
}
}