how to do error handing with WCF by using attributes to log your errors z
2015-04-22 16:39
519 查看
![](http://taswar.zeytinsoft.com/wp-content/uploads/2009/02/wcf.png)
There are multiple ways to do error handling in WCF as listed by Pedram Rezaei Blog.
The default way that WCF allows errors
message to display is by setting IncludeExceptionDetailInFaults Property to true in web.config or on the service attribute but this is only recommended when you need to debug you development code not in your shipped/release code.
In Web.config file
In the web.config file of the WCF service the includeExceptionDetailFaults attribute set it to true. With this action every endpoint associated to WCF service will send managed exception information.
<system.serviceModel>
<services>
<service name="ZeytinSoft.WCF.OliveService"
behaviorConfiguration="OliveDebugServiceConfiguration">
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="OliveDebugServiceConfiguration">
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <system.serviceModel> <services> <service name="ZeytinSoft.WCF.OliveService" behaviorConfiguration="OliveDebugServiceConfiguration"> </service> </services> <behaviors> <serviceBehaviors> <behavior name="OliveDebugServiceConfiguration"> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> |
Another way is setting the IncludeExceptionDatailInFaults property to true using the ServiceBehaviorAttribute.
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class OliveService{ }
1 2 | [ServiceBehavior(IncludeExceptionDetailInFaults=true)] public class OliveService{ } |
The IErrorHandler interface
The basic form of error logging in WCF is to use the IErrorHandler interface, which enables developers to customize the default exception reporting and propagation, and provides for a hook for custom logging.
public interface IErrorHandler
{
bool HandleError(Exception error);
void ProvideFault(Exception error,MessageVersion version,ref Message fault);
}
1 2 3 4 5 | public interface IErrorHandler { bool HandleError(Exception error); void ProvideFault(Exception error,MessageVersion version,ref Message fault); } |
public interface IServiceBehavior
{
void AddBindingParameters(ServiceDescription description,
ServiceHostBase host,
Collection <ServiceEndpoint> endpoints,
BindingParameterCollection parameters);
void ApplyDispatchBehavior(ServiceDescription description,
ServiceHostBase host);
void Validate(ServiceDescription description,ServiceHostBase host);
}
1 2 3 4 5 6 7 8 9 10 11 12 | public interface IServiceBehavior { void AddBindingParameters(ServiceDescription description, ServiceHostBase host, Collection <ServiceEndpoint> endpoints, BindingParameterCollection parameters); void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase host); void Validate(ServiceDescription description,ServiceHostBase host); } |
Back to building our own ErrorHandler, ServiceBehavior with Attribute, the code is listed below
[AttributeUsage(AttributeTargets.Class)]
public class OliveErrorHandlerBehaviorAttribute : Attribute, IServiceBehavior, IErrorHandler
{
protected Type ServiceType { get; set; }
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
//Dont do anything
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection <ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
//dont do anything
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
ServiceType = serviceDescription.ServiceType;
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(this);
}
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
fault = null; //Suppress any faults in contract
}
public bool HandleError(Exception error)
{
Logger.LogException(error); //Calls log4net under the cover
return false;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | [AttributeUsage(AttributeTargets.Class)] public class OliveErrorHandlerBehaviorAttribute : Attribute, IServiceBehavior, IErrorHandler { protected Type ServiceType { get; set; } public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { //Dont do anything } public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection <ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { //dont do anything } public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { ServiceType = serviceDescription.ServiceType; foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers) { dispatcher.ErrorHandlers.Add(this); } } public void ProvideFault(Exception error, MessageVersion version, ref Message fault) { fault = null; //Suppress any faults in contract } public bool HandleError(Exception error) { Logger.LogException(error); //Calls log4net under the cover return false; } } |
[ServiceContract]
[OliveErrorHandlerBehavior]
public class OliveService{ }
1 2 3 | [ServiceContract] [OliveErrorHandlerBehavior] public class OliveService{ } |
相关文章推荐
- How to Get Even with Your Annoying Neighbor by Bumping Them Off Their WiFi Network —Undetected
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How to Do Everything with Your Web 2.0 Blog
- How To Implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET
- How to Do Everything with Your Smartphone, Windows Mobile Edition
- How to record log of specified package by using log4j
- 3 ways to do WCF Concurrency Management(Single, Multiple, and Reentrant and How to do with Throttling)
- How to Add/Drop/Resize Redo Log with Physical Standby in place. [ID 473442.1]
- How to Do Everything with Your Web 2.0 Blog (Paperback), Oct.2007.eBook-BBL
- [转贴]How to work with stored procedures by using script in an Office InfoPath 2003 form
- How to Add/Drop/Resize Redo Log with Physical Standby in place. [ID 473442.1]
- linux - How do I write stderr to a file while using "tee" with a pipe?
- How to write to an event log by using Visual C#
- ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supp
- How to populate the datagrid on background thread with data binding by using Visual C#
- How to avoid error "LNK2001 unresolved external" by using DEFINE_GUID
- How to automate PowerPoint by using Visual C++ 5.0 or Visual C++ 6.0 with The Microsoft Foundation Classes
- How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- Console显示:Conversion to Dalvik format failed with error 1 ;your project contains errors) please fix