简体   繁体   中英

Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

I want to use Ninject in an Asp.Net vNext Class Library. But I get Compiler Error CS0246 ("The type or namespace name 'Ninject' could not be found").

Here is what i have done:

1) Create a new ASP.NET vNext Class Library

2) Edit project.json :

{         
    "dependencies": {
        "Ninject": "3.2.2.0"
    },
    "configurations" : {
        "net451" : { 
            "dependencies": {
            }
        },
        "k10" : { 
            "dependencies": {
                "System.Runtime": "4.0.20.0"
            }
        }
    }
}

3) Check Package Manager Log:

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

Looks fine ..

4) Edit Class1.cs :

using Ninject;

namespace ClassLib1
{
    public class Class1
    {
        public Class1()
        {
            var kernel = new StandardKernel();
        }
    }
}

Intellisense can resolve the references to Ninject.

在此处输入图片说明

5) Build and I got 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)

What's wrong?

NInject is not available for the Cloud Optimized CLR (CoreCLR or K10). Also, there is a known issue in VS 2014 that it builds all the available configurations, in your case net451 and k10, regardless of what you chose in the project properties - that's why you see these two lines and the errors under K:

1>  Building ClassLib1 .NETFramework,Version=v4.5.1
1>  Building ClassLib1 K,Version=v1.0

If you need NInject and you want to target both Desktop CLR and Core CLR then use a #if K10 or #if NET45 conditional compilation block like I did here . Basically you will use NInject for Desktop CLR and the out of the box DI container for Core CLR.

If you want your app to run only on Desktop CLR, then remove the K10 configuration from project.json .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM