2011-04-26 35 views
3

我有一個WCF服務,我有一個IErrorHandler綁定到它的FaultContract,它工作得很好。出於某種原因,我不知道,它在運行時停止了綁定。當我在ApplyDispatchBehaviorValidate上放置斷點時,當我開始調試(它用於停止)時,它們不會被調用,所以當出現錯誤時我的異常不會被處理。
我最初遵循this blog post來實現它,它運行良好,但是當我添加第二個接口/端點時,它停止工作。我根據this SO post更改了我的所有代碼,但無濟於事。這開始發生的另一件事是,當我打開與Microsoft服務配置編輯器web.config中,我得到的錯誤:
IErrorHandler沒有綁定,也沒有處理錯誤

The 'TestAppServer, Version 1.0.0.0, Culture=neutral, PublicKeyToken=null' assembly could not be found. Do you want to locate it? If you select 'No', you will not be prompted for the same assembly again. To avoid seeing this message every time the system cannot fund the assembly, please copy the assembly file to the same folder as the configuration file. 

上面的錯誤開始發生後,我加入了第二個端點/接口,當我在我的WCF服務上失去了錯誤處理。下面是我的web配置:

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <extensions> 
     <behaviorExtensions> 
     <add name="ServiceErrorHandler" type="company.Test.appserver.implementation.ServiceErrorHandlerBehaviorExtensionElement, TestAppServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
     </behaviorExtensions> 
    </extensions> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="SimpleBinding" /> 
     </basicHttpBinding> 
    </bindings> 
    <services> 
     <service behaviorConfiguration="DefaultBehavior" name="company.Test.appserver.implementation.TestUpdate"> 
     <endpoint address="udt" binding="basicHttpBinding" bindingConfiguration="SimpleBinding" 
      name="TestUpdate" bindingNamespace="http://company/Test/update/2011/04" 
      contract="company.Test.appserver.interfaces.ITestUpdate" /> 
     <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" 
      name="TestUpdateMex" bindingNamespace="http://company/Test/update/2011/04" 
      contract="IMetadataExchange" /> 
     </service> 
     <service behaviorConfiguration="DefaultBehavior" name="company.Test.appserver.implementation.TestTransaction"> 
     <endpoint address="udt" binding="basicHttpBinding" bindingConfiguration="SimpleBinding" 
      name="TestTransacao" bindingNamespace="http://company/Test/transaction/2011/04" 
      contract="company.Test.appserver.interfaces.ITestTransacao" /> 
     <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" 
      name="TestTransacaoMex" bindingNamespace="http://company/Test/transaction/2011/04" 
      contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="DefaultBehavior"> 
      <serviceMetadata httpGetEnabled="true" httpGetBinding="webHttpBinding" 
      httpGetBindingConfiguration="" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="true" httpGetBinding="webHttpBinding" 
      httpGetBindingConfiguration="" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

下面是我的錯誤處理代碼:

namespace company.Test.appserver.implementation 
{ 
    public class ServiceErrorHandlerBehaviorExtensionElement : BehaviorExtensionElement, IServiceBehavior 
    { 
    public override Type BehaviorType 
    { 
     get { return typeof(TestErrorHandler); } 
    } 

    protected override object CreateBehavior() 
    { 
     return new TestErrorHandler(); 
    } 

    private IErrorHandler GetInstance() 
    { 
     return new TestErrorHandler(); 
    } 

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) 
    { 

    } 

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
     IErrorHandler errorHandlerInstance = GetInstance(); 
     foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers) 
     { 
     ChannelDispatcher cd = cdb as ChannelDispatcher; 

     if (cd != null) 
     { 
      cd.ErrorHandlers.Add(errorHandlerInstance); 
     } 
     } 
    } 

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
     foreach (var svcEndpoint in serviceDescription.Endpoints) 
     { 
     if (svcEndpoint.Contract.Name != "IMetadataExchange") 
     { 
      foreach (var opDesc in svcEndpoint.Contract.Operations) 
      { 
      if (opDesc.Faults.Count == 0) 
      { 
       string msg = string.Format(""); 
      } 
      } 
     } 
     } 
    } 
    } 

    public class TestErrorHandler : IErrorHandler 
    { 
    public bool HandleError(Exception error) 
    { 
     return true; 
    } 

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault) 
    { 
     if (!(error is FaultException)) 
     { 
     ClsTestFaultContract exContract = new ClsTestFaultContract(); 

     FaultException<ClsTestFaultContract> fex = 
      new FaultException<ClsTestFaultContract>(exContract, exContract.DescricaoErro, 
      new FaultCode(Convert.ToString(exContract.CodigoErro))); 
     MessageFault msgFault = fex.CreateMessageFault(); 
     fault = Message.CreateMessage(version, msgFault, ErrorHandlerConstant.FaultAction); 
     } 
    } 
    } 
} 

下面是我的接口之一:我已經浪費了無數個小時

