1

Windowsサービスとして実行されているセルフホストのWCFサービスがあります。テストでは、コンソールアプリケーションとして実行しています。

サービスで発生する未処理の例外をすべてキャッチし、ホストをシャットダウンしたいと思います。応答の生成時に発生するすべての非FaultExceptionだけでなく、「アイドルモード」でスローされるすべての例外(つまり、一部のワーカースレッドからスローされる)もキャッチしたいと思います。

残念ながら、IErrorHandler(呼び出されない)でもAppDomain.CurrentDomain.UnhandledException(発生しない)でも例外を処理できません。

手がかりはありますか?

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TestServiceBahavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="TestServiceBahavior" name="WcfErrorHandling.TestService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8001/TestService" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding"
          bindingConfiguration="" contract="WcfErrorHandling.ITestService" />
        <endpoint address="mex" binding="mexHttpBinding"
          contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

コード:

using System;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace WcfErrorHandling
{
    [ServiceContract]
    interface ITestService
    {
        [OperationContract]
        string SayHello(string param);
    }

    public class TestService : ITestService
    {
        public string SayHello(string param)
        {
            if (param == "ae")
                throw new ArgumentException("argument exception");
            if (param == "fe")
                throw new FaultException("fault exception");
            return "hello";
        }
    }

    public class TestHost : ServiceHost, IErrorHandler
    {
        public TestHost()
            : base(typeof(TestService))
        {
        }

        public void Start()
        {
            AppDomain.CurrentDomain.UnhandledException += 
                (sender, ea) => UnhandledExceptionHandler(ea.ExceptionObject as Exception);

            Open();
            foreach (var channelDispatcher in ChannelDispatchers.OfType<ChannelDispatcher>())
                channelDispatcher.ErrorHandlers.Add(this);
        }

        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            // do nothing
        }

        public bool HandleError(Exception error)
        {
            if (!(error is FaultException))
                UnhandledExceptionHandler(error);
            return true;
        }

        private void UnhandledExceptionHandler(Exception ex)
        {
            Close();
            Environment.Exit(1);
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            var testHost = new TestHost();
            testHost.Start();
            Console.Out.WriteLine("Press any key to exit...");
            Console.ReadKey();
            testHost.Close();
        }
    }
}
4

1 に答える 1

0

同じ問題がありましたが、これで解決しました。

サービスにこのインターフェースを実装させます。

public class MyService : System.ServiceModel.Description.IServiceBehavior

次のように実装します。

    public void AddBindingParameters(System.ServiceModel.Description.ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<System.ServiceModel.Description.ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
        return;
    }

    public void ApplyDispatchBehavior(System.ServiceModel.Description.ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher channel in serviceHostBase.ChannelDispatchers) { channel.ErrorHandlers.Add(new ErrorHandler()); }
    }

    public void Validate(System.ServiceModel.Description.ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        return;
    }

そして、このクラスを追加します。

public class ErrorHandler : IErrorHandler
{
   bool IErrorHandler.HandleError(Exception error)
    { return true; }
   void IErrorHandler.ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
    { return;  }
}

次に、HandleErrorにブレークポイントを設定します。例外が表示されます

于 2011-06-09T11:24:56.377 に答える