在App_Start
期间,我遇到了请求服务实例的问题。以下错误表明该服务未注册。在下面的例子中,它是" AService"被涂黑了。
Bellow是抛出的异常:
以下代码将所有服务注册到容器中。
public override void Bind(ref IContainer container)
{
base.Bind(ref container);
container.Register<IAService, AService>(DependencyScope.PerInstance);
container.Register<IBService, BService>(DependencyScope.PerInstance);
container.Register<ICService, CService>(DependencyScope.PerInstance);
container.Register<IDService, DService>(DependencyScope.PerInstance);
container.Register<IHelperService, HelperService>(DependencyScope.PerInstance);
}
以下代码是手动创建服务及其所有依赖项的测试。
DbContext context = new DbContext("myConnectionString");
DataManager manager = new DataManager(context);
AService service = new AService(manager, new HelperService());
堆栈跟踪
at SimpleInjector.InstanceProducer.VerifyExpressionBuilding()
at SimpleInjector.Container.VerifyThatAllExpressionsCanBeBuilt(InstanceProducer[] producersToVerify)
at SimpleInjector.Container.VerifyThatAllExpressionsCanBeBuilt()
at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)
at SimpleInjector.Container.Verify()