我有一个这样的代码(它是由 nunit-console 执行的单元测试的一部分):
class MyClass
{
[DI(Type = typeof(MyClass))]
public IMyClass MyClassProperty {get;set;}
}
通过反射,我正在扫描这些类,然后在 Autofac 中注册:
// Register MyClass as IMyClass
autofacBuilder.RegisterType(diAttribute.Type).As(propertyInfo.PropertyType);
之后我需要以同样的方式解决这个属性 - 通过反射:
autofacContainer.Resolve(propertyInfo.PropertyType) // it contains IMyClass
当我在本地启动此代码时,它运行良好。但不适用于 TeamCity。失败并出现错误:
Error: 'Autofac.Core.DependencyResolutionException: An exception was thrown while executing a resolve operation. See the InnerException for details. ---> Common Language Runtime detected an invalid program. (See inner exception for details.) ---> System.InvalidProgramException: Common Language Runtime detected an invalid program.