2011-05-26 76 views
1

我有一个作为Windows服务运行的自托管的WCF服务。对于测试,我将它作为控制台应用程序运行。如何在未处理的错误时停止自托管的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(); 
     } 
    } 
} 

回答

0

有同样的问题,这解决了这个问题对我来说:

让你的服务实现此接口:

public class MyService : System.ServiceModel.Description.IServiceBehavior 

执行i t等这样的:

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 ..它会告诉你异常

+0

你的配置没有被设置为使用IErrorHandler你写的。 @Flores正在指引你朝着正确的方向前进。看看这个[MSDN文章](http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx),看看如何正确配置它。 – 2011-06-09 12:34:26

+0

我使用这个没有任何配置文件 – Flores 2011-06-09 13:10:35

+0

对不起,应该让我的评论更清晰。 **问题**中显示的配置未配置为添加IErrorHandler行为扩展,这就是为什么他的错误处理程序没有被击中。此答案中的示例服务代码还实现了添加IErrorHandler实现的服务行为,因此不需要配置文件条目。 – 2011-06-09 15:34:04