C # general error handling

I am writing a web service, which is a wrapper for a provider web service, and has a fairly detailed series of catch statements for calling provider web service methods. I have two or three types of exceptions that I handle (System.Web.Services.Protocols.SoapException, System.ApplicationException, System.Exception ...)

I just realized that most errors are the same between the two Create methods and their Update method.

Is there any smart way to split the same error handlers into multiple methods? I started writing only the usual method, but then I realized that I would have to write at least one common method for each type of exception that I am handling. It would be great if I could handle them the exact same way.

This is a web service with an installed interface. Just thinking out loud when I write this, I think I could put as little code as possible into web methods, and then they could name a common method? I just want to make sure that I'm not missing an obvious trick.

Thanks Neil

+3
source share
3 answers

You can create a function that accepts a delegate and then call it using a lambda expression (C # 3) or an anonymous method. A function can call passed in deletes in a try block and handle exceptions.

private T CallWebService<T>(Func<T> function)
{
    try
    {
        return function();
    }
    catch (SoapException e)
    {
        // handle SoapException
    }
    catch (ApplicationException e)
    {
        // handle ApplicationException
    }
    // catch and handle other exceptions    
}

public ReturnType CallCreate(ParamType param)
{
    return CallWebService(() => WebService.InvokeCreate(param));
}

public ReturnType CallUpdate(ParamType param)
{
    return CallWebService(() => WebService.InvokeUpdate(param));
}

If individual methods need their own processing, then this can be added to CallCreateand methods CallUpdate.

-. CallCreate :

public ReturnType CallCreate(ParamType param)
{
    return CallWebService<ReturnType>(delegate()
    { 
        return WebService.InvokeCreate(param) 
    });
}
+10

Phil Ross, , , IWebMethodInvoker, Invoke():

interface IWebMethodInvoker
{
    void Invoke();
}

-:

class CreateInvoker : IWebMethodInvoker
{
   public SomeDataType Data {get; set;}
   public SomeOtherType Results {get; set;}

   public void Invoke()
   {
       Results = YourWebServiceMethod(Data);
   }
}

, , :

public void ExecuteWebServiceCall(IWebMethodInvoker invoker)
{
    try
    {
        invoker.Invoke();
    }
    catch (ExceptionType1 e)
    {
        // Handle Exception Type 1
    }
    catch (ExceptionType2 e)
    {
        // Handle Exception Type 2
    } // etc
}

-:

var createInvoker = new CreateInvoker() { Data = someStuff };
ExecuteWebServiceCall(createInvoker);
var results = createInvoker.Results;

, , , , .

+1

- . .. - , SoapException ApplicationException. .

If you still think that there is a value, I would suggest using the ExceptionHandling application block from the Enterprise Library . Using EnterpriseLibrary in this scenario is cleaner and better in design than overlaying code on many catch blocks.

+1
source

Source: https://habr.com/ru/post/1725102/


All Articles