如何强制使WCF线程中未处理的异常导致进程崩溃?

4
所以这是情景:
我有一个WCF服务,其中每个操作都定义了一堆FaultContracts。我想安排这样的方式:如果在不匹配有效的FaultContract的WCF服务线程中抛出未处理的异常,它将关闭整个进程而不仅仅是线程。(原因是我想要包含异常信息的崩溃转储,因为它没有匹配合同。)
有没有干净的方法来做到这一点?我主要遇到的问题是WCF希望将所有我的异常转换为客户端故障,以保持服务运行;实际上我想关闭整个进程,这基本上意味着绕过WCF的正常行为。
3个回答

3

Environment.FailFast()会创建一个崩溃转储文件;它不会运行任何未完成的try-finally块,也不会运行任何finalizer。


2

在调用(ServiceHost).Open()之前,您需要使用IErrorHandler来自定义WCF的错误处理行为。

例如(在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()
}

0
Application.Exit();

那样做可能可以解决问题,但是用户会失去他们当时正在工作的任何内容。


不幸的是,它还存在无法生成崩溃转储的问题。 - Amy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接