Http://lwbpeter.blog.163.com/blog/static/38508211201007105437493/
A three-tier architecture + WCF + nhib.pdf c/s project was developed in the recent period. It has always been a headache to handle error capture in the project,
Today I finally found an acceptable solution.
First, capture all the errors of unprocessed threads in the foreground for unified processing.
For details, see solution for unhandled exceptions in winform.
Second, it is the error handling in the WCF Service background. Before the error contract returns the error to the foreground, it records the error information to the error log.
See:
Step-by-Step WCF distributed development: Win (15): faultcontract and exceptionhandle)
Finally, a simple and flexible error handling method is required. You do not need to write a try-Catch Block, but can also process errors in a unified manner.
As expected, the function is to use the try-Catch Block to wrap the method to be executed. I searched for it on the internet all over the day, and I went to check out C # AOP,
Not ideal,
After thinking for a long time, suddenly there was a glimmer of aura, so there was the following code:
The principle is the C # delegation mechanism.
/// <Summary>
/// Method to be executed
/// </Summary>
Public Delegate void methodtodo ();
/// <Summary>
/// Method for handling errors
/// </Summary>
/// <Param name = "E"> error object </param>
Public Delegate void methoddealexception (exception E );
/// <Summary>
/// Enclose the method with a try-Catch Block
/// </Summary>
Public class trycatch
{
Private event methodtodo;
Private event methoddealexception;
/// <Summary>
/// Enclose the method with a try-Catch Block
/// </Summary>
/// <Param name = "methodtodo"> method to be executed </param>
Public trycatch (methodtodo)
{
This. methodtodo + = methodtodo;
Do ();
}
/// <Summary>
/// Enclose the method with a try-Catch Block
/// </Summary>
/// <Param name = "methodtodo"> method to be executed </param>
/// <Param name = "methoddealexception"> method of error handling </param>
Public trycatch (methodtodo, methoddealexception)
{
This. methodtodo + = methodtodo;
This. methoddealexception + = methoddealexception;
Do ();
}
Public Void Do ()
{
If (this. methodtodo! = NULL)
{
// Obtain the delegate list
Delegate [] arr = This. methodtodo. getinvocationlist ();
Foreach (delegate D in ARR)
{
Try
{
Methodtodo method = (methodtodo) D;
// Synchronous execution Method
Method. Invoke ();
}
Catch (exception ex)
{
If (this. methoddealexception! = NULL) // execute the custom error handling method
{
Delegate [] err = This. methodtodo. getinvocationlist ();
Foreach (Delegate De in ERR)
{
Methoddealexception me = (methoddealexception) de;
Me. Invoke (Ex );
}
}
Else // executes a unified Error Handling Method
{
Throw ex;
}
}
}
}
}
}
The preceding error handling class can be called as follows:
First declare the method to be executed, such:
Private void CAL ()
{
Int I = calint ("");
}
Private int calint (string P)
{
Return Int. parse (P );
}
Then you can call the processing class for execution:
New trycatch (CAL );