Maciek
Maciek

Reputation: 19893

Can't import netstandard1.6 project into netcore1.0 app

I've created a fresh Asp.Net Core WebApi application and a .NET Core library that I intend to reference in that appliation.

I've referenced the library in the app, however it seems that the application is unable to resolve anything within that library.

web api app

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
    },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dnxcore50"
        //"dotnet5.6",
        //"portable-net45+win8"
      ],
      "dependencies": {
        "PoC.ServiceDiscovery": {
          "version": "1.0.0-*",
          "target": "project"
        }
      }
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "runtimes": {
    "win10-x64" :  {} ,
    "ubuntu.16.04-x64":  {} 
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

library

{
  "version": "1.0.0-*",
  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Microsoft.Extensions.DependencyInjection": "1.0.0"
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  },
  "runtimes": {
    "win10-x64": {},
    "ubuntu.16.04-x64": {}
  }
}

Regards

Upvotes: 1

Views: 291

Answers (3)

sina_Islam
sina_Islam

Reputation: 1163

Write

 <ItemGroup>
    <ProjectReference Include="..\YourProjectName\YourProjectName.csproj" />
  </ItemGroup>

to the targeted .csproj project.

Upvotes: 0

Technetium
Technetium

Reputation: 6158

(1) Remove the runtimes from the library's project.json. Your library is defining a set of APIs that it utilizes (the .NET Standard 1.6 APIs). The implementations of those APIs which are utilized is chosen by the runtime that depends on your library. It might work with them listed, but it doesn't make any sense for a .NET Standard library.

(2) Your library lacks a name. Give it a name so you can reference it in your WebAPI project.json. I'm going to assume it will be called PoC.ServiceDiscovery for the sake of clarity.

(3) Put PoC.ServiceDiscovery, assuming that is your library's name, into the normal list of dependencies in the WebAPI's project.json file, right next to the Microsoft.* libraries.

(4) You need to give your WebAPI project knowledge of where to look for this library. There are two main ways of doing this. One way is to share the library using a package manager like NuGet or MyGet (which requires updates to a NuGet.config file) or, what I think you want to do in your scenario, a local project reference. In that case, add a global.json file in your root directory with an array of relative paths that contain your library and WebAPI projects, and make the dependency in the WebAPI project a project reference. That would mean it would look like this: "PoC.ServiceDiscovery": { "target": "project" }

If you would like to see an example, here is a very simple library I've written that uses .NET Standard for the library, a project reference for the library's test project, and a global.json file that allows the dotnet command line tool to know where to look for the library when running the tests.

Upvotes: 1

J. Doe
J. Doe

Reputation: 2747

At first there is no longer separated ASP.NET WebApi because its now a part of ASP.NET MVC

But backing to main topic - .NET Standard is only higher level API abstract. Support for .NET Core 1.0 does not mean that run cross plat (because it support also .NET Framework, Universal Windows Platform etc.). So you have 2 ways to do in library:

  • Remove netstandard1.6 and replace netcoreapp1.0
  • Remove runtimes

For more informationi suggest read https://github.com/dotnet/standard/blob/master/docs/versions.md

Upvotes: 0

Related Questions