Most recently, we updated ASP.NET Core to version 2.1. One of the creators of the platform, our colleague David Fowler, shared some of its useful features that few people know about. In addition, in addition to the 9 secret features of ASP.NET Core, under the cat you will find a list of major innovations with all useful links. Join now!

Version 2.1 of the open-source ASP.NET Core platform has been released, and developer David Fowler shared
on Twitter with some useful features that few people know about. Of course, the platform is now faster, but there are also a number of new features and best practices that are worth considering in more detail.
ASP.NET Core now works with a new hosting model. .NET applications build and
run a host .
The host is responsible for running applications and managing their life cycle. The task of the Generic Host node is to separate the HTTP pipelining from the web host API in order to be able to create more scripts on the host. Messaging, background and other non-HTTP tasks work better thanks to Generic Host end-to-end features such as configuration, dependency injection (DI), and logging.
This means that now for scripts without web hosting there is not only WebHost, but also Generic Host. In this case, the work will be as convenient as with ASP.NET Core, but you will also have new features, such as DI, logging and configuration. Sample code for Generic Host
can be found on GitHub .
With it, you can run long background operations both in the universal host and in your web applications. ASP.NET Core 2.1
introduced support for the base class BackgroundService, which makes it much easier to create a long asynchronous loop. Sample code for the hosted service is also
on github .
Create a simple temporary background task:
public Task StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("Timed Background Service is starting."); _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(5)); return Task.CompletedTask; }
easy!
Now you can host ASP.NET Core inside a Windows service! Many users asked to add this feature. IIS you no longer need to post whatever you want. Take a look at
Microsoft.AspNetCore.Hosting.WindowsServices on NuGet and
detailed documentation on hosting your own ASP.NET Core application on Windows as a Windows service without IIS.
public static void Main(string[] args) { var pathToExe = Process.GetCurrentProcess().MainModule.FileName; var pathToContentRoot = Path.GetDirectoryName(pathToExe); var host = WebHost.CreateDefaultBuilder(args) .UseContentRoot(pathToContentRoot) .UseStartup<Startup>() .Build(); host.RunAsService(); }
As always, a simple and accurate implementation using a source on GitHub.
[assembly: HostingStartup(typeof(SampleStartups.StartupInjection))]
It will be interesting to you, be sure to pay attention to them. You can create packages that are used as an aid to source code distributed through shared source. Between ourselves, we call them “shared source packages”. They are
used in ASP.NET Core everywhere , when something needs to be shared, but it should not be publicly available through the API. Then your code will be used, but there will be no dependencies on the final package.
They are used in CSPROJ
in this way . Notice the
PrivateAssets attribute:
<PackageReference Include="Microsoft.Extensions.ClosedGenericMatcher.Sources" PrivateAssets="All" Version="" /> <PackageReference Include="Microsoft.Extensions.ObjectMethodExecutor.Sources" PrivateAssets="All" Version="" />
If you need to use a method in a type through reflection, and this method can be asynchronous, then the optimized and flexible
ObjectMethodExecutor function that we use throughout the ASP.NET Core codebase will help you.
The command uses this code in MVC to use your controller methods. They use this code in SignalR to invoke hub methods. It works with synchronous and asynchronous methods. It can also work with custom expected objects and asynchronous F # workflows.
A small and often requested method. If you do not like what is issued after starting the dotnet driver, when you host a web application (print the binding information), then you can use the new SuppressStatusMessages extension method.
WebHost.CreateDefaultBuilder(args) .SuppressStatusMessages(true) .UseStartup<Startup>();
In version 2.1 it has become easier to configure the parameters for which the services are required. Previously, you had to create a type obtained using IConfigureOptions, now you can do all this in ConfigureServices using AddOptions ‹TOptions›
public void ConfigureServicdes(IServiceCollection services) { services.AddOptions<MyOptions>() .Configure<IHostingEnvironment>((o,env) => { o.Path = env.WebRootPath; }); }
Usually there is no need to configure IHttpContext, but many want to know
how to do this , and some believe that this should be done automatically. It is not registered by default, since its presence leads to a decrease in performance. However, in ASP.NET Core 2.1
, PR was added to the extension method, which
will ease the process if you want.
services.AddHttpContextAccessor ();
So, ASP.NET Core 2.1
is ready for release .
New features in this version:
See the
list of changes in ASP.NET Core 2.1 in ASP.NET Core documents to learn more about these features. A complete list of all the changes in the new version is presented in
the release notes .
Try it! Take
the QuickStart course - and you can create basic web applications in 10 minutes.