Last year we tasted the very first bits of the NET Core platform. The RTM is out and we are close to the next 2.0 version. Is this the right moment to adopt the platform?
What are the changes of the new project system brought from Visual Studio 2017? What is new in the tools, sdk and packaging?
Adopting the new NET Core doesn’t necessarily mean migrating everything in a single step but mixing it with the regular .NET Framework thanks to the new net standard which is evolving to the 2.0 version as well.
During this code-first session, we will try to see the developer’s and architect’s perspectives in a variety of scenarios, including the cross-platform and Raspberry.
1. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Adopting NET Core
in mainstream projects
Raffaele Rialdi
@raffaeler
raffaeler@vevy.com
https://www.linkedin.com/in/raffaelerialdi/
3. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Who am I?
• I am Raffaele Rialdi, Senior Software Architect in Vevy Europe – Italy
• I am also consultant in many industries
– manufacturing, racing, healthcare, financial, …
• But also Speaker in italian and international conferences
– Trainer as well
• And very proud to have been awarded as a Microsoft MVP
– Since 2003
@raffaeler
raffaeler@vevy.com
github.com/raffaeler
5. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• It is a brand new CLR called CLRCore
– Code and libraries are open source on GitHub
• It runs cross-platforms (Windows, Linux and Mac)
– Also cross-devices (x86, x64, ARM)
• Targets the "server apps" (ASP.NET Core) and UWP
– No Winform, WPF or other UI-related set of classes
• Offers a powerful CLI
• Package-centric development lifecycle
– nuget is king
NetCore takeaways
6. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Simplified version of the old csproj and live-editable
The new ".csproj" project format
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
<UserSecretsId>aspnet-aspnetcore20basic-2A96D10C-1768-4724-A51D-9B5C2ED7DC9A</UserSecretsId>
<DockerComposeProjectPath>..docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview1-final" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0-preview1-final" />
</ItemGroup>
</Project>
The full csproj file for an ASP.NET Core MVC Application running on Docker
7. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• MVC/WebApi Apps
–ASP.NET Core is powerful and very fast
• Cross-platform web and console apps
–No ARM CPUs support
• Libraries based on netstandard
–They can be binary-shared across Frameworks
• It is also used on UWP and Xamarin
What should I use NetCore 1.x for?
8. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• A huge amount of new APIs ~33'000 APIs
–More than twice compared to NetCore 1.0
• A single metapackage library
–Include the full ASP.NET Core stack
–If NetCore is pre-installed, deploying is super-small!
• Currently in Preview 1, RTM in Q3
What's coming with NetCore 2.0?
10. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• A library specification defining a set of APIs with no implementation
– Think to netstandard as it was a sort of huge interface
– All NET Frameworks must implement all those APIs
• CoreFX (the 'BCL' of NetCore) is a superset of netstandard
• Netstandard 2.0 libraries can reference .NET Framework 'old' libs
– A "compat shim" is provided to redirect the implementation to netstandard
– Only works if the same API is available on netstandard
– No recompile is needed, but once recompiled the shim is no longer needed
– About 70% of the current nuget published packages can be already used!
• Are you a library author? Make your library "netstandard" now!
What is netstandard?
11. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Current support for netstandard 1.0 to 1.6
Framework TFM
.NET Standard netstandard 1.0 1.1 1.2 1.3 1.4 1.5 1.6 2.0
.NET Core netcoreapp 1.0 1.0 1.0 1.0 1.0 1.0 1.0
.NET Framework net 4.5 4.5 4.5.1 4.6 4.6.1 4.6.2 vNext
Mono / Xamarin - 4.6 4.6 4.6 4.6 4.6 4.6 4.6
UWP uap 10.0 10.0 10.0 10.0 10.0 vNext vNext
Windows win 8.0 8.0 8.1
Windows Phone wpa 8.1 8.1 8.1
Silverlight wp 8.0
(obsolete frameworks)
more APIs
more Frameworks
12. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
netstandard 2.0 support (at the RTM time)
Framework TFM
.NET Standard netstandard 1.0 1.1 1.2 1.3 1.4 1.5 1.6 2.0
.NET Core netcoreapp 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0
.NET Framework net 4.5 4.5 4.5.1 4.6 4.6.1 4.6.1 4.6.1 4.6.1
Mono / Xamarin - 4.6 4.6 4.6 4.6 4.6 4.6 4.6 vNext
UWP uap 10.0 10.0 10.0 10.0 10.0 vNext vNext vNext
Windows win 8.0 8.0 8.1
Windows Phone wpa 8.1 8.1 8.1
Silverlight wp 8.0
(obsolete frameworks)
more APIs
more Frameworks
Unity Game Framework is going to support netstandard 2.0
Version # of APIs
netstandard 1.6 13,501
netstandard 2.0 ~33 K
Delta +20 K
This table will be valid only when netstandard 2.0 will be shipped
13. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Add Reference, MSDN said .... but ...
• The .NET 4.x project gets a dependency from:
– At least the System.Runtime.dll (the nuget one) version 4.1.0
• This version depends on the netstandard version you choose
– Other nuget packages (such as System.Reflection)
• But versions may be different ("original" vs nuget)
Using a netstandard library from a .NET project
14. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• App.Config is your best friend
–If an EXE explicitly reference the DLL referencing
nestandard
• You can use the DLL project app.config
• Or you can specify AutoBindingRedirect in the EXE csproj
–If it is dynamically loaded
• Pre-load System.Runtime using Assembly.LoadFrom
• Or intercept the load request with AssemblyResolve event
What if the dll nuget version is more recent?
<bindingRedirect
oldVersion="0.0.0.0-4.1.1.0"
newVersion="4.1.1.0" />
15. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• T4 templates are "evil"
– Dependencies can be resolved by referencing a DLL using the
mentioned solutions
– Pssst ... use Roslyn code generator instead!
• ETW EventRegister.exe forcibly load the original (wrong)
version in a secondary AppDomain (and fails)
– Extract the requested DLL from the nuget package and specify
this dll as a reference in the EventRegister options
• Code Generation
– Specify these additional dependencies
Edge cases in managing different versions
17. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Current Stable Release
– Full NetCore 1.x tooling / templates support
– Netstandard support
• Preview Release (Update 3 Preview)
– NetCore 1.x + 2.0 Preview 1 support
– Netstandard 2 support
– Live Unit Testing in .NET Core
– Multiple Framework support when debugging
• Preview releases live side-by-side and isolated
– Two setups, no messing up the installation, twice the size on the HDD ☺
Visual Studio 2017
18. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
One CLI for all the SDK versions
• The current (latest) version of the sdk
– dotnet --version
• Creating a new project
– dotnet new console use the latest version
– dotnet new console --framework netcoreapp1.1
• Otherwise you can still force the version using global.json
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.3"
}
}
19. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
API Cross-Reference
• A cross-reference with all .NET APIs
– Availability of every APIs in the .NET Framework, Mono, Net Core, …
• Answers to the question: which framework does support this API?
http://apisof.net
20. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• In VS2017 pick the "Publish" menu in the solution explorer
• The CLI can do it too:
dotnet publish –r ubuntu.14.04-arm –o folder –c Release
• The RID (Runtime Identifier) must be specified in the csproj
• The available RIDs are in the
Microsoft.NETCore.Platforms
package
Self-Contained Depolyment (SCD)
<RuntimeIdentifiers>win8-arm;ubuntu.14.04-arm;ubuntu.16.04-arm</RuntimeIdentifiers>
21. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• A NetCore app / library
– targets
– references Microsoft.NETCore.App
• A Netstandard library targets
– targets
– references NETStandard.Library
• Future 2.0 netcore apps
– targets
• Run on a specific framework version (this may change in the future)
How Projects specify the framework
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netstandard1.4</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeFrameworkVersion>2.0.0-beta-001863-00</RuntimeFrameworkVersion>
Never reference these metapackages in the nuget references
22. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Reducing the footprint of the SCD
• Replace netcoreapp1.1 with netstandard1.6
• Explicitly reference these base packages
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.Runtime.CoreCLR" Version="1.0.2" />
<PackageReference Include="Microsoft.NETCore.DotNetHostPolicy" Version="1.0.1" />
</ItemGroup>
<TargetFramework>netstandard1.6</TargetFramework>
Standard templates Console WebApp
Default win10-x64 45.6 MB 55.5 MB
Default ubuntu.16.04-x64 52.6 MB 62.6 MB
Reduced win10-x64 28.5 MB 47.4 MB
Reduced ubuntu.16.04-x64 36.8 MB 55.9 MB
23. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Custom templates in a single slide
H:_demosTemplateDemo>dotnet new -i .
Templates Short Name Language Tags
----------------------------------------------------------------------------------------------
Console Application console [C#], F# Common/Console
Class library classlib [C#], F# Common/Library
Unit Test Project mstest [C#], F# Test/MSTest
xUnit Test Project xunit [C#], F# Test/xUnit
ASP.NET Core Empty web [C#] Web/Empty
ASP.NET Core Web App mvc [C#], F# Web/MVC
ASP.NET Core Web API webapi [C#] Web/WebAPI
A console app referencing Json.net library rafconsole Custom Templates
Solution File sln Solution
Examples:
dotnet new mvc --auth None --framework netcoreapp1.1
dotnet new rafconsole
dotnet new --help
Current folder
template.json
{
"author": "Raf",
"classifications": [ "Custom Templates" ],
"name": "A console app referencing Json.net library",
"identity": "RafConsole.RafConsoleTemplate.CSharp",
"shortName": "rafconsole",
"guids": [ "0AB14D86-C56C-4702-907F-C5F3C4E0031C" ],
"sourceName": "RafConsoleTemplate"
}
24. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Interoperability with Node.JS
–Come to my session tomorrow, May 25 - 10:30
–Use your C# libraries from Node.JS
• Warning for the weak of heart
–The session also contains C++ code ☺
Tomorrow @ 10.30