简体   繁体   中英

How do I use a .NET core class library in a .NET core application?

Specifically on ubuntu, this doesn't appear to work.

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 modify Program.cs to be:

using System;
using HelloLib;

namespace HelloApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = new Class1();
            Console.WriteLine("Hello World!");
        }
    }
}

Then the application will compile , generating 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 executing the application 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 using HelloLib line, it works:

$ dotnet HelloApp.dll 
Hello World!

What's up with that?

I presume its got something to do with the confusing incompatibility in the project files:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework> <-- **THIS**
  </PropertyGroup>

</Project>

vs:

<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 use .NET core without too much trouble.

So, am I doing something wrong?

Or are the 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

After you do

dotnet add reference ../HelloLib/HelloLib.csproj

You must do an additional

dotnet restore

so the references will be properly tracked by the project. If you follow your same steps but add a dotnet restore after the dotnet add reference step it works fine.

Any time you change or update your refrences you must call dotnet restore afterward or change your dotnet build statement to be dotnet build /t:restore so it will do the restore before the build for you.

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