ASP.NET Core

I apologize for not being consistent on my posts. Technology and projects change quite rapidly so what might be of interest today might not be in a few weeks. In the past few weeks a decision was made to use the Windows platform to develop the next generation of a storage server. The storage server will be developed using ASP.NET Core. Due to that fact, I have been experimenting with the ASP.NET Core SDK and runtime. Yes, the last statement is not a typo. The ASP.NET Core has a SDK using its own version number and a runtime using a different version number.

So far I have been working with the command line tool dotnet and notepad++ as an editor (no IDE). I was impressed with the ease of use of the dotnet tool. Today decided to switch to an IDE. Given that ASP.NET Core is a Microsoft product, Visual Studio 2017 Enterprise Edition seemed like the best choice. I had to update my Visual Studio because a Microsoft website noted that the ASP.NET Core tools are supported on VS version 15.3.0 or higher. My version was 15.2.x.

To install ASP.NET Core from the console was a breeze. The instructions were simple and most important correct. In general information regarding Windows is convoluted and most of the time does not work.

I tried adding support for ASP.NET Core directly from Visual Studio 2017 Enterprise. I was not able to find a way to get this done. I then recalled that you can use the installer of Visual Studio to add and remove installed features. While the installer was updating my version of Visual Studio I found on the web how to select workloads for it. It seems like Microsoft attempted to copy the mechanism used by Eclipse to do so. The difference being, that you find the app store inside the Eclipse IDE; not in the installer. It seems that with Visual Studio you need to use the installer, which I was lucky to find in my computer.

The following steps are the ones I used to generate and run hello.dll using a Windows console:

Created the C:\Core directory and moved into it.

C:\>mkdir Core
C:\>cd Core

I then checked the options available from the dotnet application.

C:\Core>dotnet -h
.NET Command Line Tools (2.0.0)
Usage: dotnet [runtime-options] [path-to-application]
Usage: dotnet [sdk-options] [command] [arguments] [command-options]

path-to-application:
  The path to an application .dll file to execute.

SDK commands:
  new              Initialize .NET projects.
  restore          Restore dependencies specified in the .NET project.
  run              Compiles and immediately executes a .NET project.
  build            Builds a .NET project.
  publish          Publishes a .NET project for deployment (including the runtime).
  test             Runs unit tests using the test runner specified in the project.
  pack             Creates a NuGet package.
  migrate          Migrates a project.json based project to a msbuild based project.
  clean            Clean build output(s).
  sln              Modify solution (SLN) files.
  add              Add reference to the project.
  remove           Remove reference from the project.
  list             List reference in the project.
  nuget            Provides additional NuGet commands.
  msbuild          Runs Microsoft Build Engine (MSBuild).
  vstest           Runs Microsoft Test Execution Command Line Tool.

Common options:
  -v|--verbosity        Set the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].
  -h|--help             Show help.

Run 'dotnet COMMAND --help' for more information on a command.

sdk-options:
  --version        Display .NET Core SDK version.
  --info           Display .NET Core information.
  -d|--diagnostics Enable diagnostic output.

runtime-options:
  --additionalprobingpath <path>    Path containing probing policy and assemblies to probe for.
  --fx-version <version>            Version of the installed Shared Framework to use to run the application.
  --roll-forward-on-no-candidate-fx Roll forward on no candidate shared framework is enabled.
  --additional-deps <path>          Path to additonal deps.json file.

It seems like the next step would be to check out the new option.

C:\Core>dotnet new -h
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Specifies the language of the template to create.

