How to use the .NET Core Class Library in a .NET .NET application?
In particular, it doesn't work on ubuntu.
For example, if I do this:
dotnet new sln -n HelloWorld
dotnet new classlib -n HelloLib
dotnet new console -n HelloApp
dotnet sln add ./HelloApp/HelloApp.csproj
dotnet sln add ./HelloLib/HelloLib.csproj
dotnet restore
dotnet build
cd HelloApp/
dotnet add reference ../HelloLib/HelloLib.csproj
And change Program.cs like this:
using System;
using HelloLib;
namespace HelloApp
{
class Program
{
static void Main(string[] args)
{
var x = new Class1();
Console.WriteLine("Hello World!");
}
}
}
The application will then compile creating these artifacts:
HelloApp/bin$ du -a
4 ./Debug/netcoreapp1.1/HelloApp.deps.json
4 ./Debug/netcoreapp1.1/HelloApp.runtimeconfig.json
4 ./Debug/netcoreapp1.1/HelloApp.pdb
4 ./Debug/netcoreapp1.1/HelloLib.pdb
8 ./Debug/netcoreapp1.1/HelloApp.dll
4 ./Debug/netcoreapp1.1/HelloApp.runtimeconfig.dev.json
4 ./Debug/netcoreapp1.1/HelloLib.dll
36 ./Debug/netcoreapp1.1
40 ./Debug
44 .
... but application execution fails:
$ dotnet HelloApp.dll
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'HelloLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
Aborted (core dumped)
If I remove the line using HelloLib
, it works:
$ dotnet HelloApp.dll
Hello World!
What's with that?
I am guessing it has something to do with confusing incompatibilities in the project files:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework> <-- **THIS**
</PropertyGroup>
</Project>
against
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework> <-- **And THIS**
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\HelloLib\HelloLib.csproj">
<Project>{1607a379-5bae-423b-8efc-796a06556be0}</Project>
<Name>HelloLib</Name>
</ProjectReference>
</ItemGroup>
</Project>
I assumed this was just a bug.
... but other people seem to be using the .NET core without too much trouble.
So, am I doing something wrong?
Or are people using ".NET core" just not using class libraries because they don't work?
(edit: versions:
Microsoft .NET Core Shared Framework Host
Version : 1.1.0
Build : 928f77c4bc3f49d892459992fb6e1d5542cb5e86
$ dpkg -l |grep dotnet
ii dotnet-dev-1.0.3 1.0.3-1 amd64 .NET Core SDK 1.0.3
$ cat /etc/issue
Ubuntu 16.10 \n \l
source to share
After execution
dotnet add reference ../HelloLib/HelloLib.csproj
You have to do more
dotnet restore
therefore the links will be tracked correctly by the project. If you follow the same steps but add dotnet restore
after the step dotnet add reference
, it works great.
Anytime you change or update your refrences, you must call dotnet restore
afterwards or change your statement dotnet build
like dotnet build /t:restore
so that it does a pre-build restore for you.
source to share