If you have a simple .Net console app and you’d like to convert it into a .Net Core app so that you can run it from the command line on any platform (as long as you install .Net Core), here’s what to do:
- Remove the following files and folders from your project:
- Bin folder
- Obj folder
- Packages folder
- Properties / Assemblyinfo.cs
- config
- Create a new temp folder and run this command in that folder: dotnet new console
- Take the csproj file created by that command and use it to replace the csproj file in your original project (using the name of the original csproj file).
- !! If your original csproj file was in a nested folder, the new csproj file will have to be in the root.
- But your program.cs file can stay where it was.
- Or you can create a *.sln folder at the root level and add references to any nested csproj files.
- To create a sln file: dotnet new sln
- To add a csproj file (in a nested tests folder) to your sln file: dotnet sln add .\tests\tests.csproj
- Command line: dotnet restore
- Now you can build and run the new project from the command line: dotnet run
- Check the old project for packages to include:
- Look in packages.config
- For instance, if you see a line that looks like this:
- <package id=”NUnit” version=”3.11.0″ targetFramework=”net452″ />
- …you need to run this on the command line: dotnet add package nunit -v 3.11.0
- !!! If NUnit is one of the packages, you need to do some extra actions – see Adding NUnit tests to a .Net Core console app.
- Gotchas:
- You can’t build and run a console app in Visual Studio if the code is in Google Drive. You’ll get a very generic uninformative error – “Unable to start program … refresh the process list”.
- If you try to add the NUnit package manually, you may get errors. Follow the actions in Adding NUnit tests to a .Net Core console app to fix.
- Otherwise you may get the following errors:
- 1) “Unable to find tests” Fix this with the following command (check version – this was Oct 2018): dotnet add package Microsoft.NET.Test.Sdk -v 15.7.2
- 2) “Program has more than one entry point defined”. This is fixed by adding the following sub-element to a <PropertyGroup> element in your csproj file:
- This: <GenerateProgramFile>false</GenerateProgramFile>
- More info here: https://andrewlock.net/fixing-the-error-program-has-more-than-one-entry-point-defined-for-console-apps-containing-xunit-tests/
- 3) “No test is available”. Fix this by adding the NUnit3TestAdapter package (at time of writing – Oct 2018 – this was version 3.10.0).
See also the following post:
One thought on “Converting a .Net Standard console app into a .Net Core console app”