Why is a code change required when migrating from DNX to the .NET CLI?

Today, the RC1 release for ASP.NET Core works with DNX. As I understand it, the main change for RC2 will be that ASP.NET Core will start working with the .NET Core CLI.

Now this has led you to wonder about the following: if the DNX and .NET CLI are just tools, why does this migration require code changes?

In fact, today the announcement Microsoft.AspNetCore.Mvc.Dnx appeared that requires Mvc in RC2 to work with Dnx , on which we see that to use ASP.NET Core MVC with DNX we need to add one package and more, we need to change our code so that we use the ConfigureServices Startup method to call services.AddMvcDnx();

It bothers me. I realized that DNX and the .NET Core CLI were just tools for running .NET Core applications. If it's just a tool, why does switching from one to another require code changes?

+5
source share
2 answers

It bothers me. I realized that DNX and the .NET Core CLI were just tools for running .NET Core applications. If it's just a tool, why does switching from one to another require code changes?

DNVM / DNU / DNX was not just a tool. DNX was also a runtime. He was responsible for loading the CLR and invoking your application. It also meant that he had a lot of information about runtime and application, such as dependencies, environment, etc. This information was available to the application through various services that you could enter, such as IRuntimeEnvironment , IApplicationEnvironment and ILibraryManager .

In turn, MVC has a service called IAssemblyProvider . This is responsible for providing assemblies where MVC should, in particular, look for controllers. By default, this was based on ILibraryManager , which is a DNX-specific service. This means that it will no longer work if you switch to a dotnet-based runtime that is bundled in packages, instead of using a separate tool such as DNVM.

To fix this, the MVC team first began to work based on DNX services and a newer, more targeted alternative ( Microsoft.Extensions.DependencyModel ). You can see the code here . It basically checks if the DNX-specific ILibraryManager , and if not, it returns to the alternative dotnet-API.

The problem with this approach is that it adds extra and in most cases redundant dependencies ( Microsoft.Extensions.PlatformAbstractions.Dnx ) to MVC when most people start using it with dotnet tools and runtimes. Remember; DNX etc. It remains beta and RTM will be released.

Instead, they chose the current solution; have a separate package Microsoft.AspNetCore.Mvc.Dnx , which contains, among other things, a DNX-based IAssemblyProvider for MVC. You can see what the AddMvcDnx method does here .

This means that the few people who follow the pre-release versions will have to make some changes to their code in order to still work in DNX (although I would move to dotnet ASAP), while new people will only need to call AddMvc , as usual.

I hope some of them made sense. This can be really confusing :)

+8
source

This is not DNX for the dotnet cli switch, which requires code changes. This is RC1 - RC2. As you can see in the rc2 announcements , 31 of 34 announcements are about violation of changes.

All packages and associated spaces have been changed, from Microsoft.AspNet.* To Microsoft.AspNetCore.* , The same for entity infrastructure.

If you had RC2 on dnx, and then switched to dotnet cli, there would not be many code changes (if any), except for the way the application starts.

There are no more commands in dotnet cli, and most likely they will not return. For dotnet cli, you need to explicitly run an application like this:

  public static void Main(string[] args) { var host = new WebHostBuilder() .UseServer("Microsoft.AspNetCore.Server.Kestrel") .UseStartup<Startup>() .Build(); host.Start(); } 

It is still not released, so still expect code changes.

Digging a bit on GitHub (source and problems), I got this here:

RC1 and early RC2 builds used to inject IMvcRazorHost into an ICompilationService implementation that is no longer available. RazorLoadContext is now used, as you can see in the new commit package .

Further we see that it targets DOTNET5_6, which is a new nickname, indicating a new platform level (dotnet 5.1 equals netstandard 1.0, dotnet54 equals 1.3, so dotnet56 will equal netstandard 1.5).

This question here points to a switch to dotnet56 / DOTNET5_6 .

+5
source

Source: https://habr.com/ru/post/1244075/


All Articles