Templates                                         Short Name       Language          Tags
--------------------------------------------------------------------------------------------------------
Console Application                               console          [C#], F#, VB      Common/Console
Class library                                     classlib         [C#], F#, VB      Common/Library
Unit Test Project                                 mstest           [C#], F#, VB      Test/MSTest
xUnit Test Project                                xunit            [C#], F#, VB      Test/xUnit
ASP.NET Core Empty                                web              [C#], F#          Web/Empty
ASP.NET Core MVC Basic                            mvcbasic         [C#]              Web/MVC
ASP.NET Core MVC Fast                             mvcfast          [C#]              Web/MVC
ASP.NET Core Web App (Model-View-Controller)      mvc              [C#], F#          Web/MVC
ASP.NET Core Web App                              razor            [C#]              Web/MVC/Razor Pages
ASP.NET Core with Angular                         angular          [C#]              Web/MVC/SPA
ASP.NET Core with React.js                        react            [C#]              Web/MVC/SPA
ASP.NET Core with React.js and Redux              reactredux       [C#]              Web/MVC/SPA
ASP.NET Core Static Web                           staticweb        [C#]              Web/Static
ASP.NET Core Web API                              webapi           [C#], F#          Web/WebAPI
global.json file                                  globaljson                         Config
Nuget Config                                      nugetconfig                        Config
Web Config                                        webconfig                          Config
Solution File                                     sln                                Solution
Razor Page                                        page                               Web/ASP.NET
MVC ViewImports                                   viewimports                        Web/ASP.NET
MVC ViewStart                                     viewstart                          Web/ASP.NET

Examples:
    dotnet new mvc --auth Individual
    dotnet new razor --auth Individual
    dotnet new --help

If you are familiar with Git, you can see the resemblance from the dotnet application. It provides examples on how to use the commands as you go.

Once again it seems that a good choice would be to create a console application.

C:\Core>dotnet new console -h
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Specifies the language of the template to create.


Console Application (C#)
Author: Microsoft
Description: A project for creating a command-line application that can run on .NET Core on Windows, Linux and macOS
Options:
  --no-restore  If specified, skips the automatic restore of the project on create.
                bool - Optional
                Default: false

We would like to specify the name for our console project so I issued the following:

C:\Core>dotnet new console -o hello
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on hello\hello.csproj...
  Restoring packages for C:\Core\hello\hello.csproj...
  Generating MSBuild file C:\Core\hello\obj\hello.csproj.nuget.g.props.
  Generating MSBuild file C:\Core\hello\obj\hello.csproj.nuget.g.targets.
  Restore completed in 257.41 ms for C:\Core\hello\hello.csproj.

Restore succeeded.

It seems like dotnet has created a project with our application. Now let’s take a look at the current folder.

C:\Core>dir
 Volume in drive C is OS
 Volume Serial Number is 26E8-87B0

 Directory of C:\Core

10/08/2017  09:17 AM    <DIR>          .
10/08/2017  09:17 AM    <DIR>          ..
10/08/2017  09:17 AM    <DIR>          hello
               0 File(s)              0 bytes
               3 Dir(s)  667,868,180,480 bytes free

C:\Core>cd hello

C:\Core\hello>dir
 Volume in drive C is OS
 Volume Serial Number is 26E8-87B0

 Directory of C:\Core\hello

10/08/2017  09:17 AM    <DIR>          .
10/08/2017  09:17 AM    <DIR>          ..
10/08/2017  09:17 AM               178 hello.csproj
10/08/2017  09:17 AM    <DIR>          obj
10/08/2017  09:17 AM               187 Program.cs
               2 File(s)            365 bytes
               3 Dir(s)  667,868,180,480 bytes free

Let’s now see if it works.

C:\Core\hello>dotnet run
Hello World!

It seems like all is well. The program executed under control of dotnet.

Using Notepad++ edited the source file produced by the dotnet application. The contents of the Program.cs file follow:

using System;

namespace hello
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Changed the text of the Console.WriteLine() method call to “Hello John !!!” and save it.

We now need to tell dotnet that it needs to rebuild and run the application. This can be accomplished as follows:

C:\Core\hello>dotnet restore
  Restore completed in 27.18 ms for C:\Core\hello\hello.csproj.

C:\Core\hello>dotnet run
Hello John !!!

Take a look at the code generated by the dotnet application:

C:\Core\hello>dir
 Volume in drive C is OS
 Volume Serial Number is 26E8-87B0

 Directory of C:\Core\hello

10/08/2017  09:21 AM    <DIR>          .
10/08/2017  09:21 AM    <DIR>          ..
10/08/2017  09:21 AM    <DIR>          bin
10/08/2017  09:17 AM               178 hello.csproj
10/08/2017  09:21 AM    <DIR>          obj
10/08/2017  09:26 AM               189 Program.cs
               2 File(s)            367 bytes
               4 Dir(s)  667,868,622,848 bytes free

C:\Core\hello>cd bin

C:\Core\hello\bin>dir
 Volume in drive C is OS
 Volume Serial Number is 26E8-87B0

 Directory of C:\Core\hello\bin

10/08/2017  09:21 AM    <DIR>          .
10/08/2017  09:21 AM    <DIR>          ..
10/08/2017  09:21 AM    <DIR>          Debug
               0 File(s)              0 bytes
               3 Dir(s)  667,868,622,848 bytes free

C:\Core\hello\bin>cd Debug

C:\Core\hello\bin\Debug>dir
 Volume in drive C is OS
 Volume Serial Number is 26E8-87B0

 Directory of C:\Core\hello\bin\Debug

10/08/2017  09:21 AM    <DIR>          .
10/08/2017  09:21 AM    <DIR>          ..
10/08/2017  09:21 AM    <DIR>          netcoreapp2.0
               0 File(s)              0 bytes
               3 Dir(s)  667,868,622,848 bytes free

C:\Core\hello\bin\Debug>cd netcoreapp2.0

C:\Core\hello\bin\Debug\netcoreapp2.0>dir
 Volume in drive C is OS
 Volume Serial Number is 26E8-87B0

 Directory of C:\Core\hello\bin\Debug\netcoreapp2.0

10/08/2017  09:21 AM    <DIR>          .
10/08/2017  09:21 AM    <DIR>          ..
10/08/2017  09:21 AM               447 hello.deps.json
10/08/2017  09:27 AM             4,608 hello.dll
10/08/2017  09:27 AM               400 hello.pdb
10/08/2017  09:21 AM               234 hello.runtimeconfig.dev.json
10/08/2017  09:21 AM               154 hello.runtimeconfig.json
               5 File(s)          5,843 bytes
               2 Dir(s)  667,868,622,848 bytes free

The dotnet application produced a hello.dll file. DLLs are not executables. The DLL needs to be executed under control of the dotnet application.

Like I said before, the console application dotnet seems to work fine. I am sure there is a way to open and more importantly run the code generated by dotnet from Visual Studio. Given that I like to use a full blown IDE (i.e., Eclipse, Visual Studio) when developing software, I decided that from this point on, will stop using a text editor (i.e., Notepad, notepad++, vim) while experimenting with ASP.NET Core. I will now replicate the hello.dll using Visual Studio Enterprise 2017.

I opened Visual Studio. Selected File -> New -> Project … and the New Project window is displayed. On the left pane I selected Visual C# -> .NET Core -> Console App (.NET Core) and on the bottom of the window I filled the following:

Name: hello
Location: C:\Users\John\Documents\Visual Studio 2017\Projects
Solution name: hello

When done clicked on the <OK> button.

After a few seconds the Solution Explorer pane contains the contents of my new project. The Programs.cs file contains the following source:

using System;

namespace hello
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Set a break point on the left bracket following the call to Console.WriteLine(). I then clicked on Debug button on the top of the IDE window. A console labeled C:\Program Files\dotnet.dotnet.exe is displayed with the following text:

Hello World!

Things are looking good so far. I then edited the text and ran the code.

Hello John !!!

I will continue experimenting with APS.NET Core in the next few weeks and will post my findings as I get into more exciting examples.

If you have comments or questions, please leave me a message towards the bottom of the post.

Happy development;

John

Follow me on Twitter:  @john_canessa

2 thoughts on “ASP.NET Core”

  1. Hi John

    I am desperate so I am reaching out to anyone who might be able to help.

    I have Windows 11, VS 22 community and VS code, dotnet 6. I can’t get any SP App to run in either environment. It trys to launch the app but can’t find the client, I think. I understan dotnet 6 starts up SPA apps differently.

    Can you tell me how to fix this?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.