= .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);
}
}
}
}
}}}