Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

c# - Using Simple Injector with Castle Proxy Interceptor

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))
            {
                e.Expression = Expression.Convert(
                    Expression.Invoke(
                        Expression.Constant(createProxy),
                        Expression.Constant(e.RegisteredServiceType, typeof(Type)),
                        e.Expression,
                        c.GetRegistration(typeof(TInterceptor), true).BuildExpression()),
                    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...