Lightning Talk - .NET CLI in 5 Minutes
Wrote this up after reading through Jeremy Miller's article on .NET CLI as a 5 minute lightning talk.
.NET Core Command Line Interface
Can we be friends with it?
Test Setup
- .NET Core installed 2.2
- Windows Subsystem for Linux (Ubuntu 18.04 LTS image)
Check your version of .NET Core
dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 2.2.105
Commit: 7cecb35b92
Runtime Environment:
OS Name: ubuntu
OS Version: 18.04
OS Platform: Linux
RID: ubuntu.18.04-x64
Base Path: /usr/share/dotnet/sdk/2.2.105/
...
Create a working directory
mkdir best && cd best
Run the command line new
option
dotnet new
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.
--nuget-source Specifies a NuGet source to use during install.
--type Filters templates based on available types. Predefined values are "project", "item" or "other".
--dry-run Displays a summary of what would happen if the given command line were run if it would result in a template creation.
--force Forces content to be generated even if it would change existing files.
-lang, --language Filters templates based on language and 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
xUnit Test Project xunit [C#], F#, VB Test/xUnit
...
Razor Class Library razorclasslib [C#] Web/Razor/Library/Razor Class Library
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
Create a Web API project
dotnet new webapi --name tippytop
The template "ASP.NET Core Web API" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on tippytop/tippytop.csproj...
Restoring packages for /mnt/c/Users/rnmichael/Documents/best/tippytop/tippytop.csproj...
Generating MSBuild file /mnt/c/Users/rnmichael/Documents/best/tippytop/obj/tippytop.csproj.nuget.g.props.
Generating MSBuild file /mnt/c/Users/rnmichael/Documents/best/tippytop/obj/tippytop.csproj.nuget.g.targets.
Restore completed in 4.45 sec for /mnt/c/Users/rnmichael/Documents/best/tippytop/tippytop.csproj.
Restore succeeded.
Create an empty solution file
dotnet new sln
The template "Solution File" was created successfully.
Add the current project to the Solution
dotnet sln best.sln add tippytop/tippytop.csproj
Project `tippytop/tippytop.csproj` added to the solution.
Create a test project (using xUnit)
dotnet new xunit --output tippytop.tests
The template "xUnit Test Project" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on tippytop.tests/tippytop.tests.csproj...
Restoring packages for /mnt/c/Users/rnmichael/Documents/best/tippytop.tests/tippytop.tests.csproj...
Generating MSBuild file /mnt/c/Users/rnmichael/Documents/best/tippytop.tests/obj/tippytop.tests.csproj.nuget.g.props.
Generating MSBuild file /mnt/c/Users/rnmichael/Documents/best/tippytop.tests/obj/tippytop.tests.csproj.nuget.g.targets.
Restore completed in 2.65 sec for /mnt/c/Users/rnmichael/Documents/best/tippytop.tests/tippytop.tests.csproj.
Restore succeeded.
Add the test project to your Solution
dotnet sln best.sln add tippytop.tests/tippytop.tests.csproj
Project `tippytop.tests/tippytop.tests.csproj` added to the solution.
Reference your Web API project in the test project
dotnet add tippytop.tests/tippytop.tests.csproj reference tippytop/tippytop.csproj
Reference `..\tippytop\tippytop.csproj` added to the project.
Build the solution
dotnet build best.sln
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restoring packages for /mnt/c/Users/rnmichael/Documents/best/tippytop.tests/tippytop.tests.csproj...
Restore completed in 410.61 ms for /mnt/c/Users/rnmichael/Documents/best/tippytop/tippytop.csproj.
Restore completed in 509.78 ms for /mnt/c/Users/rnmichael/Documents/best/tippytop.tests/tippytop.tests.csproj.
tippytop -> /mnt/c/Users/rnmichael/Documents/best/tippytop/bin/Debug/netcoreapp2.2/tippytop.dll
tippytop.tests -> /mnt/c/Users/rnmichael/Documents/best/tippytop.tests/bin/Debug/netcoreapp2.2/tippytop.tests.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:05.52
Run tests
dotnet build && dotnet clean && dotnet test
Run the solution
dotnet run --project tippytop/tippytop.csproj
Test the default response
curl -X GET https://localhost:5001/api/values -H 'cache-control: no-cache' --insecure | jq '.'
[
"value1",
"value2"
]
Create a Dockerfile
Place this file in the tippytop folder
FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "tippytop.dll"]
Build the Docker image
Move to webapi folder
cd tippytop
Build and tag docker image
docker build -t tippytop .
Run the new Docker image
docker run -d -p 8080:80 --name myapp tippytop
Test the Web API in the running container
curl -X GET http://localhost:8080/api/values -H 'cache-control: no-cache' | jq '.'
Conclusions
- Command Line Interface support for .NET Core works!!!
- Scripting templates, setups, and tests are good options
- Microsoft is pushing developers to automate and familiarize themselves with these tools going forward
- Understanding the CLI will help you debug problems in Visual Studio