Apologies for the fairly ambiguous title but what I'm trying to achieve is probably better stated in code.
I have a WCF client. When I'm calling methods I would like to wrap each call in some error handling code. So, instead of exposing the methods directly, I've created the following helper function on the client class:
public T HandleServiceCall<T>(Func<IApplicationService, T> serviceMethod)
{
try
{
return serviceMethod(decorator);
}
[...]
}
And the client code uses it like this:
service.HandleServiceCall(channel => channel.Ping("Hello"));
And the call to Ping gets nicely wrapped in some logic that will try to handle any errors.
This works great except that I now have a requirement to know which methods are actually being called on the service. Initially , I was hoping to just inspect the Func<IApplicationService, T>
using Expression trees but didn't get very far.
Finally, I settled on a Decorator pattern:
public T HandleServiceCall<T>(Func<IApplicationService, T> serviceMethod)
{
var decorator = new ServiceCallDecorator(client.ServiceChannel);
try
{
return serviceMethod(decorator);
}
[...]
finally
{
if (decorator.PingWasCalled)
{
Console.Writeline("I know that Ping was called")
}
}
}
And the Decorator itself:
private class ServiceCallDecorator : IApplicationService
{
private readonly IApplicationService service;
public ServiceCallDecorator(IApplicationService service)
{
this.service = service;
this.PingWasCalled = new Nullable<bool>();
}
public bool? PingWasCal开发者_开发技巧led
{
get;
private set;
}
public ServiceResponse<bool> Ping(string message)
{
PingWasCalled = true;
return service.Ping(message);
}
}
It's really clunky and quite a lot of code. Is there a more elegant way of doing this?
You could use an Expression, then inspect the body.
Something like
public T HandleServiceCall<T>(Expression<Func<IApplicationService, T>> serviceMethod)
{
try
{
var func = serviceMethod.Compile();
string body = serviceMethod.Body.ToString();
return func(new ConcreteAppService());
}
catch(Exception ex)
{
...
}
}
Have you considered using aspect oriented approach? It sounds like exactly what you need.
Wrapping exceptions and other 'meta method' functionality could be written as aspects 'orthogonal' to what your serviceMethods do.
Some general information on AOP: AOP in wikipedia
And a potential solution with a container: AOP with Windsor Castle
Here's a quick example using an expression tree:
public T HandleServiceCall<T>(Expression<Func<T>> serviceMethod)
{
try
{
return serviceMethod();
}
finally
{
var serviceMethodInfo = ((MethodCallExpression)serviceMethod.Body).Method;
Console.WriteLine("The '{0}' service method was called", serviceMethodInfo.Name);
}
}
Note that this example assumes that the serviceMethod
expression always contains a method invocation.
Related resources:
- Expression Class
- MethodCallExpression Class
Yes, I believe your code is overcooked.
In terms of Wrapping your code for a common safe disposing of proxies, look here for a nice implementation. Using it is easy:
using (var client = new Proxy().Wrap()) {
client.BaseObject.SomeMethod();
}
Now you also need to access method name - for that simply use Environment.StackTrace
. You need to add walking up the stack in Marc Gravell's Wrap
.
精彩评论