调试代码时Ninject没有源可用错误 [英] No Source Available Error With Ninject When Debugging Code
问题描述
我已经使用NuGet安装了最新版本的Ninject(v2.2.1.4).
I have used NuGet to install the latest version of Ninject (v2.2.1.4).
然后,我创建了自己的NinjectDependencyResolver(要获得
I have then created my own NinjectDependencyResolver (credit to 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应用启动中注册依赖项解析器:
And then registered the dependency resolver in the global.asax applcation startup:
protected void Application_Start()
{
//...other code
DependencyResolver.SetResolver(new NinjectDependencyResolver());
}
然后我的代码中包含以下行:
I then have the following line in my code:
ITitleRepository titleRepository = (ITitleRepository)DependencyResolver.Current.GetService(typeof(ITitleRepository));
如果我在调试模式下运行代码,它会显示以正常工作,但是,如果我(逐行)进入此代码,则它将在运行内核时运行.发生以下错误:
If I run the code through in debug mode it appears to work correctly, however, if I step into this code (line by line) then when it runs the kernel.TryGet(serviceType) the following error occurs:
无可用来源
希望图像可见吗?
有人知道为什么会发生这种情况吗?
Does anyone have any idea why this may be occuring?
推荐答案
之所以会发生这种情况,是因为Visual Studio找不到Ninject的源代码.
This happens because Visual Studio does not find the source code for 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
这篇关于调试代码时Ninject没有源可用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!