Size: 25133
Comment:
|
Size: 25204
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 724: | Line 724: |
* export DOTNET_ROOT=$HOME/dotnet * export PATH=$PATH:$HOME/dotnet |
.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.
.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://docs.microsoft.com/pt-pt/dotnet/core/docker/building-net-docker-images
https://msdn.microsoft.com/en-us/library/ff361664(v=vs.110).aspx
.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
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
myApp/myApp.csproj
myLib/Class1.cs
myLib/myLib.csproj
myLibTests/UniTest1.cs
myLibTests/myLibTests.csproj
1 <Project Sdk="Microsoft.NET.Sdk">
2
3 <PropertyGroup>
4 <TargetFramework>netcoreapp2.0</TargetFramework>
5
6 <IsPackable>false</IsPackable>
7 </PropertyGroup>
8
9 <ItemGroup>
10 <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
11 <PackageReference Include="xunit" Version="2.3.1" />
12 <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
13 <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
14 </ItemGroup>
15
16 <ItemGroup>
17 <ProjectReference Include="..\myLib\myLib.csproj" />
18 </ItemGroup>
19
20 </Project>
Docker commands
1 #build image
2 docker build -t dotnetcorehello .
3 # execute bash in running container
4 docker exec -it $1 bash
5 # delete all containers
6 docker ps -a | awk '//{print $1}' | grep -v "CONTAINER" | xargs -i sh -c 'docker stop {};docker rm {}'
7 # execute container using inage dotnetcorehello
8 docker run -d dotnetcorehello
9 # list all containers
10 docker ps -a
dotnet core commands
test Task
1 // buiild and run with mono
2 // mcs testTask.cs
3 // mono testTask.exe
4
5 using System;
6 using System.Threading.Tasks;
7 using System.Threading;
8 // string interpolation with $ might not be supported in mono
9
10 namespace testTask
11 {
12 class Program
13 {
14 static void Main()
15 {
16 var x=1;
17 // String interpolation $"{varxyz}"
18 //Console.WriteLine($"ThreadId {Thread.CurrentThread.ManagedThreadId}");
19 Console.WriteLine("ThreadId {0}",Thread.CurrentThread.ManagedThreadId);
20 Task t = Task.Run( () => {
21 x++;
22 Console.WriteLine("Stuff");
23 //Console.WriteLine($"ThreadId {Thread.CurrentThread.ManagedThreadId}");
24 Console.WriteLine("ThreadId {0}",Thread.CurrentThread.ManagedThreadId);
25 } );
26 t.ContinueWith((Task task)=>{
27 x++;
28 Console.WriteLine("This the end !");
29 //Console.WriteLine($"ThreadId {Thread.CurrentThread.ManagedThreadId}");
30 Console.WriteLine("ThreadId {0}",Thread.CurrentThread.ManagedThreadId);
31 task.ContinueWith((Task t2)=>{
32 x++;
33 Console.WriteLine("t2 {0}",x);
34 Console.WriteLine("ThreadId {0}",Thread.CurrentThread.ManagedThreadId);
35 });
36 });
37 //t.Wait();
38 var tx = Task<long>.Run( ()=>{ return 11L; } );
39
40 tx.ContinueWith((Task<long> txy)=>{
41 //Console.WriteLine($"Res: {txy.Result}");
42 Console.WriteLine("Res: {0}",txy.Result);
43 });
44 }
45 }
46 }
test foreach
1 //build and run using mono
2 //mcs testForeach.cs
3 //mono testForeach.exe
4 using System;
5 using System.Collections.Generic;
6
7 namespace loopNrs
8 {
9 class Program
10 {
11 static void Main()
12 {
13 var numbers = new List<long> { 1,2,3,4 };
14 foreach (var nr in numbers)
15 {
16 //string interpolation is not supported in mono !?
17 // Console.WriteLine($"Hello {nr}!");
18 Console.WriteLine("Hello {0}!",nr);
19 }
20 }
21 }
22 }
Sample DI
Commands:
- dotnet build
- dotnet .\bin\Debug\netcoreapp2.0\testDI.dll
testDI.csproj
Program.cs
1 using System;
2 using Microsoft.Extensions.DependencyInjection;
3
4 namespace testDI
5 {
6 interface IDummy : IDisposable
7 {
8 string GetPong();
9 }
10
11 interface ISnafu : IDisposable
12 {
13 string GetSnafu();
14 }
15
16 interface ITransx : IDisposable
17 {
18 string GetTransx();
19 }
20
21 class Transx : ITransx
22 {
23 private string guid;
24
25 public Transx()
26 {
27 guid = Guid.NewGuid().ToString();
28 Console.WriteLine($"Constructed transx {guid} !");
29 }
30
31 public string GetTransx()
32 {
33 return $"Transx {guid} !";
34 }
35
36 public void Dispose()
37 {
38 Console.WriteLine($"Disposing transx {guid} !");
39 }
40 }
41
42 class Dummy : IDummy
43 {
44 private string guid;
45
46 public Dummy()
47 {
48 guid = Guid.NewGuid().ToString();
49 Console.WriteLine($"Constructed dummy {guid} !");
50 }
51
52 public string GetPong()
53 {
54 return $"Pong {guid} !";
55 }
56
57 public void Dispose()
58 {
59 Console.WriteLine($"Disposing dummy {guid} !");
60 }
61 }
62
63 class Snafu:ISnafu
64 {
65 private IDummy dummy;
66 private string guid;
67
68 public Snafu(IDummy dummy)
69 {
70 this.dummy = dummy;
71 guid = Guid.NewGuid().ToString();
72 Console.WriteLine($"Constructed snafu {guid} !");
73 }
74
75 public string GetSnafu()
76 {
77 return $"Snafu {guid} related with {dummy.GetPong() }!";
78 }
79
80 public void Dispose()
81 {
82 Console.WriteLine($"Disposing snafu {guid} !");
83 }
84 }
85
86 class Program
87 {
88 private static IServiceProvider ioc; //Default implementation ServiceProvider
89
90 private static void PrepareContainer()
91 {
92 var serviceCollection = new ServiceCollection();
93 serviceCollection.AddSingleton<IDummy, Dummy>();
94 serviceCollection.AddSingleton<ISnafu, Snafu>();
95 serviceCollection.AddTransient<ITransx, Transx>();
96 ioc = serviceCollection.BuildServiceProvider();
97 }
98
99 static void Main(string[] args)
100 {
101 Console.WriteLine("Hello World DI!");
102 TestOne();
103 TestTwo();
104 }
105
106 private static void TestOne()
107 {
108 Console.WriteLine("\nTestOne");
109 PrepareContainer();
110 var d1 = ioc.GetService<IDummy>();
111 Console.WriteLine( $"{d1.GetPong()}" );
112 var d2 = ioc.GetService<IDummy>();
113 Console.WriteLine( $"{d2.GetPong()}" );
114 var s1 = ioc.GetService<ISnafu>();
115 Console.WriteLine($"{s1.GetSnafu()}" );
116 var s2 = ioc.GetService<ISnafu>();
117 Console.WriteLine($"{s2.GetSnafu()}" );
118 var t1 = ioc.GetService<ITransx>();
119 Console.WriteLine($"{t1.GetTransx()}" );
120 var t2 = ioc.GetService<ITransx>();
121 Console.WriteLine($"{t2.GetTransx()}" );
122 var t3 = ioc.GetService<ITransx>();
123 Console.WriteLine($"{t3.GetTransx()}" );
124 ReleaseContainer();
125 }
126
127 private static void TestTwo()
128 {
129 Console.WriteLine("\nTestTwo");
130 PrepareContainer();
131 var sx1 = ioc.GetService<ISnafu>();
132 Console.WriteLine($"{sx1.GetSnafu()}" );
133 var dx1 = ioc.GetService<IDummy>();
134 Console.WriteLine( $"{dx1.GetPong()}" );
135 var dx2 = ioc.GetService<IDummy>();
136 Console.WriteLine( $"{dx2.GetPong()}" );
137 ReleaseContainer();
138 }
139
140 private static void ReleaseContainer()
141 {
142 // dispose container and services in it
143 if(ioc is IDisposable)
144 {
145 Console.WriteLine("Can dispose stuff !");
146 ( (IDisposable)ioc).Dispose();
147 }
148 }
149 }
150 }
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/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"; } <h2>@ViewData["Title"].</h2> <h3>@ViewData["Message"]</h3> <p>Use this area to provide additional information.</p>
1 // method About related with About.cshtml
2 // ViewData["Message"] defined in the method and accessed inside the view in the About.cshtml
3 public IActionResult About()
4 {
5 ViewData["Message"] = "Your application description page.";
6
7 return View();
8 // return explicit view Orders
9 // return View("Orders");
10 // return both a view and a model
11 // return View("Orders", Orders);
12 }
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
Strong type sample
// CSHTML @model WebApplication1.ViewModels.Address <h2>Contact</h2> <address> @Model.Street<br> @Model.City, @Model.State @Model.PostalCode<br> <abbr title="Phone">P:</abbr> 425.555.0100 </address> 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
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Threading.Tasks;
6 using Microsoft.AspNetCore;
7 using Microsoft.AspNetCore.Hosting;
8 using Microsoft.Extensions.Configuration;
9 using Microsoft.Extensions.Logging;
10
11 namespace react_test
12 {
13 public class Program
14 {
15 public static void Main(string[] args)
16 {
17 CreateWebHostBuilder(args).Build().Run();
18 }
19
20 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21 WebHost.CreateDefaultBuilder(args)
22 .UseStartup<Startup>().UseUrls("http://0.0.0.0:5000;https://0.0.0.0:5001");
23 }
24 }
Kestrel
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 debian buster
- mkdir ~/dotnet
- mv dotnet-sdk-3.1.201-linux-musl-x64.tar.gz ~/dotnet/
- cd ~/dotnet/
- tar xvzf dotnet-sdk-3.1.201-linux-musl-x64.tar.gz
- export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet