使用IIndex<,>的Autofac MVC4。在尝试通过索引器解析时,CI会抛出EntryPointNotFoundException

时间:2013-02-16 23:31:36

标签: asp.net-mvc-4 autofac

我已经设置了下一件事:

标记接口,IFormatter,通用接口ITypedFormatter,基类FormatterBase以及FormatterBase上的一堆实现。

看起来像这样:

public interface IFormatter{}
public interface ITypedFormatter<T> : IFormatter {...}
public abstract class FormatterBase<T> : ITypedFormatter<T> {...}

public class SomeFormatter<SomeClass> : FormatterBase<SomeClass> { ... }

我用autofac注册它们:

builder.RegisterType<SomeFormatter>().Keyed<IFormatter>(typeof(SomeClass));

然后我在另一个类中使用它通过IIndex收集它们并在方法中使用它:

public class SomeService
{
    public SomeService(IIndex<Type, IFormatter> formatterFactory){...}

    public T Format<T>(T entity)
    {
        var formatter = formatterFactory[typeof (T)];
        var typedFormatter = formatter as ITypedFormatter<T>;
        if(typedFormatter == null)
            return default(T);

        return typedFormatter.Format(entity);
    }
}

通过控制器等所有接线都能正常工作。我可以看到所有的注册和所有。但是从我到达方法的第一行的那一刻起,我得到了EntryPointNotFoundException。

最奇怪的是,当在方法的第一行设置断点并通过visual studio的监视窗口设置格式化程序字段并跳过该步骤时,一切正常。

当我尝试在此方法之外解析时,在设置DependencyResolver之后它也可以正常工作:

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
var iindex = DependencyResolver.Current.GetService<IIndex<Type, IFormatter>>();
var test = (ITypedFormatter<SomeClass>) iindex[typeof (SomeClass)];

我在上面和运行的应用程序之间看到的唯一区别是服务是从 System.Web.Script.Serialization.JavaScriptSerializer()的内部执行路径中调用的.Serialize(model)方法。我也试过 NewtonSoft.Json.JsonSerializer ,但没有运气。

我真的很困惑这个。

0 个答案:

没有答案