StructureMap возвращает динамический прокси - PullRequest
0 голосов
/ 01 марта 2019

Знаете ли вы, как создать перехватчик в StructureMap, который возвращает динамический прокси.Вот что я пытаюсь сделать до сих пор, но это не работает.

public class StructureMapTypeInterceptor : ISyncInterceptionBehavior
{
    private readonly IContainer _container;
    private readonly IAspectConfiguration _aspectConfiguration;
    private readonly ProxyGenerator _proxyGenerator;

    public StructureMapTypeInterceptor(IContainer container, IAspectConfiguration aspectConfiguration)
    {
        _container = container;
        _aspectConfiguration = aspectConfiguration;
        _proxyGenerator = new ProxyGenerator();
    }

    public IMethodInvocationResult Intercept(ISyncMethodInvocation methodInvocation)
    {         
        object target = methodInvocation.TargetInstance;
        Type targetType = target.GetType();

        var instanceRef = _container.Model.AllInstances.FirstOrDefault(e => e.ReturnedType == targetType);

        if (instanceRef != null)
        {
            Type interfaceToProxy = instanceRef.PluginType;

            if (interfaceToProxy.IsInterface)
            {
                object proxy = _proxyGenerator.CreateInterfaceProxyWithTargetInterface(interfaceToProxy, target, new[] { (IInterceptor)new AspectInterceptor(_aspectConfiguration) });

                return methodInvocation.CreateResult(proxy);
            }
        }

        return methodInvocation.CreateResult(target);
    }
}

Вот регистрация

    var aspectTypeInterceptor = new StructureMapTypeInterceptor(ObjectFactory.Container, AspectConfiguration);
    ObjectFactory.Container
        .Configure(c => c.Policies
        .Interceptors(new DynamicProxyInterceptorPolicy(aspectTypeInterceptor)));

У меня она работала в StructureMap 2 следующим образом:

public class StructureMapTypeInterceptor : TypeInterceptor
{
    private readonly IContainer _container;
    private readonly IAspectConfiguration _aspectConfiguration;
    private readonly ProxyGenerator _proxyGenerator;

    public StructureMapTypeInterceptor(IContainer container, IAspectConfiguration aspectConfiguration)
    {
        _container = container;
        _aspectConfiguration = aspectConfiguration;
        _proxyGenerator = new ProxyGenerator();
    }

    public object Process(object target, IContext context)
    {
        Type targetType = target.GetType();

        var instanceRef = _container.Model.AllInstances.FirstOrDefault(e => e.ConcreteType == targetType);

        if (instanceRef != null)
        {
            Type interfaceToProxy = instanceRef.PluginType;
            var proxy = _proxyGenerator.CreateInterfaceProxyWithTargetInterface(interfaceToProxy, target, new[] { (IInterceptor)new AspectInterceptor(_aspectConfiguration) });
            return proxy;
        }

        return target;
    }
}

Но в более новой версии они удалили TypeInterceptor.Знаете ли вы, есть ли альтернатива этому в более новых версиях?

...