Category: Programming Tools & Languages

Converting a .Net Standard console app into a .Net Core console app

Converting a .Net Standard console app into a .Net Core console app

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:
      • 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:

Feelin’ Groovy (“Unable to resolve class” or “Configure Groovy SDK”)

Feelin’ Groovy (“Unable to resolve class” or “Configure Groovy SDK”)

I was working with some Groovy scripts in IntelliJ today – a first for me. I came up against a couple of simple getting-started issues… I’m just making notes about them here. There are notes here on two errors I came across: “Unable to resolve class” and “Configure Groovy SDK”.

  1. “Configure Groovy SDK”:
  • On command line:
    • This: brew install groovy
    • Then to run a script: groovy path/to/file.groovy
  • In IntelliJ:
    • Install the groovy plugin
    • Open the folder containing the scripts
    • When it says “Groovy SDK is not configured for module ‘my-module’” or “Configure Groovy SDK”
      • Click the link with the “Configure Groovy SDK” message (top right in IntelliJ)
      • Click Create
      • (Find your Groovy installation:
        • On the command line: brew ls groovy
        • This will give you something like this: /usr/local/Cellar/groovy/2.5.2/bin/groovy
        • Then you need to find your libexec folder – probably at same level as bin folder – in my case it’s here: /usr/local/Cellar/groovy/2.5.2/libexec)
      • Find the libexec folder, select it and click open
      • More here: https://stackoverflow.com/questions/46123890/configuring-groovy-sdk-within-intellij-idea
    • Now you can run a Groovy script by clicking the big green Play button, top right
    • To pass parameters into a script:
      • Top right, click the little down arrow next to the name of the script
      • Click Edit configurations
      • Fill in Program arguments

 

2. “Unable to resolve class” 

  • This can happen when your classes are in a package and you try to run your script from the command line.
  • It will start in the folder the class is in, then from there it will look for a further folder structure – eg if your package is clare.is.cool then it will look for the folder structure clare/is/cool from the path of the groovy script.
  • The solution is to set the classpath on the command line when running the script, and start further back in the directory tree.
    • For instance if your class is here: c:\overall\path\clare\is\cool\MyScript.groovy
    • Then you run it like this: groovy -cp c:\overall\path c:\overall\path\clare\is\cool\MyScript.groovy
    • (or if you have already navigated to c:\overall\path\clare\is\cool, you can just run groovy -cp c:\overall\path MyScript.groovy)

More here: https://stackoverflow.com/questions/45072923/groovy-unable-to-resolve-class