1

我正在嘗試將Unity IoC容器設置爲ASP.NET mvc4應用程序。 我創建的空項目,並使用包管理控制檯安裝下一個產品Microsoft Unity IoC不起作用和ASP.NET mvc4應用程序。爲什麼?

  • 安裝,包裝統一
  • 安裝,包裝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類型,布爾publicOnly,布爾NOCHECK,布爾& canBeCached,RuntimeMethodHandleInternal &構造函數,布爾& bNeedSecurityCheck)0 System.RuntimeType.CreateInstanceSlow(布爾publicOnly,布爾skipCheckThis,布爾fillCache)98 System.RuntimeType.CreateInstanceDefaultCtor(布爾publicOnly,布爾skipVisibilityChecks,布爾skipCheckThis,啵精簡fillCache)+241 System.Activator.CreateInstance(Type type,Boolean nonPublic)+69 System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Type modelType)+190 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.A同步。 <> C_ DisplayClass25.b _1e(的AsyncCallback的AsyncCallback,對象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.WrappedAsyncResult 1.Begin(AsyncCallback的回調,對象的狀態,的Int32超時)124 System.Web.Mvc .Controller.BeginExecuteCore(AsyncCallback callback,Object state)+389 System.Web.Mvc.Async.WrappedAsyncResult 1.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.WrappedAsyncResult 1。開始(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上下文,AsyncCallback cb,Object extraData)+16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep。執行()8837208 System.Web.HttpApplication.ExecuteStep(IExecutionStep步驟,布爾& completedSynchronously)184

+0

請提供堆棧跟蹤。 – Steven 2013-04-06 11:46:17

+0

對不起,我忘了它。完成 – Vitalii 2013-04-06 11:49:18

+1

我想你需要在控制器中創建一個實例。看到我的答案,它可能會幫助你 – 2013-04-06 12:01:09

回答

4

我完全不知道你的問題,但這裏是適用於我的設置;

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<ICategoryRepository, CategoryRepository>(new HierarchicalLifetimeManager()); 
     return container; 
    } 
} 

的Global.asax

Bootstrapper.Initialise(); 
現在

,在我的控制器構造;

ICategoryRepository _catRepo; 

public CategoryController(ICategoryRepository catRepo) 
{ 
    _catRepo = catRepo; 
} 
public ActionResult Index() 
{ 
    ViewBag.CategoriesList = _catRepo.GetAllCategories(); 
    return View(); 
} 
+2

我把接口的行動,而不是控制器的構造函數。這似乎是我需要一些放鬆:)感謝很多幫助 – Vitalii 2013-04-06 12:13:53

+0

很高興幫助:) – 2013-04-06 12:14:50

相關問題