= .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://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 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 == Commands: * dotnet build * dotnet .\bin\Debug\netcoreapp2.0\testDI.dll === testDI.csproj === {{{#!highlight xml Exe netcoreapp2.0 }}} === 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 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