Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3275280
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:07:36+00:00 2026-05-17T19:07:36+00:00

Does anyone have a good example for common error handling for service calls? I

  • 0

Does anyone have a good example for common error handling for service calls? I just started a new project and I see a lot of duplicate code that I don’t like and would like to get rid of. I manage to do so in several other layers but in the proxy layer making the calls to services it is a little bit harder. The code is basically structured as follows:

   ResponseType MyProxyFunc(different, parameters)
   {
      var client = MyServiceClient();
      using (Tracer functionTracer = new Tracer(Constants.TraceLog))
      {
          try
          {
             var response = client.MyRequest(different, parameters);
                if (response.ErrorCode != Constants.OK)
                {
                   ProxyCommon.ThrowError(besvarelseDS.Status[0].ErrorCode);
                }
          }
          finally
          {
             ProxyCommon.CloseWcfClient(client);
          }
          return response;
       }
   }

The code above is just a sample, the ProxyCommon object is a static class with various methods (should probably not be abstract instead of static but that is another discussion). So does anyone have a good suggestion how to abstract this piece of code? I want to have the using, the try/catch and the if-statment in somekind of abstract method, but it’s hard since MyServiceClient differs, the numbers of parameters differs and the request is not the same.

EDIT: A pattern I’ve used before is to use a generic Execute function like public T Execute<T>(Func<T> func). But I can’t get that type of pattern to work in this case.

EDIT #2: I’ve updated the code but I’m not 100 % satisfied, more like 60-75 %. The problem with the code below is that it uses the service objects and they will not be the same for all services, but this will work for service calls against the service in the example that has a wrapped request and response object which it’s ok. But I still don’t think this is the solution to the problem:

public IList<PGSA.Data.Cargo.PGSAReportCargo> GetPGSAReport(DateTime dateFrom, DateTime? dateTo)
{
    var reportService = new ReportServiceClient();
    var request = new GetPGSAReportRequest()
    {
        SystemCode = Settings.SystemID,
        FromDate = dateFrom,
        ToDate = dateTo
    };
    var response = Execute(reportService, reportService.GetPGSAReport, request);
    return response.PGSAReport.ToList();
}

public L Execute<T, K, L>(T client, Func<K, L> serviceFunc, K request) 
    where T : ICommunicationObject
    where K : RequestBase
    where L : ResponseBase
{
    using (Tracer functionTracer = new Tracer(Constants.TraceLog))
    {
        try
        {
            L response = serviceFunc(request);
            if (response.ErrorCode != Constants.OK)
            {
                ProxyCommon.ThrowError(response.ErrorCode);
            }
            return response;
        }
        finally
        {
            ProxyCommon.CloseWcfClient(client);
        }
    }
}

EDIT #3: The ResponseBase and RequestBase in EDIT #2 are base classes defined by the service.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:07:36+00:00Added an answer on May 17, 2026 at 7:07 pm

    Your last approach looks fine to me – I would simplify it slightly as follows:

    public R Execute<T, R>(this T client, Func<R> serviceFunc) 
        where T : ICommunicationObject
        where L : ResponseBase
    {
        using (Tracer functionTracer = new Tracer(Constants.TraceLog))
        {
            try
            {
                R response = serviceFunc();
                if (response.ErrorCode != Constants.OK)
                {
                    ProxyCommon.ThrowError(response.ErrorCode);
                }
                return response;
            }
            finally
            {
                ProxyCommon.CloseWcfClient(client);
            }
        }
    }
    

    And use it

    reportService.Execute(() => reportService.GetPGSAReport(request));
    

    Idea here is to eliminate dependency on not needed request object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a good example of a WPF or silverlight windows gadget?
Does anyone have a good example in Ruby of using a Composite of Commands?
Does anyone have a good example of how to do https over http (or
Does anyone have a good example of creating a WPF custom control or some
Does anyone have a good example of using the HeaderParser class in Python for
I'm using the Silverlight UnitTest framerwork does anyone have a good example have how
DOes anyone have any good information with regards to when to log, I was
Does anyone have a good VBA way (or perhaps there is an MS Word
Does anyone have a good way of deleting duplicate transactions (same date, amount, biller,
Does anyone have any good advice or experience on how to create an engine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.