2012-11-19 35 views
3

你好我試圖讓Ierrorhandler爲小時現在的工作和即時通訊相當堅持:) 我已經得到了最結果​​本指南不能得到全球的ErrorHandler在我selfhosting WCF服務工作

http://www.remondo.net/wcf-global-exception-handling-attribute-and-ierrorhandler/#comment-10385

,但我不能把它與我的opertations /功能

工作的Program.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 

namespace Phpwcfconsole 
{ 
    class program 
    { 
     static void Main(string[] args) 
     { 
      using (ServiceHost host = new ServiceHost(typeof(Service1))) 
      { 
       try 
       { 
        host.Open(); 
        Console.WriteLine("Host open. Press any key to <EXIT>"); 
        Console.ReadLine(); 
        host.Close(); 
       } 
       catch (Exception e) 
       { 
        Console.WriteLine(Environment.NewLine + e.Message); 
        host.Close(); 
       } 
      } 
     } 
    } 
} 

的app.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<system.web> 
    <compilation debug="true" /> 
</system.web> 

<system.serviceModel> 
    <services> 
    <service behaviorConfiguration="Phpwcfconsole.Service1Behavior" 
     name="Phpwcfconsole.Service1"> 
    <endpoint 
     address="" 
     binding="basicHttpBinding" 
     contract="Phpwcfconsole.IService"> 
     <identity> 
     <dns value="localhost" /> 
     </identity> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://agent007:8732/phpwcf/" /> 
     </baseAddresses> 
    </host> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
     <behavior name="Phpwcfconsole.Service1Behavior"> 
      <serviceMetadata httpGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="MyServiceBinding" 
       hostNameComparisonMode="StrongWildcard" 
       receiveTimeout="00:10:00" 
       sendTimeout="00:10:00" 
       openTimeout="00:10:00" 
       closeTimeout="00:10:00" 
       maxReceivedMessageSize="20000000" 
       maxBufferSize="20000000" 
       maxBufferPoolSize="20000000" 
       transferMode="Buffered" 
       messageEncoding="Text" 
       textEncoding="utf-8" 
       bypassProxyOnLocal="false" 
       useDefaultWebProxy="true" > 
      <security mode="None" /> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    </system.serviceModel> 
</configuration> 

IService.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 


namespace Phpwcfconsole 
{ 
    [ServiceContract] 
    public interface IService 
    { 
     [OperationContract] 
     string GetData(int value); 
    } 
} 

Serverfunctions.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace Phpwcfconsole 
{ 
    public partial class Service1 : IService 
    { 
     public string GetData(int value) 
     { 
      throw new Exception("error"); 
     } 
    } 
} 

ExeceptionHandler.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.ServiceModel.Channels; 
using System.ServiceModel.Dispatcher; 
using System.ServiceModel.Description; 
using System.Collections.ObjectModel; 
using System.ServiceModel.Configuration; 

namespace Phpwcfconsole 
{ 
    public class GlobalExceptionHandler : IErrorHandler 
    { 
     public bool HandleError(Exception ex) 
     { 
      return true; 
     } 

     public void ProvideFault(Exception ex, MessageVersion version, 
          ref Message msg) 
     { 
      // Do some logging here 

      var newEx = new FaultException(
       string.Format("CALLED FROM YOUR GLOBAL EXCEPTION HANDLER BY {0}", 
           ex.TargetSite.Name)); 

      MessageFault msgFault = newEx.CreateMessageFault(); 
      msg = Message.CreateMessage(version, msgFault, newEx.Action); 
     } 
    } 

    public class GlobalExceptionHandlerBehaviourAttribute : Attribute, IServiceBehavior 
    { 
     private readonly Type _errorHandlerType; 

     public GlobalExceptionHandlerBehaviourAttribute(Type errorHandlerType) 
     { 
      _errorHandlerType = errorHandlerType; 
     } 

     public void Validate(ServiceDescription description, ServiceHostBase  serviceHostBase) 
     { 

     } 

     public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, 
      Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters) 
     { 
     } 

     public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase) 
     { 
      var handler = (IErrorHandler)Activator.CreateInstance(_errorHandlerType); 

      foreach (ChannelDispatcherBase dispatcherBase in serviceHostBase.ChannelDispatchers) 
      { 
       var channelDispatcher = dispatcherBase as ChannelDispatcher; 
       if (channelDispatcher != null) 
       channelDispatcher.ErrorHandlers.Add(handler); 
      } 
     } 
    } 
} 

確定,所以,如果我把一些console.writeLine內

公共類GlobalExceptionHandlerBehaviourAttribute:屬性,IServiceBehavior接口 功能我看到他們在運行時我啓動程序。但我不能得到與我的函數指導工作的例子拋出一個異常,並得到一個由我的IErrorhandler捕獲的。

我已經嘗試了其他函數內的一些異常,並沒有發生與我的IErrorhandler。

但一個例外抓到它,我迄今發現它時,我加入我的服務Wcftestclient,然後停止調試,並在我的IService.cs刪除[OperationContract的],然後重新開始,並嘗試運行該功能沒有刷新,該異常被IErrorhandler捕獲

所以我的問題是爲什麼我不能捕捉函數內的異常? 非常感謝你的答案。 C(:

+0

嗯看這個網站[鏈接] http://msdn.microsoft .com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx非常小心,我在t他要求我安裝了windows 4.0。現在運行Windows更新拇指交叉。 – Dendei

+0

有人知道這件事嗎?你使用哪些平臺進行wcf服務? – Dendei

回答