namespace company.Test.appserver.interfaces 
{ 
    [ServiceContract(Namespace = "http://company/Test/transaction/2011/04", 
      Name = "ITestTransacao", SessionMode = SessionMode.Allowed)] 
    public interface ITestTransacao 
    { 
    [OperationContract] 
    [FaultContract(typeof(ClsTestFaultContract), Action = ErrorHandlerConstant.FaultAction)] 
    Int32 RegistrarTransacao(String Param1, String Param2, String Param3, Int32 Param4); 
    [OperationContract] 
    [FaultContract(typeof(ClsTestFaultContract), Action = ErrorHandlerConstant.FaultAction)] 
    void ConfirmarTransacao(String Param1, String Param2, Int32 Param3, String Param4, String Param5); 
    [OperationContract] 
    [FaultContract(typeof(ClsTestFaultContract), Action = ErrorHandlerConstant.FaultAction)] 
    void CancelarTransacao(String Param1, String Param2, String Param3, Int32 Param4, String Param5 = ""); 
    } 
} 

,我似乎無法弄清楚發生了什麼事情,打破了我的錯誤處理,這很好。 韓國社交協會這麼多的幫助

編輯

我張貼工作代碼下面,我得到的幫助後。 TKS所以再多... 下面是正確的錯誤處理程序:

namespace company.Test.appserver.implementation 
{ 
    public class ServiceErrorHandlerBehaviorExtensionElement : BehaviorExtensionElement 
    { 
    public override Type BehaviorType 
    { 
     get { return typeof(TestErrorHandler); } 
    } 

    protected override object CreateBehavior() 
    { 
     return new TestErrorHandler(); 
    } 
    } 

    public class TestErrorHandler : ServiceErrorHandlerBehaviorExtensionElement, IErrorHandler, IServiceBehavior 
    { 
    public bool HandleError(Exception error) 
    { 
     return true; 
    } 

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault) 
    { 
     if (!(error is FaultException)) 
     { 
     ClsTestFaultContract exContract = new ClsTestFaultContract(); 

     FaultException<ClsTestFaultContract> fex = 
      new FaultException<ClsTestFaultContract>(exContract, exContract.DescricaoErro, 
      new FaultCode(Convert.ToString(exContract.CodigoErro))); 
     MessageFault msgFault = fex.CreateMessageFault(); 
     fault = Message.CreateMessage(version, msgFault, ErrorHandlerConstant.FaultAction); 
     } 
    } 

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) 
    { 

    } 

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
     foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers) 
     { 
     ChannelDispatcher cd = cdb as ChannelDispatcher; 

     if (cd != null) 
     { 
      cd.ErrorHandlers.Add(this); 
     } 
     } 
    } 

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
     foreach (var svcEndpoint in serviceDescription.Endpoints) 
     { 
     if (svcEndpoint.Contract.Name != "IMetadataExchange") 
     { 
      foreach (var opDesc in svcEndpoint.Contract.Operations) 
      { 
      if (opDesc.Faults.Count == 0) 
      { 
       string msg = string.Format(""); 
      } 
      } 
     } 
     } 
    } 
    } 
} 

及以下正確的web.config

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <extensions> 
     <behaviorExtensions> 
     <add name="ServiceErrorHandler" type="company.Test.appserver.implementation.ServiceErrorHandlerBehaviorExtensionElement, TestAppServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
     </behaviorExtensions> 
    </extensions> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="SimpleBinding" /> 
     </basicHttpBinding> 
    </bindings> 
    <services> 
     <service behaviorConfiguration="DefaultBehavior" name="company.Test.appserver.implementation.TestUpdate"> 
     <endpoint address="udt" binding="basicHttpBinding" bindingConfiguration="SimpleBinding" 
      name="TestUpdate" bindingNamespace="http://company/Test/update/2011/04" 
      contract="company.Test.appserver.interfaces.ITestUpdate" /> 
     <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" 
      name="TestUpdateMex" bindingNamespace="http://company/Test/update/2011/04" 
      contract="IMetadataExchange" /> 
     </service> 
     <service behaviorConfiguration="DefaultBehavior" name="company.Test.appserver.implementation.TestTransaction"> 
     <endpoint address="udt" binding="basicHttpBinding" bindingConfiguration="SimpleBinding" 
      name="TestTransacao" bindingNamespace="http://company/Test/transaction/2011/04" 
      contract="company.Test.appserver.interfaces.ITestTransacao" /> 
     <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" 
      name="TestTransacaoMex" bindingNamespace="http://company/Test/transaction/2011/04" 
      contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="DefaultBehavior"> 
      <serviceMetadata httpGetEnabled="true" httpGetBinding="webHttpBinding" 
      httpGetBindingConfiguration="" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
      <ServiceErrorHandler /> 
     </behavior> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="true" httpGetBinding="webHttpBinding" 
      httpGetBindingConfiguration="" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
      <ServiceErrorHandler /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

回答

4

您的配置文件的ServiceErrorHandler行爲擴展註冊,但它不是實際在< serviceBehaviors/>下的任何集合中使用它。

+0

就是這樣嗎?什麼不見​​了?在配置第二個端點時,我必須將其擦除。 Tks太多了 – Pascal 2011-04-26 12:52:27

+0

我知道了...... Tks太多了。只注意到了缺失的行爲。我要用正確的代碼編輯我的問題。 – Pascal 2011-04-26 15:40:05

+0

感謝您編輯您的問題,這也解決了我的問題! – NibblyPig 2013-06-03 12:59:31

相關問題