C# 未处理的异常会导致 WCF 服务崩溃吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1136048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unhandled exception will make WCF service crash?
提问by George2
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.
我想知道未处理的异常是否会导致 WCF 服务崩溃。我编写了以下程序,该程序显示 WCF 服务启动的线程中未处理的异常将使整个 WCF 服务崩溃。
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.
我的问题是,我想确认线程中未处理的异常(由 WCF 服务启动)是否会导致 WCF 崩溃?我的困惑是我认为 WCF 应该是稳定的服务,不应因未处理的异常而崩溃。
I am using VSTS 2008 + C# + .Net 3.5 to develop a self-hosted Windows Service based WCF service.
我正在使用 VSTS 2008 + C# + .Net 3.5 来开发基于自托管 Windows 服务的 WCF 服务。
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");
}
}
}
采纳答案by Fredrik M?rk
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();
}
回答by jussij
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/catchto handle the exceptions so that your service is not killed ?
你为什么不添加一个try/catch来处理异常,这样你的服务就不会被杀死?
回答by AutomatedTester
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
在您的代码中阻止以确保如果它确实抛出异常是优雅地处理它。http://msdn.microsoft.com/en-us/library/fk6t46tz(VS.71).aspx 上的示例
回答by marc_s
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 IErrorHandlerinterface, which you should implement on your service implementation class, to turn all unhandled .NET exceptions into either SOAP faults (which will NOTcause the channel to fault), or to report / swallow them entirely.
最好的办法是尽可能在服务器端处理所有错误。查看您应该在服务实现类上实现的IErrorHandler接口,将所有未处理的 .NET 异常转换为 SOAP 错误(不会导致通道出错),或者完全报告/吞下它们。
Marc
马克
回答by marc_s
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.
WCF 运行时的默认行为是吞下除少数类型异常之外的所有异常。因此,如果您的代码在堆栈中向 WCF 运行时抛出异常(例如,如果您从 WCF 操作中抛出),它不会使应用程序崩溃(除非它被视为“致命”异常,例如 OOM、SEHException 等.) 如果异常不是操作故障契约的一部分,则通道将出现故障,否则不会。
If the WCF runtime is not under your code on the stack, then the exception /will/ crash the process.
如果 WCF 运行时不在堆栈上的代码下,则异常 / 将 / 使进程崩溃。
This is similar to the ASP.NET runtime.
这类似于 ASP.NET 运行时。
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.
如果您想以一般方式筛选从 WCF 操作飞出的异常,我建议使用 IOperationInvoker 接口。您也可以使用 IErrorHandler,但您的 IErrorHandler 实现将收到除从“用户代码”(WCF 操作)抛出的异常之外的异常的通知,例如 WCF 内部 I/O 线程上的 SocketAbortedExceptions,您可能不感兴趣。