1

我已经设置了下一件事:

一个标记接口、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但没有运气。

我真的对这个感到困惑。

4

0 回答 0