Reputation: 11088
In my [Webmethod]s I have code like this.
var something = container.ResolveSomething();
something.Run();
All registered components except one have lifestyle defined as PerWebRequest. One is registered as Singleton (logger). For some of components I have defined and configured Interceptor that will log method calls and their results.
My question is: Will I have problems if I register this Interceptor with Lifestyle PerWebRequest? Documentation advices to make all Interceptors Transient and use other lifestyles if we are really sure we want to do it. If I register Interceptors with lifestyle Transient any of my about 100 methods will have to look like this.
IComponent component = null;
try
{
component = container.ResolveComponent();
compoment.Run();
}
finally
{
container.Release(component);
}
So more boilerplate then real code.
Here is my interceptor:
public class LoggingInterceptor : IInterceptor
{
private readonly ILogger logger;
public LoggingInterceptor(ILogger logger)
{
this.logger = logger;
}
public void Intercept(IInvocation invocation)
{
var call = string.Format("{0}.{1}({2})", invocation.TargetType.FullName, invocation.Method.Name, string.Join(", ", invocation.Arguments.Select(arg => arg.ToString()).ToArray()));
try
{
logger.Info(call);
invocation.Proceed();
logger.Info("Result: " + call + " = " + invocation.ReturnValue);
}
catch (Exception e)
{
logger.Error(call, e);
throw;
}
}
}
I know WCF is better prepaired for IoC but I have to stay with ASP.NET WebServices.
Upvotes: 2
Views: 741
Reputation: 5189
My understanding is that even if the Interceptor is transient, it's lifetime is bound with intercepted component. It will be released with intercepted component because it is tracked by container.
Upvotes: 4