加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

为什么NuPack生成的NinjectMVC3.cs无法编译? (或者ASP.NET MVC

发布时间:2020-12-16 04:13:08 所属栏目:asp.Net 来源:网络整理
导读:使用NuPack插件并安装NInject MVC 3程序包会在生成的NinjectMVC3.cs文件中导致以下编译错误. 当前上下文中不存在名称“MvcServiceLocator” sample video David Ebbo发布的显示为working just fine at 09:43. 这是当前生成的类: public class NinjectMVC3 {
使用NuPack插件并安装NInject MVC 3程序包会在生成的NinjectMVC3.cs文件中导致以下编译错误.

当前上下文中不存在名称“MvcServiceLocator”

sample video David Ebbo发布的显示为working just fine at 09:43.

这是当前生成的类:

public class NinjectMVC3 {
    public static void RegisterServices(IKernel kernel) {
        //kernel.Bind<IThingRepository>().To<SqlThingRepository>();
    }

    public static void SetupDependencyInjection() {
        // Create Ninject DI Kernel 
        IKernel kernel = new StandardKernel();

        // Register services with our Ninject DI Container
        RegisterServices(kernel);

        // Tell ASP.NET MVC 3 to use our Ninject DI Container 
        MvcServiceLocator.SetCurrent(new NinjectServiceLocator(kernel));
    }
}

解决方法

基本上,MvcServiceLocator已经消失了.我想,每当制作视频时,版本都会出现版本不匹配的情况.

here和here有很好的解释.

使Ninject工作的两个步骤如下.用以下内容替换NinjectMVC3(我也更改了不必要的名称):

public class NinjectResolver : IDependencyResolver
{
    private static IKernel kernel;

    public NinjectResolver()
    {
        kernel = new StandardKernel();
        RegisterServices(kernel);
    }

    public static void RegisterServices(IKernel kernel)
    {
        //kernel.Bind<IThingRepository>().To<SqlThingRepository>();
    }

    public object GetService(Type serviceType)
    {
        return kernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return kernel.GetAll(serviceType);
    }
}

并将以下行添加到gloabl.asax.cs中的App_Start()

DependencyResolver.SetResolver(new NinjectResolver());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读