= .Net core = .NET Core to develop console or Web applications. .NET Core is a set of runtime, library and compiler components that allow you to create apps that run on Windows, macOS and Linux. * https://docs.microsoft.com/en-us/dotnet/core/about .NET Core has the following characteristics: * Cross-platform: Runs on Windows, macOS, and Linux operating systems. * Consistent across architectures: Runs your code with the same behavior on multiple architectures, including x64, x86, and ARM. * Command-line tools: Includes easy-to-use command-line tools that can be used for local development and in continuous-integration scenarios. * Flexible deployment: Can be included in your app or installed side-by-side (user-wide or system-wide installations). Can be used with Docker containers. * Compatible: .NET Core is compatible with .NET Framework, Xamarin, and Mono, via .NET Standard. * Open source: The .NET Core platform is open source, using MIT and Apache 2 licenses. .NET Core is a .NET Foundation project. * Supported by Microsoft: .NET Core is supported by Microsoft, per .NET Core Support. * Supported languages: C#, Visual Basic, and F# languages can be used to write applications and libraries for .NET Core. These components are distributed in the following ways: * .NET Core Runtime -- includes the .NET Core runtime and framework libraries. * ASP.NET Core Runtime -- includes ASP.NET Core and .NET Core runtime and framework libraries. * .NET Core SDK -- includes the .NET Core CLI, ASP.NET Core runtime, and .NET Core runtime and framework. The major differences between .NET Core and the .NET Framework: * App-models -- .NET Core doesn't support all the .NET Framework app-models. In particular, it doesn't support ASP.NET Web Forms and ASP.NET MVC, but it supports ASP.NET Core MVC. And starting with .NET Core 3.0, .NET Core also supports WPF and Windows Forms on Windows only. * APIs -- .NET Core contains a large subset of .NET Framework Base Class Library, with a different factoring (assembly names are different; members exposed on types differ in key cases). In some cases, these differences require changes to port source to .NET Core. For more information, see The .NET Portability Analyzer. .NET Core implements the .NET Standard API specification. * Subsystems -- .NET Core implements a subset of the subsystems in the .NET Framework, with the goal of a simpler implementation and programming model. For example, Code Access Security (CAS) isn't supported, while reflection is supported. * Platforms -- The .NET Framework supports Windows and Windows Server while .NET Core also supports macOS and Linux. * Open Source -- .NET Core is open source, while a read-only subset of the .NET Framework is open source. * https://www.microsoft.com/net/learn/get-started/windows * https://en.wikipedia.org/wiki/ASP.NET_Core * https://hub.docker.com/r/microsoft/dotnet/ * https://docs.microsoft.com/pt-pt/dotnet/core/docker/building-net-docker-images * https://msdn.microsoft.com/en-us/library/ff361664(v=vs.110).aspx * https://docs.microsoft.com/en-us/dotnet/core/ .NET Core is an open-source, general-purpose development platform maintained by Microsoft and the .NET community on GitHub. It's cross-platform (supporting Windows, macOS, and Linux) and can be used to build device, cloud, and IoT applications. Dependency Injection is used similar to Spring. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1 ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. A dependency is any object that another object requires. https://dotnet.microsoft.com/download/dotnet-core/3.0 SDK 3.0.102 * Visual Studio support: Visual Studio 2019 (v16.3 or later) * Included runtimes * .NET Core Runtime 3.0.2 (The .NET Core Runtime contains just the components needed to run a console app.) * ASP.NET Core Runtime 3.0.2 (ASP.NET Core Runtime enables you to run existing web/server applications.) * Desktop Runtime 3.0.2 (run existing Windows desktop applications.) * Language support C# 8.0 F# 4.7 * OS: Linux macOS Windows Dotnet core commands (dotnet core cli - command line interface) (scafolding, prepare projects). {{{ dotnet new console dotnet run }}} https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-1 In browser tutorial * https://www.microsoft.com/net/learn/in-browser-tutorial/1 == Sample hello world == {{{ . ├── appSolution.sln ├── Dockerfile ├── myApp │   ├── myApp.csproj │   └── Program.cs ├── myLib │   ├── Class1.cs │   └── myLib.csproj ├── myLibTests │   ├── myLibTests.csproj │   └── UniTest1.cs }}} === Dockerfile === {{{ FROM microsoft/dotnet:2.0-sdk WORKDIR /app RUN apt-get update RUN apt-get install net-tools procps vim nano -y RUN mkdir -p /app/myApp RUN mkdir -p /app/myLib RUN mkdir -p /app/myLibTests ADD myApp/* /app/myApp/ ADD myLib/* /app/myLib/ ADD myLibTests/* /app/myLibTests/ ADD appSolution.sln /app/ RUN cd /app && dotnet clean && dotnet build RUN cd /app/myLibTests && dotnet clean && dotnet test CMD cd /app/myApp/ && dotnet bin/Debug/netcoreapp2.0/myApp.dll && tail -f /var/log/lastlog #CMD tail -f /var/log/lastlog }}} === appSolution.sln === {{{ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26124.0 MinimumVisualStudioVersion = 15.0.26124.0 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "myApp", "myApp\myApp.csproj", "{5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "myLib", "myLib\myLib.csproj", "{00D6703F-2D64-4D9A-95AA-430DC38E4EC3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Debug|Any CPU.Build.0 = Debug|Any CPU {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Debug|x64.ActiveCfg = Debug|x64 {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Debug|x64.Build.0 = Debug|x64 {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Debug|x86.ActiveCfg = Debug|x86 {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Debug|x86.Build.0 = Debug|x86 {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Release|Any CPU.ActiveCfg = Release|Any CPU {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Release|Any CPU.Build.0 = Release|Any CPU {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Release|x64.ActiveCfg = Release|x64 {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Release|x64.Build.0 = Release|x64 {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Release|x86.ActiveCfg = Release|x86 {5E009AC4-9BF5-4E1B-9C22-377AC9ECC4EB}.Release|x86.Build.0 = Release|x86 {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Debug|Any CPU.Build.0 = Debug|Any CPU {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Debug|x64.ActiveCfg = Debug|x64 {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Debug|x64.Build.0 = Debug|x64 {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Debug|x86.ActiveCfg = Debug|x86 {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Debug|x86.Build.0 = Debug|x86 {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Release|Any CPU.ActiveCfg = Release|Any CPU {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Release|Any CPU.Build.0 = Release|Any CPU {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Release|x64.ActiveCfg = Release|x64 {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Release|x64.Build.0 = Release|x64 {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Release|x86.ActiveCfg = Release|x86 {00D6703F-2D64-4D9A-95AA-430DC38E4EC3}.Release|x86.Build.0 = Release|x86 EndGlobalSection EndGlobal }}} === myApp/Program.cs === {{{#!highlight csharp using System; using myLib; namespace myApp { class Program { static void Main(string[] args) { Console.WriteLine( Class1.getHelloWorld() ); } } } }}} === myApp/myApp.csproj === {{{#!highlight xml Exe netcoreapp2.0 }}} === myLib/Class1.cs === {{{#!highlight csharp using System; namespace myLib { public class Class1 { public static String getHelloWorld() { return "Hello world from library."; } } } }}} === myLib/myLib.csproj === {{{#!highlight xml netstandard2.0 }}} === myLibTests/UniTest1.cs === {{{#!highlight csharp using System; using Xunit; using myLib; namespace myLibTests { public class UnitTest1 { [Fact] public void Test1() { Assert.Equal("Hello world from library." , Class1.getHelloWorld() ); } } } }}} === myLibTests/myLibTests.csproj === {{{#!highlight xml netcoreapp2.0 false }}} === Docker commands === {{{#!highlight bash #build image docker build -t dotnetcorehello . # execute bash in running container docker exec -it $1 bash # delete all containers docker ps -a | awk '//{print $1}' | grep -v "CONTAINER" | xargs -i sh -c 'docker stop {};docker rm {}' # execute container using inage dotnetcorehello docker run -d dotnetcorehello # list all containers docker ps -a }}} === dotnet core commands === {{{#!highlight bash # build solution cd /app dotnet clean dotnet build # run tests for library myLibTests cd /app/myLibTests dotnet clean dotnet test # execute app cd /app/myApp/ dotnet bin/Debug/netcoreapp2.0/myApp.dll }}} == test Task == {{{#!highlight csharp // buiild and run with mono // mcs testTask.cs // mono testTask.exe using System; using System.Threading.Tasks; using System.Threading; // string interpolation with $ might not be supported in mono namespace testTask { class Program { static void Main() { var x=1; // String interpolation $"{varxyz}" //Console.WriteLine($"ThreadId {Thread.CurrentThread.ManagedThreadId}"); Console.WriteLine("ThreadId {0}",Thread.CurrentThread.ManagedThreadId); Task t = Task.Run( () => { x++; Console.WriteLine("Stuff"); //Console.WriteLine($"ThreadId {Thread.CurrentThread.ManagedThreadId}"); Console.WriteLine("ThreadId {0}",Thread.CurrentThread.ManagedThreadId); } ); t.ContinueWith((Task task)=>{ x++; Console.WriteLine("This the end !"); //Console.WriteLine($"ThreadId {Thread.CurrentThread.ManagedThreadId}"); Console.WriteLine("ThreadId {0}",Thread.CurrentThread.ManagedThreadId); task.ContinueWith((Task t2)=>{ x++; Console.WriteLine("t2 {0}",x); Console.WriteLine("ThreadId {0}",Thread.CurrentThread.ManagedThreadId); }); }); //t.Wait(); var tx = Task.Run( ()=>{ return 11L; } ); tx.ContinueWith((Task txy)=>{ //Console.WriteLine($"Res: {txy.Result}"); Console.WriteLine("Res: {0}",txy.Result); }); } } } }}} == test foreach == {{{#!highlight csharp //build and run using mono //mcs testForeach.cs //mono testForeach.exe using System; using System.Collections.Generic; namespace loopNrs { class Program { static void Main() { var numbers = new List { 1,2,3,4 }; foreach (var nr in numbers) { //string interpolation is not supported in mono !? // Console.WriteLine($"Hello {nr}!"); Console.WriteLine("Hello {0}!",nr); } } } } }}} == Sample DI (in debian buster)== Commands: * dotnet build * dotnet bin/Debug/netcoreapp3.1/testDI.dll === testDI.csproj === {{{#!highlight xml Exe netcoreapp3.1 }}} === Program.cs === {{{#!highlight csharp using System; using Microsoft.Extensions.DependencyInjection; namespace testDI { interface IDummy : IDisposable { string GetPong(); } interface ISnafu : IDisposable { string GetSnafu(); } interface ITransx : IDisposable { string GetTransx(); } class Transx : ITransx { private string guid; public Transx() { guid = Guid.NewGuid().ToString(); Console.WriteLine($"Constructed transx {guid} !"); } public string GetTransx() { return $"Transx {guid} !"; } public void Dispose() { Console.WriteLine($"Disposing transx {guid} !"); } } class Dummy : IDummy { private string guid; public Dummy() { guid = Guid.NewGuid().ToString(); Console.WriteLine($"Constructed dummy {guid} !"); } public string GetPong() { return $"Pong {guid} !"; } public void Dispose() { Console.WriteLine($"Disposing dummy {guid} !"); } } class Snafu:ISnafu { private IDummy dummy; private string guid; public Snafu(IDummy dummy) { this.dummy = dummy; guid = Guid.NewGuid().ToString(); Console.WriteLine($"Constructed snafu {guid} !"); } public string GetSnafu() { return $"Snafu {guid} related with {dummy.GetPong() }!"; } public void Dispose() { Console.WriteLine($"Disposing snafu {guid} !"); } } class Program { private static IServiceProvider ioc; //Default implementation ServiceProvider private static void PrepareContainer() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddTransient(); ioc = serviceCollection.BuildServiceProvider(); } static void Main(string[] args) { Console.WriteLine("Hello World DI!"); TestOne(); TestTwo(); } private static void TestOne() { Console.WriteLine("\nTestOne"); PrepareContainer(); var d1 = ioc.GetService(); Console.WriteLine( $"{d1.GetPong()}" ); var d2 = ioc.GetService(); Console.WriteLine( $"{d2.GetPong()}" ); var s1 = ioc.GetService(); Console.WriteLine($"{s1.GetSnafu()}" ); var s2 = ioc.GetService(); Console.WriteLine($"{s2.GetSnafu()}" ); var t1 = ioc.GetService(); Console.WriteLine($"{t1.GetTransx()}" ); var t2 = ioc.GetService(); Console.WriteLine($"{t2.GetTransx()}" ); var t3 = ioc.GetService(); Console.WriteLine($"{t3.GetTransx()}" ); ReleaseContainer(); } private static void TestTwo() { Console.WriteLine("\nTestTwo"); PrepareContainer(); var sx1 = ioc.GetService(); Console.WriteLine($"{sx1.GetSnafu()}" ); var dx1 = ioc.GetService(); Console.WriteLine( $"{dx1.GetPong()}" ); var dx2 = ioc.GetService(); Console.WriteLine( $"{dx2.GetPong()}" ); ReleaseContainer(); } private static void ReleaseContainer() { // dispose container and services in it if(ioc is IDisposable) { Console.WriteLine("Can dispose stuff !"); ( (IDisposable)ioc).Dispose(); } } } } }}} === Example output === {{{ Hello World DI! TestOne Constructed dummy 37a7cced-15c3-4b33-9eec-55f0fb58ce8c ! Pong 37a7cced-15c3-4b33-9eec-55f0fb58ce8c ! Pong 37a7cced-15c3-4b33-9eec-55f0fb58ce8c ! Constructed snafu 619160b5-6e67-4396-8555-f453bd1ad06f ! Snafu 619160b5-6e67-4396-8555-f453bd1ad06f related with Pong 37a7cced-15c3-4b33-9eec-55f0fb58ce8c !! Snafu 619160b5-6e67-4396-8555-f453bd1ad06f related with Pong 37a7cced-15c3-4b33-9eec-55f0fb58ce8c !! Constructed transx 6cf6600a-e622-46fd-8d10-3b6015601911 ! Transx 6cf6600a-e622-46fd-8d10-3b6015601911 ! Constructed transx c666091c-efe9-407f-8659-6c1bdc7e2b7f ! Transx c666091c-efe9-407f-8659-6c1bdc7e2b7f ! Constructed transx 421a63ca-0e93-4656-ba5c-c8909774827c ! Transx 421a63ca-0e93-4656-ba5c-c8909774827c ! Can dispose stuff ! Disposing transx 421a63ca-0e93-4656-ba5c-c8909774827c ! Disposing transx c666091c-efe9-407f-8659-6c1bdc7e2b7f ! Disposing transx 6cf6600a-e622-46fd-8d10-3b6015601911 ! Disposing snafu 619160b5-6e67-4396-8555-f453bd1ad06f ! Disposing dummy 37a7cced-15c3-4b33-9eec-55f0fb58ce8c ! TestTwo Constructed dummy 7233afd9-9f2e-43a4-ae1a-bc0abba6abc7 ! Constructed snafu 10298883-4f16-4383-b5b7-ce9e5ad90715 ! Snafu 10298883-4f16-4383-b5b7-ce9e5ad90715 related with Pong 7233afd9-9f2e-43a4-ae1a-bc0abba6abc7 !! Pong 7233afd9-9f2e-43a4-ae1a-bc0abba6abc7 ! Pong 7233afd9-9f2e-43a4-ae1a-bc0abba6abc7 ! Can dispose stuff ! Disposing snafu 10298883-4f16-4383-b5b7-ce9e5ad90715 ! Disposing dummy 7233afd9-9f2e-43a4-ae1a-bc0abba6abc7 ! }}} == Eager service bean instantiation == To instantiate a service/bean before being used else where add it as an argument in the Configure method that belongs to the Startup class. == RBAC role based authorization == * https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.0&tabs=visual-studio%2Caspnetcore2x * https://docs.microsoft.com/en-us/aspnet/core/security/authorization/secure-data?view=aspnetcore-2.0 * https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-2.0 * https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.0 Identity is enabled for the application by calling UseAuthentication in the Configure method. Startup.cs in dotnet core to init the services/beans. In ASP.NET Core MVC, views are .cshtml files that use the C# programming language in Razor markup. * About.cshtml {{{ @{ ViewData["Title"] = "About"; }

