Geo*_*ge2 7 .net c# wcf windows-services visual-studio-2008
我想知道未处理的异常是否会导致WCF服务崩溃.我编写了以下程序,该程序显示由WCF服务启动的线程中的未处理异常将使整个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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
mar*_*c_s 18
服务端的未处理异常将导致通道(客户端和服务器之间的连接)"故障" - 例如被拆除.
从那时起,您无法再使用相同的代理客户端对象实例从客户端调用 - 您将不得不重新创建代理客户端.
最好的办法是尽可能处理服务器端的所有错误.查看应在服务实现类上实现的IErrorHandler接口,将所有未处理的.NET异常转换为SOAP错误(不会导致通道出错),或者完全报告/吞下它们.
渣
Fre*_*örk 11
是的,线程中的未处理异常将使该过程失效.
这个过程会崩溃:
static void Main(string[] args)
{
Thread t = new Thread(() =>
{
throw new NullReferenceException();
});
t.Start();
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
这个不会:
static void Main(string[] args)
{
Thread t = new Thread(() =>
{
try
{
throw new NullReferenceException();
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}
});
t.Start();
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
小智 7
WCF运行时的默认行为是吞下除少数类型外的所有异常.因此,如果您的代码将堆栈中的异常抛出到WCF运行时(例如,如果您从WCF操作中抛出),它将不会使应用程序崩溃(除非它被视为"致命"异常,例如OOM,SEHException等) ).如果异常不是操作的错误合同的一部分,则通道将出现故障,否则不会出现故障.
如果WCF运行时不在堆栈上的代码下,那么异常/将使进程崩溃.
这类似于ASP.NET运行时.
如果您希望以一般方式筛选出WCF操作之外的异常,我建议使用IOperationInvoker接口.您也可以使用IErrorHandler,但是您的IErrorHandler实现将被通知除"用户代码"(WCF操作)之外的异常,例如WCF内部I/O线程上的SocketAbortedExceptions,这些可能对您不感兴趣.
| 归档时间: |
|
| 查看次数: |
13278 次 |
| 最近记录: |