c# - 调试代码时 Ninject 出现无源可用错误

标签 c# asp.net-mvc-3 ninject-2

我已使用 NuGet 安装最新版本的 Ninject (v2.2.1.4)。

然后我创建了自己的 NinjectDependencyResolver(归功于 Adam Freeman & Steve Sanderson ):

public class NinjectDependencyResolver : IDependencyResolver
{
    private IKernel kernel;

    public NinjectDependencyResolver()
    {
        kernel = new StandardKernel();
        AddBindings();
    }

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

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

    public IBindingToSyntax<T> Bind<T>()
    {
        return kernel.Bind<T>();
    }

    public IKernel Kernel
    {
         get { return kernel; }
    }

    private void AddBindings()
    {
        kernel.Bind<ITitleRepository>().To<TitleRepository>();
        kernel.Bind<IDayRepository>().To<DayRepository>();
        kernel.Bind<IMonthRepository>().To<MonthRepository>();
    }
}

然后在global.asax应用程序启动中注册依赖解析器:

    protected void Application_Start()
    {
        //...other code

        DependencyResolver.SetResolver(new NinjectDependencyResolver());
    }

然后我的代码中有以下行:

ITitleRepository titleRepository = (ITitleRepository)DependencyResolver.Current.GetService(typeof(ITitleRepository));

如果我在 Debug模式下运行代码,它似乎可以正常工作,但是,如果我单步执行此代码(逐行),那么当它运行 kernel.TryGet(serviceType) 时出现以下错误:

没有可用来源

希望图像可见?

enter image description here

有人知道为什么会发生这种情况吗?

最佳答案

发生这种情况是因为 Visual Studio 找不到 Ninject 的源代码。

执行以下操作之一:

  • 下载适当的源代码并将 VS 指向它
  • 配置 VS 使用 symbolsource.org 作为符号服务器(仅适用于 Ninject 3.0.0-rc3 及更高版本)
  • 删除所有 Ninject pdb
  • 在 VS 设置中禁用除您的代码之外的其他代码的调试(工具/选项/调试/仅启用我的代码)

参见http://msdn.microsoft.com/en-us/library/3sehk0fb%28v=vs.100%29.aspx

关于c# - 调试代码时 Ninject 出现无源可用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8506618/

相关文章:

.net - 需要一个简单的使用nhibernate + 工作单元+ 存储库模式+ 服务层+ ninject 的例子

c# - Web 应用程序可以访问和修改 Windows 的注册表吗?

ASP.NET MVC 3 : programmatically add DataAnnotation (RequiredAttribute) to view model

c# - 是否可以使用 MVC/Razor 写出生成的有序数字序列,跨越内容页和母版页?

asp.net-mvc-3 - ASP.NET MVC3 Razor - 将 View 放置在备用位置时丢失智能感知?

c# - 根据其他值获取对象实例,而不使用服务位置

c# - IHostedService/BackgroundService 按计划运行(与 Task.Delay 相反)

c# - 为什么我的项目中程序集绑定(bind)失败?

c# - DateTime 是否在 C# 中保持自身更新?

aop - Ninject拦截扩展中的拦截接口(interface)方法