In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to upgrade the site from ASP.NET 5 RC1 to ASP.NET Core 1.0". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The biggest change from ASP.NET 5 to ASP.NET Core, in addition to renaming, is to replace dnx with dotnet cli (command name is dotnet). So to run the ASP.NET Core program, first install dotnet cli, which we installed with the apt-get install dotnet-nightly command on the Ubuntu server.
The command to run the ASP.NET 5 program is dnx restore + dnx web, and the command to run the ASP.NET Core program becomes dotnet restore + dotnet run. There is a big difference between running ASP.NET programs in dotnet and dnx in that in addition to project.json and Startup.cs positions, a Program.cs is required.
To run an ASP.NET 5 program with dnx, you need to configure the corresponding command in project.json, such as:
"commands": {"web": "Microsoft.AspNet.Hosting-server Microsoft.AspNet.Server.Kestrel-server.urls http://*:8001"}
In ASP.NET Core, the command is no longer needed, but is left to Program.cs. For example, the Program.cs code used in our example project is as follows:
Using System.IO;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Builder Namespace CNBlogs.AboutUs.Web {public class Program {public static void Main (string [] args) {var host = new WebHostBuilder () .UseServer ("Microsoft.AspNetCore.Server.Kestrel") .UseUrls ("http://*:8001") .UseApplication BasePath (Directory.GetCurrentDirectory () .UseDefaultConfiguration (args) .UseIISPlatformHandlerUrl () .UseStartup () .build () Host.Run ();}
As can be seen from the above code, the startup work of ASP.NET Core applications starts from WebHostBuilder (source code), but it is not the protagonist, just an assistant, prepare some startup parameters, and finally give the startup work to the real protagonist-WebHost. If you are interested in how WebHost works, you can look at its source code.
After getting the Program.cs done, the next step is manual work-- renaming.
Change EntityFrameworkCore.MicrosoftSqlServer to Microsoft.EntityFrameworkCore.SqlServer
Change Microsoft.AspNet.Builder to Microsoft.AspNetCore.Builder
Change Microsoft.Data.Entity to Microsoft.EntityFrameworkCore
Change Microsoft.AspNet.Mvc to Microsoft.AspNetCore.Mvc
Change Microsoft.AspNet.Html.Abstractions to Microsoft.AspNetCore.Html
Remove Microsoft.Dnx.Runtime Namespace
Wait
After completing the manual work of "renaming", the next task is the most laborious and tiring-configuring project.json, and now project.json does not support annotations, which makes it more troublesome to debug the configuration.
First of all, you need to add the following emitEntryPoint configuration to project.json. It is OK not to add it during the dnx period, but not now.
"compilationOptions": {"emitEntryPoint": true}
The first problem encountered is a not compatible with DNXCore,Version=v5.0 error in dotnet restore. Later, it was solved by adding the following configuration to project.json, but it is still not clear why adding this seemingly irrelevant configuration can solve the problem (or only superficially).
"tools": {"dotnet-publish-iis": "1.0.0 muri *"}
The second problem encountered is The dependency Ix-Async 1.2.5 does not support framework DNXCore,Version=v5.0. This problem is related to EntityFramework. As long as you remove "Microsoft.EntityFrameworkCore.SqlServer" from project.json 's dependencies, the problem disappears. Later, referring to the source code of Entity Framework, add the following configuration to project.json to solve the problem:
"netstandard1.3": {"imports": ["dotnet5.4", "portable-net452+win81"]}
The next problem encountered is the ASP.NET Core MVC route matching problem. After running the site with dotnet run, there is a 404 error accessing any URL. This is a difficult problem to start with, because judging from the code in Startup.cs, there is nothing wrong with the configuration of MVC. Later, I suspected that it might be a problem with project.json, so I compared it with project.json in dotnet-cli 's sample project, cli-samples, and tried to add the following configuration, and the problem was miraculously solved (this configuration was not further studied at that time).
{"compilationOptions": {"preserveCompilationContext": true}}
The last question is the most speechless, and the problem is that there was an error accessing the ASP.NET Core MVC site: Could not load file or assembly 'Microsoft.Win32.Registry'. This is a problem not only with our project, but also with the HelloMvc project in cli-samples. The problem occurs in Microsoft.AspNetCore.DataProtection, and Microsoft.Win32.Registry is indeed referenced in DataProtectionServices.cs, but we run on Linux, so doesn't Microsoft.AspNetCore.DataProtection currently support cross-platform support?
The whole upgrade process got stuck here, and when we were about to temporarily abandon the upgrade to ASP.NET Core 1.0, we found that the prject.json in cli-samples had been updated yesterday, and then tried to run the HelloMvc project, and the problem was solved miraculously.
Originally, NETStandard.Library was removed from dependecies and netstandardapp1.3 configuration was added to frameworks. So, following this modification of the project.json in our project, the problem was solved immediately, and the sample site of our .NET cross-platform tour, about.cnblogs.com, was successfully run, and the upgrade was finally successfully completed.
Share three files from this sample project:
Project.json:
{"compilationOptions": {"preserveCompilationContext": true, "emitEntryPoint": true}, "dependencies": {"Microsoft.Extensions.Logging.Console": "1.0.0Mel *", "Microsoft.AspNetCore.IISPlatformHandler": "1.0.0Mel *", "Microsoft.AspNetCore.HttpOverrides": "1.0.0Mel *" "Microsoft.AspNetCore.Mvc": "1.0.0Mui *", "Microsoft.AspNetCore.StaticFiles": "1.0.0MUE *", "Microsoft.AspNetCore.Diagnostics": "1.0.0Mel *", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0MUE *", "System.Runtime.Serialization.Primitives": "4.1.0Mel *" "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0mura *"}, "frameworks": {"netstandardapp1.3": {"dependencies": {"NETStandard.Library": "1.0.0Murray *"}, "imports": ["dnxcore50", "portable-net45+win8"]}} "tools": {"dotnet-publish-iis": "1.0.0Mutual *"}}
Startup.cs:
Namespace CNBlogs.AboutUs.Web {public class Startup {public Startup (IApplicationEnvironment appEnv) {IConfigurationBuilder builder = new ConfigurationBuilder () .SetBasePath (appEnv.ApplicationBasePath) .AddJsonFile ("config.json", false); Configuration = builder.Build ();} public IConfiguration Configuration {get; set } public void Configure (IApplicationBuilder app, ILoggerFactory loggerFactory) {loggerFactory.AddConsole (LogLevel.Debug); app.UseDeveloperExceptionPage (); app.UseMvcWithDefaultRoute (); app.UseStaticFiles (); app.UseRuntimeInfoPage ();} public void ConfigureServices (IServiceCollection services) {services.AddMvc () Services.AddEntityFramework () .AddSqlServer () .AddDbContext (options = > {options.UseSqlServer (Configuration ["data:ConnectionString"]);}); services.AddTransient (); services.AddTransient ();}
NuGet.Config:
That's all for "how to upgrade your site from ASP.NET 5 RC1 to ASP.NET Core 1.0". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.