本文介绍了如何做动作注射MVC 3使用Autofac?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建ASP.NET MVC 3应用程序欲借控制器动作注射描述此处。

I'm creating an ASP.NET MVC 3 application trying to take advantage of controller action injection as described here.

控制器构造函数注入工作没有任何问题,但我似乎无法得到行动注射工作。

Controller constructor injection works without any issues, but I can't seem to get action injection to work.

我已经设置了Autofac在Global.asax中是这样的:

I've set up Autofac in Global.asax like this:

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));

和我的控制器有一个这样的动作:

And my controller has an action like this:

public ActionResult Test(ITestInject testInject)
{
        return View(testInject);
}

的TestInject / ITestInject类/接口被定义为:

The TestInject/ITestInject class/interface is defined as :

public interface ITestInject
{
    string TestString { get; }
}
public class TestInject : ITestInject
{
    public string TestString { get; set; }
}

当我尝试导航到测试行动,我看到这个错误:

When I try to navigate to the the Test action, I see this error:

服务器错误/应用程序。

无法创建接口的实例。

说明:在当前Web请求的执行过程中发生未处理的异常。请检查堆栈跟踪有关该错误的详细信息以及它起源于code。

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.

异常详细信息:system.missingMethodException而:无法创建接口的实例

Exception Details: System.MissingMethodException: Cannot create an instance of an interface.

源错误:

当前Web请求的执行过程中生成了未处理的异常。可以使用异常堆栈跟踪下面确定有关异常原因和位置信息。

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.

堆栈跟踪:


[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) +184

版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.1

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

这是我做错了什么在这里的任何想法?

Any ideas on what I'm doing wrong here?

我使用Autofac版本2.4.5.724。

I'm using Autofac version 2.4.5.724.

感谢。

推荐答案

行动注入默认情况下,在当前Autofac MVC3实施关闭。

Action injection is turned off by default in the current Autofac MVC3 implementation.

要启用它,参数传递给 ExtensibleActionInvoker

To enable it, pass a parameter to ExtensibleActionInvoker:

builder.RegisterType<ExtensibleActionInvoker>()
    .As<IActionInvoker>()
    .WithParameter("injectActionMethodParameters", true);

这是一个相当最近的更改和一些文档需要更新 - 有关的麻烦遗憾

This was quite a recent change and some documentation will need updating - sorry about the hassle.

这篇关于如何做动作注射MVC 3使用Autofac?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 01:35