I have a web service where I find myself putting the exact same try/catch block in every method and I'm trying to eliminate that code reuse.
Here is a sample method (some code removed) where you can see #1/#2/#3 are the core calls and are very similar.
[HttpGet]
[Route("GetKeys")]
[Produces(MediaTypeNames.Application.Json)] // "application/json"
public async Task<EntityKeyPage> GetKeys([FromQuery] string company = "", [FromQuery] DocumentPaging documentPaging = null)
{
[...]
try
{
// Sometimes it's this #1
VendTableServiceGetKeysResponse getKeysResponse = await aifClient.getKeysAsync(getKeysRequest);
// Sometimes it's this #2
// VendTableServiceReadResponse readResponse = await aifClient.readAsync(readRequest);
// Sometimes it's this #3
// VendTableServiceFindKeysResponse findKeysResponse = await aifClient.findKeysAsync(findKeysRequest);
}
catch (System.ServiceModel.FaultException<AifFault> ex)
{
var message = this.GetAXMessage(ex);
aifClient.Abort();
throw new Exception(message);
}
catch (Exception e)
{
aifClient.Abort();
string errorMessage = e.Message != null ? e.Message : "";
errorMessage = e.InnerException != null ? errorMessage + "\n" + e.InnerException : errorMessage;
throw new Exception(errorMessage);
}
finally
{
aifClient.Close();
}
return getKeysResponse.EntityKeyPage;
}
Here is my attempt at a generic method I can use.
private async Task<Out> MakeAIFCall<In, Out>(Func<In, Out> action, In @in)
{
Out @out;
try
{
@out = action(@in);
}
catch (System.ServiceModel.FaultException<AifFault> ex)
{
var message = this.GetAXMessage(ex);
aifClient.Abort();
throw new Exception(message);
}
catch (Exception e)
{
aifClient.Abort();
string errorMessage = e.Message != null ? e.Message : "";
errorMessage = e.InnerException != null ? errorMessage + "\n" + e.InnerException : errorMessage;
throw new Exception(errorMessage);
}
finally
{
aifClient.Close();
}
return @out;
}
I wrote that method based on my reading, but I can't figure out how to call it? It appears I normally would pass a method name, but in this case it would be an object+method (aifClient.getKeysAsync(...)
/aifClient.readAsync(...)
/aifClient.findKeysAsync(...)
)?
How do I call my method and or am I doing it wrong?
This is my first attempt at calling it, but it's wrong:
var response = await MakeAIFCall<VendTableServiceGetChangedKeysRequest, VendTableServiceGetChangedKeysResponse>(aifClient.getKeysAsync, getKeysRequest);
The variables @out
and @in
were suggested by intellisense...I have no idea why the @
symbol is there or what it does.