@ViewData["Title"].

@ViewData["Message"]

Use this area to provide additional information.

}}} * HomeController.cs {{{#!highlight csharp // method About related with About.cshtml // ViewData["Message"] defined in the method and accessed inside the view in the About.cshtml public IActionResult About() { ViewData["Message"] = "Your application description page."; return View(); // return explicit view Orders // return View("Orders"); // return both a view and a model // return View("Orders", Orders); } }}} View discovery searches for a matching view file in this order: * Views/[ControllerName]/[ViewName].cshtml * Views/Shared/[ViewName].cshtml Pass data to views using several approaches: * Strongly-typed data: viewmodel * Weakly-typed data * ViewData (ViewDataAttribute) * ViewBag Strong type sample {{{ // CSHTML @model WebApplication1.ViewModels.Address

Contact

@Model.Street
@Model.City, @Model.State @Model.PostalCode
P: 425.555.0100
To provide the model to the view, the controller passes it as a parameter: // C# public IActionResult Contact() { ViewData["Message"] = "Your contact page."; var viewModel = new Address() { Name = "Microsoft", Street = "One Microsoft Way", City = "Redmond", State = "WA", PostalCode = "98052-6399" }; return View(viewModel); } }}} Angular automatically passes an antiforgery token in a request header named X-XSRF-TOKEN. The ASP.NET Core MVC app is configured to refer to this header in its configuration in Startup.cs: {{{ public void ConfigureServices(IServiceCollection services) { // Angular's default header name for sending the XSRF token. services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN"); services.AddMvc(); } }}} == Install dotnetcore in CentOS == * rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm * yum install dotnet-sdk-2.2 * dotnet new console --name hello-console * yum install epel-release * yum install nodejs * dotnet new reactredux --name "react-test" * cd react-test/ * dotnet clean * dotnet build * dotnet publish * dotnet bin/Debug/netcoreapp2.2/publish/react-test.dll * vi Program.cs {{{#!highlight csharp using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; namespace react_test { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup().UseUrls("http://0.0.0.0:5000;https://0.0.0.0:5001"); } } }}} == Kestrel == * https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1 Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the web server that's included by default in ASP.NET Core project templates. Kestrel supports the following scenarios: * HTTPS * Opaque upgrade used to enable WebSockets * Unix sockets for high performance behind Nginx * HTTP/2 (except on macOS†) == Install dotnetcore in Debian buster == {{{ wget https://packages.microsoft.com/config/ubuntu/19.10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb sudo apt-get update sudo apt-get install apt-transport-https sudo apt-get update sudo apt-get install dotnet-sdk-3.1 nodejs npm }}} * https://code.visualstudio.com/ * cd ~/Downloads * wget https://go.microsoft.com/fwlink/?LinkID=760868 * mv index.html\?LinkID\=760868 vscode.deb * sudo dpkg -i vscode.deb # 1.43.2 * code & * Install extension ms-dotnettools.csharp * Install extension Eclipse Keymap alphabotsec.vscode-eclipse-keybindings === Angular project === {{{ # nodejs --version # v10.15.2 # npm --version 5.8.0 # dotnet --version # 3.1.201 cd ~ dotnet new angular --name test # creates folder test with the project cd test dotnet clean dotnet build dotnet run https://localhost:5001/ # There are issues building the project on the sandbox folder shared with windows through a mounted folder. }}}