我有什么似乎是使用简单注入器向MVC控制器注入依赖关系的一个非常基本的问题。之前使用过StructureMap的我使用Simple Injector是新手。使用简单注入器注入MVC控制器构造函数时,未注册参数
MVC的版本是4.5,它是从NuGet安装的最新版本的Simple Injector。
观看的HomeController的/索引视图时,我得到的错误是:
类型的HomeController的构造函数包含未注册名称“语境”型IContext的参数。请确保IContext已在容器中注册,或更改HomeController的构造函数。
控制器是这样:
public class HomeController : Controller
{
public HomeController(IContext context) { }
public ActionResult Index() { }
}
的IContext只是一个简单的界面:
public interface IContext
{
}
和具体IContext实现也很简单,仅仅是一个普通的DbContext的包装。
public class DbContext : System.Data.Entity.DbContext, IContext
{
}
对于信息的IContext接口生活在一个不同的项目VS /装配到的DbContext执行。这些被MVC项目引用。
我在我的Global.asax.cs如下:
protected void Application_Start()
{
var container = new Container();
container.Register<IContext, DbContext>();
container.RegisterMvcControllers(System.Reflection.Assembly.GetExecutingAssembly());
container.RegisterMvcAttributeFilterProvider();
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
// Regular MVC startup
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
这是堆栈跟踪:
[ActivationException: The constructor of the type HomeController contains the parameter of type IContext with name 'context' that is not registered. Please ensure IContext is registered in the container, or change the constructor of HomeController.]
SimpleInjector.Advanced.DefaultConstructorInjectionBehavior.BuildParameterExpression(ParameterInfo parameter) +229
SimpleInjector.Registration.BuildParameterExpressionFor(ParameterInfo parameter) +43
SimpleInjector.Registration.BuildConstructorParameters(ConstructorInfo constructor) +170
SimpleInjector.Registration.BuildNewExpression(Type serviceType, Type implementationType) +62
SimpleInjector.Registration.BuildTransientExpression() +85
SimpleInjector.Registration.BuildExpression(InstanceProducer producer) +62
SimpleInjector.InstanceProducer.BuildExpressionInternal() +42
System.Lazy`1.CreateValue() +14443208
System.Lazy`1.LazyInitValue() +476
SimpleInjector.InstanceProducer.BuildExpression() +159
[ActivationException: The registered delegate for type HomeController threw an exception. The constructor of the type HomeController contains the parameter of type IContext with name 'context' that is not registered. Please ensure IContext is registered in the container, or change the constructor of HomeController.]
SimpleInjector.InstanceProducer.BuildExpression() +257
SimpleInjector.InstanceProducer.VerifyExpressionBuilding() +53
[InvalidOperationException: The configuration is invalid. Creating the instance for type HomeController failed. The registered delegate for type HomeController threw an exception. The constructor of the type HomeController contains the parameter of type IContext with name 'context' that is not registered. Please ensure IContext is registered in the container, or change the constructor of HomeController.]
SimpleInjector.InstanceProducer.VerifyExpressionBuilding() +161
SimpleInjector.Container.VerifyIfAllExpressionsCanBeBuilt(InstanceProducer[] producersToVerify) +45
SimpleInjector.Container.VerifyIfAllExpressionsCanBeBuilt() +166
SimpleInjector.Container.Verify() +39
MyMvcApp.App_Start.SimpleInjectorInitializer.Initialize() +216
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +229
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +193
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35
WebActivator.BaseActivationMethodAttribute.InvokeMethod() +341
WebActivator.ActivationManager.RunActivationMethods() +534
WebActivator.ActivationManager.RunPostStartMethods() +38
WebActivator.StartMethodCallingModule.Init(HttpApplication context) +159
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +530
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +304
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +404
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +475
[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12889028
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12730121
我不知道为什么它不工作的所有代码是根据简单注射器快速入门指南。
感谢您的回复史蒂文。 IContext绝对是同一类型,所有程序集中只有一个定义。正在被注入的所有类型正在抛出错误。我把所有的东西都剥离了,并用我能找到的最简单的案例(在使用IContext的情况下)进行测试。 – Graham
是否有第二个'HomeController'?你可以用'container.Register()'替换'container.RegisterMvcControllers(...)'这行来检查吗? –
Steven
项目中只有一个控制器(整个MVC项目中唯一的注释代码如上所示)。我只是尝试更换该行,但没有任何区别。 :s – Graham