在Net Core上注册动态代理

时间:2017-10-27 16:41:51

标签: .net-core inversion-of-control ioc-container castle dynamic-proxy

我正在使用Castle的动态代理进入AOP。

我让我的第一个拦截器成为一个选择器,现在正试图在启动类上使用代理生成器:

public class Startup
{
    private static readonly ProxyGenerator _generator = new ProxyGenerator();

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddTransient<IInterface, Implementation>(ctx=>
        {
            var dependency = ctx.GetService<IOtherInterface>();
            var proxyOptions = new ProxyGenerationOptions { Selector = new Selector(dependency)};

            return (Implementation)
                _generator.CreateInterfaceProxyWithTarget(
                typeof(IContactRepository),
                new ContactRepository(options, logger),
                proxyOptions);
        });
    }

    //Removed the rest for this example
}

这是失败的,我知道生成结果的演员应该是IInterface而不是Implementation(这就是现在失败的rith),但是service.AddTransient上的实现工厂的Func需要一个实现。

我应该如何在netcore IOC上设置代理?

谢谢!

0 个答案:

没有答案