What to Expect in ASP .NET Core 3.0
Here are a few things you can expect in ASP .NET Core 3.0, whenever it comes out next year:
- Newtonsoft’s Json.NET will be removed from the shared framework
- EF Core will be shipped separately as pure NuGet packages
- ASP .NET Core 3.0 will only run on the cross-platform .NET Core 3.0
- ASP .NET Core 3.0 will NOT run on the Windows-only .NET Framework
- Note that some new C# 8.0 features will only be coming to .NET Core 3.0
You can get more information on ASP .NET Core 3.0 from an Oct 2018 announcement:
- ASP.NET Core 3.0 first look: https://blogs.msdn.microsoft.com/webdev/2018/10/29/a-first-look-at-changes-coming-in-asp-net-core-3-0/
Some additional things you should know about Blazor, the cool kid on the block:
- Blazor on WebAssembly is still experimental at this time: https://blazor.net/docs
- Server-Side Blazor (aka Razor Components) is expected to ship with .NET Core 3.0
- You can get the Blazor project templates and tooling today: https://marketplace.visualstudio.com/items?itemName=aspnet.blazor
From Daniel Roth (Oct 2018): “We are now working towards shipping Razor Components and the editing in .NET Core 3.0,”… “This includes integrating Razor Components into ASP.NET Core so that it can be used from MVC. We expect to have a preview of this support early next year after the ASP.NET Core 2.2 release has wrapped up.
- Server-Side Blazor to Ship in .NET Core 3.0: https://visualstudiomagazine.com/articles/2018/10/03/blazor-update.aspx?m=1
What’s New in ASP .NET Core 2.2 Right Now
So what’s new in ASP .NET Core 2.2 right now? First of all, you can work with ASP .NET Core 2.2 in the latest version of Visual Studio 2017 or VS Code with the .NET Core 2.2 SDK. There are many cool new features, notably:
- New Health Checks API
- Improved Open API (Swagger) Integration
- Client-side template updates (Bootstrap 4, Angular 6)
- SignalR Java Client
- HTTP/2 Server Support (Preview)
- Improved perf: IIS throughput, MVC model validation + routing, HTTP Client
You can read the full announcement here:
- Announcing ASP.NET Core 2.2: https://blogs.msdn.microsoft.com/webdev/2018/12/04/asp-net-core-2-2-available-today/
You can get also a recap of the API improvements and Health Checks API in this video from Connect:
- Code Samples: https://github.com/bradygaster/DispatchR/tree/master/src/DispatchR.Api
- Video: https://www.youtube.com/watch?v=_vw3hcnSA1Y
Health Checks
From the aforementioned Github repo, here is a code snippet for the new Health Checks feature:
public void ConfigureServices(IServiceCollection services) { ... services.AddHealthChecks() .AddDbContextCheck<PlacesContext>(); ... } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { ... app.UseHealthChecks("/ready"); ... }
In the above snippet, the call to AddHealthChecks() followed by AddDbContextCheck() ensures that the data connection is healthy before determining that the application is healthy. The route /ready can be accessed in a browser or externally to determine the health of the application.
Open API (Swagger)
Features for using Open API (Swagger) with ASP .NET Core was already included in ASP .NET Core 2.1 and has further improved with ASP .NET Core 2.2. As before, you can use community-driven projects such as NSwag and Swashbuckle.AspNetCore to visualize Open API documents for your API.
Benefits include:
- interactive documentation
- client SDK generation
- API discoverability
So what’s new in 2.2? We now have Microsoft.AspNetCore.Mvc.Api.Analyzers, which work with [APIController]-decorated controllers introduced in 2.1, and also builds on API conventions. With this package added to a new Web API project, my .csproj project file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp2.2</TargetFramework> <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Api.Analyzers" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> </ItemGroup> </Project>
So what do these new analyzers do? One example can be explained by starting with the code snippet below:
// GET api/values/5 [HttpGet("{id}")] [ProducesResponseType(200)] public ActionResult<string> Get(int? id) { if (id == null) { return NotFound(); } return "value"; }
In this simple Get method in my API Controller, it returns an HTTP 200 (Success!) Status Code when everything is going well, which is indicated by [ProducesResponsesType(200)]. But if the id value is null, it returns NotFound(). In this case, the analyzer can automatically guide you to add the proper attribute for a HTTP 400 (Not Found) Status Code as well.
See this illustrated in the GIF below:
Here is the end result:
// GET api/values/5 [HttpGet("{id}")] [ProducesResponseType(200)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesDefaultResponseType] public ActionResult<string> Get(int? id) { if (id == null) { return NotFound(); } return "value"; }
If you use common API Conventions, you can make your life even easier by using Conventions. Conventions can be applied via attributes in 3 different ways: at the assembly level, at the controller level and at the method level, each deeper level superseding the attribute at the higher level. These can then be superseded by ProducesResponseType applied at an action level.
Three examples are shown below:
At the assembly level:
[assembly: ApiConventionType(typeof(DefaultApiConventions))]
At the controller level, e.g. ItemsController
[ApiConventionType(typeof(DefaultApiConventions))] [ApiController] [Route("/api/[controller]")] public class ItemsController : ControllerBase { ... }
At the method level, e.g. PutItem()
// PUT: api/items/5 [ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Put))] [HttpPut("{id}")] public async Task<ActionResult<Item>> PutItem(long id, Item item) { ... }
For more information, check out the official announcement from August 2018:
- Open API Analyzers & Conventions: https://blogs.msdn.microsoft.com/webdev/2018/08/23/asp-net-core-2-20-preview1-open-api-analyzers-conventions/
What’s New in EF Core 2.2 & 3.0
EF Core 2.2 includes some new features and a ton of bug fixes. New features include:
- Spatial data support: used to represent the physical location and shape of objects
- Collections of owned entities: extends the ability to model ownership to 1-to-many associations, going beyond 1-to-1 associations (previously added in 2.0)
- Query tags: allows you to annotate a LINQ query with a string of text that appears as comments in generated SQL output (which may be captured in logs)
EF Core 3.0 will bring several new features:
- LINQ improvements
- Cosmos DB support
- C# 8.0 support
- Reverse engineering database views into query types
- Property bag entities
- EF 6.3 on .NET Core
These are all explained in detail in the official announcement:
- EF Core 2.2: https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-entity-framework-core-2-2/
What to Expect in C# 8.0
From one of the C# 8.0 announcements, “The current plan is that C# 8.0 will ship at the same time as .NET Core 3.0. However, the features will start to come alive with the previews of Visual Studio 2019”.
Here are some new features you can expect in C #8.0:
- Nullable Reference Types: get a friendly warning if you try to assign non-nullable reference types to null. This setting can be toggled, to assist in future projects and also provide warnings in existing projects. In other words, C# 8.0 allows you to express “nullable intent”.
string myString = null; // Friendly Warning! string? myString = null; // This is ok
- IAsyncEnumerable<T> for consumption of async streams: It is essentially an asynchronous version of IEnumerable<T>, allowing you to await foreach over the items in an async stream.
async IAsyncEnumerable<int> GetBigResultsAsync() { await foreach (var result in GetResultsAsync()) { if (result > SomeValue) yield return result; } }
- Ranges and Indices: A new Index type and a .. (two dots) range feature bring in the ability to easily slice through subsets of ranges of values. You may have missed such a feature in C# if you’ve worked with Python to manipulate your data.
// for an array of ints int[] myArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Index index1 = 3; // 0-index, number 3, counted from beginning Index index2 = ^4; // 4th item from end, counting backwards Console.WriteLine($"{myArray[index1]}, {myArray[index2]}"); // "3, 6" var mySlice = myArray[index1..index2]; // { 3, 4, 5 } not including end
When I used the above code in Visual Studio 2019 Preview 1, I had to update my .csproj to use C# 8.0. Take a look at the example below:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.0</TargetFramework> <LangVersion>8.0</LangVersion> </PropertyGroup> </Project>
- Default implementations of interface members: this gives you the ability to add a real implementation of an interface member that will be used by any class that implements the interface but doesn’t explicitly define the member in its class. Existing classes will automatically get this implementation.
interface ILogger { // method 1 defined in interface void Log(LogLevel level, string message); /// method 2 can be added to interface, even after class was created void Log(Exception ex) => Log(LogLevel.Error, ex.ToString()); } class ConsoleLogger : ILogger { // method 1, explicitly implemented in class public void Log(LogLevel level, string message) { ... } // method 2, Log(Exception) gets default implementation }
You can get more information on C# 8.0 features at:
- Building C# 8.0: https://blogs.msdn.microsoft.com/dotnet/2018/11/12/building-c-8-0/
References, Docs & Tutorials
New/Updated Downloads:
- .NET Core 2.2 (Release): https://dotnet.microsoft.com/download/dotnet-core/2.2
- .NET Core 3.0 (Preview): https://dotnet.microsoft.com/download/dotnet-core/3.0
- Visual Studio 2017 15.9 (Release): https://visualstudio.microsoft.com/downloads/
- Visual Studio 2019 (Preview): https://visualstudio.microsoft.com/vs/preview/
- VS Code (Stable): https://code.visualstudio.com/
- VS Code (Insiders): https://code.visualstudio.com/insiders/
.NET Core 3 and ASP .NET Core 3.0 References:
- (May 7, 2018) .NET Core 3 and Support for Windows Desktop Applications | .NET Blog: https://blogs.msdn.microsoft.com/dotnet/2018/05/07/net-core-3-and-support-for-windows-desktop-applications/
- (Oct 4, 2018) Update on .NET Core 3.0 and .NET Framework 4.8 | .NET Blog: https://blogs.msdn.microsoft.com/dotnet/2018/10/04/update-on-net-core-3-0-and-net-framework-4-8/
- (Oct 29, 2018) A first look at changes coming in ASP.NET Core 3.0 | ASP.NET Blog: https://blogs.msdn.microsoft.com/webdev/2018/10/29/a-first-look-at-changes-coming-in-asp-net-core-3-0/
ASP .NET Core 2.2 Tutorials, Samples and Docs:
- Announcing ASP.NET Core 2.2: https://blogs.msdn.microsoft.com/webdev/2018/12/04/asp-net-core-2-2-available-today/
- Code samples: https://github.com/bradygaster/DispatchR/tree/master/src/DispatchR.Api
- Healthchecks: https://blogs.msdn.microsoft.com/webdev/2018/08/22/asp-net-core-2-2-0-preview1-healthcheck/
- Open API Analyzers & Conventions: https://blogs.msdn.microsoft.com/webdev/2018/08/23/asp-net-core-2-20-preview1-open-api-analyzers-conventions/
- API Analyzers for improved WEB API documentation: https://www.talkingdotnet.com/asp-net-core-2-2-api-analyzers/
- ASP .NET Core 2.2 video from Connect(); 2018: https://www.youtube.com/watch?v=_vw3hcnSA1Y
- New Features .md docs on Github: https://github.com/dotnet/roslyn/tree/features/NullableReferenceTypes/docs/features
- Announcing Entity Framework Core 2.2 | .NET Blog: https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-entity-framework-core-2-2/
Open API (Swagger) References
- Intro with 2.1: https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-2.1
- 2.2 Update: https://blogs.msdn.microsoft.com/webdev/2018/08/23/asp-net-core-2-20-preview1-open-api-analyzers-conventions/
- Swashbuckle on NuGet: https://www.nuget.org/packages/swashbuckle.aspnetcore/
- NSwag on NuGet: https://www.nuget.org/packages/NSwag.AspNetCore/
- 2.2 API Analyzers: https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Api.Analyzers
- Docs for API Analyzers: https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/analyzers?view=aspnetcore-2.2&tabs=visual-studio
ASP .NET Core 3.0
- Server-Side Blazor to Ship in .NET Core 3.0 — Visual Studio Magazine: https://visualstudiomagazine.com/articles/2018/10/03/blazor-update.aspx?m=1
- Server-Side Blazor to Ship in .NET Core 3.0: https://www.techiexpert.com/server-side-blazor-to-ship-in-net-core-3-0/
- ASP.NET Core 3.0 will only run on .NET Core: https://github.com/aspnet/Announcements/issues/324
- Breaking changes to Microsoft.AspNetCore.App in 3.0 · Issue #325 · aspnet/Announcements
https://github.com/aspnet/Announcements/issues/325
C# 8.0 Tutorials, Samples and Docs:
- Building C# 8.0: https://blogs.msdn.microsoft.com/dotnet/2018/11/12/building-c-8-0/
- Take C# 8.0 for a spin: https://blogs.msdn.microsoft.com/dotnet/2018/12/05/take-c-8-0-for-a-spin/
- Nullable Ref Types, etc: https://msdn.microsoft.com/en-us/magazine/mt829270.aspx
- Async Streams : https://neelbhatt.com/2017/08/26/c-8-0-expected-features-part-i/
- Default Interface Method Implementations: https://neelbhatt.com/2017/09/22/c-8-0-expected-features-part-ii-implementation-of-method-in-the-interface/
- Indexes and Ranges: https://neelbhatt.com/2018/05/26/c-8-0-expected-features-part-iv-something-new-for-indexes-and-ranges/
References for Microsoft Connect(); (Dec 4, 2018)
- Microsoft Connect(); 2018: Empowering every developer to achieve more: https://news.microsoft.com/connect-2018/
- [PDF] BOOK OF NEWS CONNECT 2018: https://news.microsoft.com/uploads/prod/sites/543/2018/12/Connect-2018-Book-of-News.pdf
- Announcing .NET Core 3 Preview 1 and Open Sourcing Windows Desktop Frameworks | .NET Blog: https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-3-preview-1-and-open-sourcing-windows-desktop-frameworks/
- Announcing Open Source of WPF, Windows Forms, and WinUI at Microsoft Connect(); 2018 – Windows Developer BlogWindows Developer Blog: https://blogs.windows.com/buildingapps/2018/12/04/announcing-open-source-of-wpf-windows-forms-and-winui-at-microsoft-connect-2018/
- Making every developer more productive with Visual Studio 2019 | The Visual Studio Blog: https://blogs.msdn.microsoft.com/visualstudio/2018/12/04/making-every-developer-more-productive-with-visual-studio-2019/
- Introducing CNAB: a cloud-agnostic format for packaging and running distributed applications: https://open.microsoft.com/2018/12/04/announcing-cnab-cloud-agnostic-format-packaging-running-distributed-applications/
- Announcing .NET Core 2.2 | .NET Blog: https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-2-2/
- Announcing .NET Core 3 Preview 1 and Open Sourcing Windows Desktop Frameworks | .NET Blog: https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-3-preview-1-and-open-sourcing-windows-desktop-frameworks/
- Demo Projects: https://github.com/Microsoft/TailwindTraders/blob/master/README.md
6 thoughts on “Exploring .NET Core 3.0 and the Future of C# with ASP .NET Core”