问题
I'm using Simple Injector in my asp.net mvc 4 project.
I can't figure out how can I use Simple Injector with castle proxy interceptor.
回答1:
There's in fact a section about Interception in the Simple Injector documentation that pretty clearly describes how to do interception. The code samples given there don't show how to work with Castle DynamicProxy, but you actually need to change a few lines of code to get this working.
If you use the Interception Extensions code snippet, to get it working, you only need to remove the IInterceptor
and IInvocation
interfaces, add a using Castle.DynamicProxy
on the top of the file, and replace the generic Interceptor
with the following:
public static class Interceptor
{
private static readonly ProxyGenerator generator = new ProxyGenerator();
public static object CreateProxy(Type type, IInterceptor interceptor,
object target)
{
return generator.CreateInterfaceProxyWithTarget(type, target, interceptor);
}
}
But at a minimum, this would be the code you need to get interception working with Castle DynamicProxy:
using System;
using System.Linq.Expressions;
using Castle.DynamicProxy;
using SimpleInjector;
public static class InterceptorExtensions
{
private static readonly ProxyGenerator generator = new ProxyGenerator();
private static readonly Func<Type, object, IInterceptor, object> createProxy =
(p, t, i) => generator.CreateInterfaceProxyWithTarget(p, t, i);
public static void InterceptWith<TInterceptor>(this Container c,
Predicate<Type> predicate)
where TInterceptor : class, IInterceptor
{
c.ExpressionBuilt += (s, e) =>
{
if (predicate(e.RegisteredServiceType))
{
var interceptorExpression =
c.GetRegistration(typeof(TInterceptor), true).BuildExpression();
e.Expression = Expression.Convert(
Expression.Invoke(Expression.Constant(createProxy),
Expression.Constant(e.RegisteredServiceType, typeof(Type)),
e.Expression,
interceptorExpression),
e.RegisteredServiceType);
}
};
}
}
This is how to use this:
container.InterceptWith<MonitoringInterceptor>(
type => type.IsInterface && type.Name.EndsWith("Repository"));
This allows intercepting all interface registrations which name end with 'Repository' to be intercepted with a transient MonitoringInterceptor
.
来源:https://stackoverflow.com/questions/24513530/using-simple-injector-with-castle-proxy-interceptor