asp.net – 使用ninject时出现异常

我一直在尝试使用Asp.Net Pro MVC 2中的示例,第3章作者使用ninjet进行依赖注入,我已经将代码转换为vb.net,因为他使用了c#,添加了引用但是当我尝试运行代码我得到一个例外

Return DirectCast(kernel.Get(controllerType), IController)

System.InvalidProgramException异常,但是当我使用c#的作者代码时,它工作正常并成功传递该行,

所以任何人都可以帮助我

这是例外细节:

[InvalidProgramException: Common Language Runtime detected an invalid program.]
   System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method) +0
   System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType) +9574057
   Ninject.Injection.DynamicMethodInjectorFactory.Create(ConstructorInfo constructor) +504
   Ninject.Planning.Strategies.ConstructorReflectionStrategy.Execute(IPlan plan) +213
   Ninject.Planning.<>c__DisplayClass1.<GetPlan>b__0(IPlanningStrategy s) +40
   Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable`1 series, Action`1 action) +200
   Ninject.Planning.Planner.GetPlan(Type type) +246
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) +223
   Ninject.Activation.Context.Resolve() +395
   Ninject.KernelBase.<Resolve>b__7(IContext context) +31
   System.Linq.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x) +32
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
   System.Linq.Enumerable.Single(IEnumerable`1 source) +196
   Ninject.ResolutionExtensions.Get(IResolutionRoot root, Type service, IParameter[] parameters) +62
   SportsStore.WebUI.NinjectControllerFactory.GetControllerInstance(RequestContext context, Type controllerType) in D:\Ahmad\VB Projects\SportsStore\SportsStore.WebUI\Infrastructure\NinjectControllerFactory.vb:14
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +204
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +193
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +160
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +80
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +45
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862676
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

最佳答案 没有理由说VB应该表现得很差.您确定使用相同的代码吗?或者尝试使用基于反射的注射:

new StandardKernel(new NinjectSettings { UseReflectionBasedInjection = true })
点赞