C# WCF如何全局处理错误
所以基本上我想集中处理WCF服务中的异常。为此,我创建了一个实现C# WCF如何全局处理错误,c#,.net,wcf,C#,.net,Wcf,所以基本上我想集中处理WCF服务中的异常。为此,我创建了一个实现IErrorHandler的类和一个实现IServiceBehavior的类,在该服务行为实现中,我要说的是,对于每个通道调度器,我希望像这样添加自定义错误处理程序 public void ApplyDispatcherBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { IErrorHandler errorHa
IErrorHandler
的类和一个实现IServiceBehavior
的类,在该服务行为实现中,我要说的是,对于每个通道调度器,我希望像这样添加自定义错误处理程序
public void ApplyDispatcherBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
IErrorHandler errorHandler = new CustomErrorHandler();
foreach (var channelDispatcher in serviceHostBase.ChannelDispatchers)
{
channelDispatcher.ErrorHandlers.Add(errorHandler);
}
}
我所做的最后一件事是创建了一个类,该类通过适当的实现从BehaviorExtensionElement
派生,并在配置文件中注册了扩展名。但它似乎不起作用。我的意思是,我想做的事情是,如果异常未被处理,我想在一个集中的地方处理它。但它似乎对我不起作用。我的实现是否正确,我是否误解了我实现的整个事情应该如何工作。如有任何建议,将不胜感激
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
<bindings>
<basicHttpBinding>
<binding name="soapBinding" />
</basicHttpBinding>
<webHttpBinding>
<binding name="webBinding" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="poxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="defaultBehavior">
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="MyCustomBehaviorExtensionElement" type="MyNamespace.CustomExtensionElement, MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<services>
<service name="MyNamespace.MyService" behaviorConfiguration="defaultBehavior">
<endpoint address="pox" binding="webHttpBinding" behaviorConfiguration="poxBehavior"
name="pox" contract="MyNamespace.IMyService" />
<endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="soapBinding"
name="soap" contract="MyNamespace.IMyService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/Services" />
</baseAddresses>
</host>
</service>
以下是您的web配置中缺少的配置文件部分 如果您尚未应用扩展,则需要在服务行为中添加扩展:
<serviceBehaviors>
<behavior name="defaultBehavior">
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceMetadata httpGetEnabled="true" />
<MyCustomBehaviorExtensionElement />
</behavior>
</serviceBehaviors>
配置文件:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="webHttpWhithErrorHanlding" type="Server.Web.WebHttpWithErrorHandlingElement, Server.Web"/>
</behaviorExtensions>
</extensions>
<!-- other staff -->
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttpWhithErrorHanlding />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
然后将其应用于端点:
<service name="YourServiceContract">
<endpoint address=""
binding="webHttpBinding"
behaviorConfiguration="webBehavior"
contract="IYourServiceContract"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/"/>
</baseAddresses>
</host>
</service>
在您的web配置中丢失 如果您尚未应用扩展,则需要在服务行为中添加扩展:
<serviceBehaviors>
<behavior name="defaultBehavior">
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceMetadata httpGetEnabled="true" />
<MyCustomBehaviorExtensionElement />
</behavior>
</serviceBehaviors>
配置文件:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="webHttpWhithErrorHanlding" type="Server.Web.WebHttpWithErrorHandlingElement, Server.Web"/>
</behaviorExtensions>
</extensions>
<!-- other staff -->
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttpWhithErrorHanlding />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
然后将其应用于端点:
<service name="YourServiceContract">
<endpoint address=""
binding="webHttpBinding"
behaviorConfiguration="webBehavior"
contract="IYourServiceContract"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/"/>
</baseAddresses>
</host>
</service>
在您的web配置中丢失 如果您尚未应用扩展,则需要在服务行为中添加扩展:
<serviceBehaviors>
<behavior name="defaultBehavior">
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceMetadata httpGetEnabled="true" />
<MyCustomBehaviorExtensionElement />
</behavior>
</serviceBehaviors>
配置文件:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="webHttpWhithErrorHanlding" type="Server.Web.WebHttpWithErrorHandlingElement, Server.Web"/>
</behaviorExtensions>
</extensions>
<!-- other staff -->
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttpWhithErrorHanlding />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
然后将其应用于端点:
<service name="YourServiceContract">
<endpoint address=""
binding="webHttpBinding"
behaviorConfiguration="webBehavior"
contract="IYourServiceContract"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/"/>
</baseAddresses>
</host>
</service>
在您的web配置中丢失 如果您尚未应用扩展,则需要在服务行为中添加扩展:
<serviceBehaviors>
<behavior name="defaultBehavior">
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceMetadata httpGetEnabled="true" />
<MyCustomBehaviorExtensionElement />
</behavior>
</serviceBehaviors>
配置文件:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="webHttpWhithErrorHanlding" type="Server.Web.WebHttpWithErrorHandlingElement, Server.Web"/>
</behaviorExtensions>
</extensions>
<!-- other staff -->
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttpWhithErrorHanlding />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
然后将其应用于端点:
<service name="YourServiceContract">
<endpoint address=""
binding="webHttpBinding"
behaviorConfiguration="webBehavior"
contract="IYourServiceContract"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/"/>
</baseAddresses>
</host>
</service>
共享您的确认文件。你们有什么类型的装订?您还可以检查:您尚未应用behaviorExtension,必须将其添加到行为“defaultBehavior”中,作为元素:。但是当您使用WebHttpBehavior时,您需要使用另一种方法,请参见下面的答案。共享您的确认文件。你们有什么类型的装订?您还可以检查:您尚未应用behaviorExtension,必须将其添加到行为“defaultBehavior”中,作为元素:。但是当您使用WebHttpBehavior时,您需要使用另一种方法,请参见下面的答案。共享您的确认文件。你们有什么类型的装订?您还可以检查:您尚未应用behaviorExtension,必须将其添加到行为“defaultBehavior”中,作为元素:。但是当您使用WebHttpBehavior时,您需要使用另一种方法,请参见下面的答案。共享您的确认文件。你们有什么类型的装订?您还可以检查:您尚未应用behaviorExtension,必须将其添加到行为“defaultBehavior”中,作为元素:。但是当您使用WebHttpBehavior时,您需要使用另一种方法,请参见下面的答案。