【发布时间】:2014-10-01 23:46:19
【问题描述】:
我想在 Asp.Net vNext 类库中使用 Ninject。但我得到编译器错误 CS0246(“找不到类型或命名空间名称 'Ninject'”)。
这是我所做的:
1) 新建ASP.NET vNext Class Library
2) 编辑project.json:
{
"dependencies": {
"Ninject": "3.2.2.0"
},
"configurations" : {
"net451" : {
"dependencies": {
}
},
"k10" : {
"dependencies": {
"System.Runtime": "4.0.20.0"
}
}
}
}
3) 检查包管理器日志:
Restoring packages for C:\Projects\ClassLib1\project.json
Attempting to resolve dependency ClassLib1 >= 1.0.0
Attempting to resolve dependency Ninject >= 3.2.2.0
GET https://www.nuget.org/api/v2/FindPackagesById()?Id='Ninject'
Attempting to resolve dependency mscorlib >=
Attempting to resolve dependency System >=
Attempting to resolve dependency System.Core >=
Attempting to resolve dependency Microsoft.CSharp >=
Attempting to resolve dependency ClassLib1 >= 1.0.0
Attempting to resolve dependency Ninject >= 3.2.2.0
Attempting to resolve dependency System.Runtime >= 4.0.20.0
OK https://www.nuget.org/api/v2/FindPackagesById()?Id='Ninject' 1250ms
GET https://www.nuget.org/api/v2/FindPackagesById?id='Ninject'&$skiptoken='Ninject','3.0.2-unstable-9057'
OK https://www.nuget.org/api/v2/FindPackagesById?id='Ninject'&$skiptoken='Ninject','3.0.2-unstable-9057' 179ms
GET https://www.nuget.org/api/v2/package/Ninject/3.2.2
OK https://www.nuget.org/api/v2/package/Ninject/3.2.2 949ms
Resolving complete, 2531ms elapsed
Installing Ninject 3.2.2.0
Restore complete, 2755ms elapsed
看起来不错..
4) 编辑Class1.cs:
using Ninject;
namespace ClassLib1
{
public class Class1
{
public Class1()
{
var kernel = new StandardKernel();
}
}
}
Intellisense 可以解析对 Ninject 的引用。
5) 构建并获得 CS0246:
1>------ Build started: Project: ClassLib1, Configuration: Debug Any CPU ------
1> Building ClassLib1 .NETFramework,Version=v4.5.1
1> Building ClassLib1 K,Version=v1.0
1>C:\Projects\ClassLib1\Class1.cs(1,7): error CS0246: The type or namespace name 'Ninject' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Projects\ClassLib1\Class1.cs(9,30): error CS0246: The type or namespace name 'StandardKernel' could not be found (are you missing a using directive or an assembly reference?)
1>
1> Build failed.
1> 0 Warnings(s)
1> 2 Error(s)
怎么了?
【问题讨论】:
-
在此示例中:github.com/aspnet/DependencyInjection/tree/dev/src/…。他们在 project.json 中有一条奇怪的线 - “Microsoft.Framework.DependencyInjection”:“”,也许这就是 ninject 的问题?
-
没有。我用
Microsoft.Framework.DependencyInjection和Microsoft.Framework.DependencyInjection.Ninject测试了它 - 得到了同样的错误。
标签: c# asp.net asp.net-core