【发布时间】:2013-03-28 20:18:42
【问题描述】:
我正在尝试将 Unity IoC 容器设置为 ASP.NET mvc4 应用程序。 我创建了空项目并使用包管理控制台安装了下一个产品
- 安装包 Unity
- 安装包 Unity.Mvc4
一切顺利完成。但是在我运行我的应用程序后,我遇到了异常
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 异常详细信息:System.MissingMethodException:无法创建接口实例。
这是简单的代码。 Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Bootstrapper.Initialise();
}
}
public interface IA
{
void test();
}
public class A : IA
{
public A() { }
public void test()
{
throw new NotImplementedException();
}
}
HomeController.cs
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index(IA a)
{
return View();
}
}
Bootstrapper.cs:
public static class Bootstrapper
{
public static void Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
container.RegisterType<IA, A>();
return container;
}
}
我做错了什么以及为什么代码不起作用?
堆栈跟踪:
堆栈跟踪:[MissingMethodException:无法创建接口的实例。] System.RuntimeTypeHandle.CreateInstance(RuntimeType 类型,Boolean publicOnly,Boolean noCheck,Boolean& canBeCached,RuntimeMethodHandleInternal& ctor,Boolean& bNeedSecurityCheck) +0 System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔skipCheckThis,布尔fillCache)+98 System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly,布尔skipVisibilityChecks,布尔skipCheckThis,布尔fillCache)+241 System.Activator.CreateInstance(类型类型,布尔非公共)+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) +454 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117 System.Web.Mvc.Async.c_DisplayClass25.b_1e(AsyncCallback asyncCallback, Object asyncState) +476 System.Web.Mvc.Async.WrappedAsyncResult
1.Begin(AsyncCallback callback, Object state, Int32 timeout) +124 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +304 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback 回调,对象状态,Int32 超时)+124 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback 回调,对象状态)+389 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback callback, Object state, Int32 timeout) +124 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +319 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +76 System.Web.Mvc.Async.WrappedAsyncResult1.Begin(AsyncCallback 回调,对象状态,Int32 超时)+124 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback 回调, 对象状态) +251 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback 回调, 对象状态) +50 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8837208 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
【问题讨论】:
-
请提供堆栈跟踪。
-
对不起,我忘记了。完成
-
我认为您需要在控制器中创建一个实例。看看我的回答可能对你有帮助
标签: asp.net inversion-of-control unity-container ioc-container