Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Library with .NET CLI

Tags:

c#

.net

.net-core

In the last few days I've been trying out the new .NET CLI and although it's fairly straightforward to build console and web applications it is not being at all obvious how to build a class library.

I did the following: as usual, on the command line I've used dotnet new to create a project.json file. I've then coded a simple class in this project and nothing more.

Then I created a console app with the .NET CLI which included the first one as a dependency on project.json and used the class I've built on the class library to show a message on screen.

When I tried to run the console app, the other project was located and the .NET CLI tried to build it. The build of the class library failed with message:

Program does not contain a static 'Main' method suitable for an entry point.

In that case it was treating the project as a console app and trying to find a main entry point.

I believe this happened because when I created the class library with the dotnet new command it generated the project.json as follows:

{
    "version": "1.0.0-*",
    "compilationOptions": {
    "emitEntryPoint": true
},
    "dependencies": {
        "Microsoft.NETCore.App": {
            "type": "platform",
            "version": "1.0.0-*"
        }
    },
    "frameworks": {
        "netcoreapp1.0": {}
    }    
}

Looking there I believe there could be two reasons for this: one of them the fact that the runtime is Microsoft.NETCore.App and second the TFM.

I tried changing the TFM to netstandard1.5 but it didn't work, giving the same error. In that case I believe the issue is with the runtime. Somehow I believe depending on Microsoft.NETCore.App implies we are building a console app and not a library and then one entry point is required.

How is the right way to build a class library with .NET Core CLI then? Is really the runtime the issue? If so, how do we deal with it?

like image 927
user1620696 Avatar asked Apr 17 '16 04:04

user1620696


1 Answers

The two issues here are "emitEntryPoint": true and your dependencies section.

A class library will not have an entry point (the static void Main() method) so emitEntryPoint should be set to false.

As for the dependencies, you can either target your required dependencies specifically

"dependencies" : {
   "System.Console": "4.0.0-*"
}

or the NETStandard.Library NuGet package

"dependencies" : {
   "NETStandard.Library": "1.5.0-*"
}

The NETStandard.Library package is not available from NuGet yet so you'll need to target MyGet until it's officially released. Drop the following into a NuGet.config file within your project folders

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="AspNetCI" value="https://www.myget.org/F/aspnetcirelease/api/v3/index.json" />
    <add key="NuGet.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>
like image 118
gixxer_k3 Avatar answered Nov 15 '22 00:11

gixxer_k3