我正在创建一个试图利用控制器动作注入的ASP.NET MVC 3应用程序,如here所述。如何使用Autofac为MVC 3做动作注入?
控制器构造函数注入没有任何问题,但我似乎无法获得操作注入工作。
我已经设置了Autofac在Global.asax中是这样的:
var builder = new ContainerBuilder();
builder.Register<ITestInject>(c => new TestInject { TestString = "hi" });
builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();
builder.RegisterControllers(typeof(MvcApplication).Assembly).InjectActionInvoker();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
我的控制器有一个这样的动作:
public ActionResult Test(ITestInject testInject)
{
return View(testInject);
}
的TestInject/ITestInject类/接口定义为:
public interface ITestInject
{
string TestString { get; }
}
public class TestInject : ITestInject
{
public string TestString { get; set; }
}
当我尝试导航到测试动作,我看到这个错误:对,我做错了什么在这里
Server Error in '/' Application.
Cannot create an instance of an interface.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: Cannot create an instance of an interface.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[MissingMethodException: Cannot create an instance of an interface.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241 System.Activator.CreateInstance(Type type, Boolean nonPublic) +69 System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +199 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +572 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +449 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317 Autofac.Integration.Mvc.ExtensibleActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +122 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343 System.Web.Mvc.Controller.ExecuteCore() +116 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10 System.Web.Mvc.<
>c_DisplayClassb.<
BeginProcessRequest>b_5() +37 System.Web.Mvc.Async.<
>c_DisplayClass1.<
MakeVoidDelegate>b_0() +21 System.Web.Mvc.Async.<
>c_DisplayClass8`1.<
BeginSynchronous>b_7(IAsyncResult ) +12 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 System.Web.Mvc.<
>c_DisplayClasse.<
EndProcessRequest>b_d() +50 System.Web.Mvc.SecurityUtil.<
GetCallInAppTrustThunk>b_0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
任何想法?
我正在使用Autofac版本2.4.5.724。
谢谢。
哦,我的,它的工作!谢谢! – 2011-03-25 13:12:31