首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在WCF线程中强制未处理的异常使进程崩溃?

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

Stack Overflow用户
提问于 2009-02-10 02:06:41
回答 3查看 2.9K关注 0票数 4

所以这是一个场景:

我有一个WCF服务,每个操作定义了一堆FaultContracts。我希望将其安排为,如果在与有效FaultContract不匹配的WCF服务线程中抛出未处理的异常,它将关闭整个进程,而不仅仅是线程。(原因是我想要一个包含异常信息的崩溃转储,因为它与约定不匹配。)

有没有办法干净利落地做这件事?我遇到的主要问题是,WCF希望将我的所有异常转换为客户端错误,以便保持服务运行;我实际上希望关闭整个过程,这本质上意味着绕过WCF的正常行为。

EN

回答 3

Stack Overflow用户

发布于 2009-02-13 14:00:48

Environment.FailFast()将创建一个崩溃转储;它不会运行任何挂起的try-finally块,也不会运行任何终结器。

票数 3
EN

Stack Overflow用户

发布于 2009-04-23 04:05:29

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

例如(查找Main()中的"serviceHost.Description.Behaviors.Add(new FailBehavior());“行):

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2009-02-10 02:44:14

代码语言:javascript
复制
Application.Exit();

这可能会做到这一点,但用户将丢失他们当时正在处理的任何内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/530852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档