所以这是场景:
我有一个WCF服务,每个操作都定义了一堆FaultContracts。我想安排它,如果在与有效FaultContract不匹配的WCF服务线程中抛出未处理的异常,它将取消整个进程而不仅仅是线程。 (原因是我想要一个包含异常信息的崩溃转储,因为它与合同不匹配。)
有没有办法干净利落地做到这一点?我遇到的主要问题是WCF希望将所有异常转换为客户端故障,以保持服务运行;我实际上想要整个过程,这实际上意味着绕过WCF的正常行为。
答案 0 :(得分:3)
Environment.FailFast()将创建崩溃转储;它不会运行任何挂起的try-finally块,也不会运行任何终结器。
答案 1 :(得分:2)
您需要使用IErrorHandler来自定义WCF的错误处理行为。您在调用(ServiceHost).Open()。
之前“应用行为”例如(在Main()中查找“serviceHost.Description.Behaviors.Add(new FailBehavior());”中的行:
class FailBehavior : IServiceBehavior
{
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
return;
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
Console.WriteLine("The FailFast behavior has been applied.");
var f = new FailOnError();
foreach(ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
{
chanDisp.ErrorHandlers.Add(f);
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
return;
}
}
class FailOnError : IErrorHandler
{
public bool HandleError(Exception error)
{
// this is called for every exception -- even ungraceful disconnects
if( !(error is CommunicationException) )
throw new TargetInvocationException( "WCF operation failed.", error );
else
throw new CommunicationException( "Unexpected communication problem. (see inner exception)", error );
// Unreachable
//return false; // other handlers should be called
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
// Can't throw from here (it will be swallowed), and using Environment.FailFast
// will result in all crashes going to the same WER bucket. We could create
// another thread and throw on that, but instead I think throwing from HandleError
// should work.
//Console.WriteLine( "Unhandled exception: {0}", error );
//Environment.FailFast("Unhandled exception thrown -- killing server");
}
}
class Program
{
static void Main( string[] args )
{
Console.WriteLine( "Greetings from the server." );
Uri uri = new Uri( "net.tcp://localhost:5678/ServerThatShouldCrash" );
using( ServiceHost serviceHost = new ServiceHost( typeof( Server ), uri ) )
{
Binding binding = _CreateBinding();
serviceHost.AddServiceEndpoint( typeof( IServer ),
binding,
uri );
serviceHost.Description.Behaviors.Add(new FailBehavior());
serviceHost.Open();
// The service can now be accessed.
Console.WriteLine( "The service is ready." );
Console.WriteLine( "\nPress <ENTER> to terminate service.\n" );
Console.ReadLine();
}
}
private static Binding _CreateBinding()
{
NetTcpBinding netTcp = new NetTcpBinding( SecurityMode.None );
netTcp.ReceiveTimeout = TimeSpan.MaxValue;
netTcp.ReliableSession.InactivityTimeout = TimeSpan.MaxValue;
return netTcp;
} // end _CreateBinding()
}
答案 2 :(得分:0)
Application.Exit();
可能会这样做,但是用户将失去他们当时正在处理的任何事情。