. NET cross-platform tour: Upgrade the sample site from ASP. NET 5 RC1 to ASP. NET Core 1.0 and rc1core
Finally, I upgraded the ". NET cross-platform tour" example site about.cnblogs.com from ASP. NET 5 RC1 to ASP. NET Core 1.0, which has gone through many twists and turns. I will record it in this blog post.
The biggest change from ASP. NET 5 to ASP. NET Core is to replace dnx with dotnet cli (the command name is dotnet. Therefore, to run the ASP. NET Core program, install dotnet cli first. we installed it with the apt-get install dotnet command on the Ubuntu server.
The command for running ASP. NET 5 is dnx restore + dnx web, and the command for running ASP. NET Core program is changed to dotnet restore + dotnet run. Dotnet has a big difference between running ASP. NET programs and dnx. In addition to project. json and Startup. cs jobs, a Program. cs job is also required.
To run the ASP. NET 5 program with dnx, you must configure the corresponding command in project. json, for example:
"commands":{ "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://*:8001"}
In ASP. NET Core, this command is no longer needed, but assigned to Program. cs. For example, the Program. cs code used in this 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") .UseApplicationBasePath(Directory.GetCurrentDirectory()) .UseDefaultConfiguration(args) .UseIISPlatformHandlerUrl() .UseStartup<Startup>() .Build(); host.Run(); } }}
From the code above, we can see that ASP. the start of the NET Core application is started by WebHostBuilder (source code), but it is not the main character, just an assistant. Prepare some startup parameters and finally deliver the startup work to the real protagonist-WebHost, if you are interested in how the WebHost works, you can view its source code.
After Program. cs is completed, the next step is to change the name of the physical activity.
- 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.
- Microsoft. AspNet. Html. Modify actions to Microsoft. AspNetCore. Html.
- Remove the Microsoft. Dnx. Runtime namespace
- And so on.
After the "RENAME" physical activity is completed, the next job will be the most cost-effective and tiring-configuring project. json, and the current project. json does not support annotations, making debugging and configuration more troublesome.
First, add the following emitEntryPoint configuration in project. json. You can do this without adding a dnx during the dnx period.
"compilationOptions": { "emitEntryPoint": true}
The first problem encountered was the not compatible with DNXCore, Version = v5.0 error in dotnet restore... Later, the following configuration was added to the project. json file, but it still failed to figure out why adding seemingly unrelated configuration could solve the problem (or simply solve it on the surface ).
"tools": { "dotnet-publish-iis": "1.0.0-*"}
The second problem encountered was The dependency Ix-Async 1.2.5 does not support framework DNXCore, Version = v5.0. This issue is related to Entity Framework. If you remove "Microsoft. EntityFrameworkCore. SqlServer" from dependencies of project. json, the issue disappears. Later, refer to the source code of Entity Framework and add the following configuration in project. json to solve the problem:
"netstandard1.3": { "imports": [ "dotnet5.4", "portable-net452+win81" ]}
The following problem occurs: ASP. NET Core MVC routing matching. After the website is run using dotnet run, the 404 error occurs when accessing any URL. This is a difficult problem, because from the code in Startup. cs, the MVC configuration has no problems. Later, I still suspected that it may be a project. json. json for comparison, tried to add the following configuration, the problem unexpectedly miraculously solved (this configuration was not further studied at the time ).
{ "compilationOptions": { "preserveCompilationContext": true }}
The last problem was the least confusing. The problem was that an error occurred while accessing the ASP. NET Core MVC site: cocould not load file or assembly 'Microsoft. Win32.Registry '. Not only does our project have this problem, but the HelloMvc project in cli-samples also has this problem. The problem occurs in Microsoft. aspNetCore. dataProtection, and DataProtectionServices. cs does reference Microsoft. win32.Registry, but we are running on Linux. Is it Microsoft. aspNetCore. does DataProtection currently not support cross-platform?
The entire upgrade process is stuck here. When we are about to temporarily stop upgrading to ASP. NET Core 1.0, the prject in cli-samples was found yesterday. json updated, and then tried to run the HelloMvc project. The problem was solved magically. Take a look at the corresponding git commit:
Originally, NETStandard. Library was deleted in dependecies and the netstandardapp1.3 configuration was added to frameworks. As a result, the project. json in our project was modified according to this, and the problem was solved immediately. The example site about.cnblogs.com Of Our. NET cross-platform tour was successfully run, and the upgrade was completed.
Share the three files in this example project:
Project. json:
{ "compilationOptions": { "preserveCompilationContext": true, "emitEntryPoint": true }, "dependencies" : { "Microsoft.Extensions.Logging.Console": "1.0.0-*", "Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-*", "Microsoft.AspNetCore.HttpOverrides": "1.0.0-*", "Microsoft.AspNetCore.Mvc": "1.0.0-*", "Microsoft.AspNetCore.StaticFiles": "1.0.0-*", "Microsoft.AspNetCore.Diagnostics": "1.0.0-*", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*", "System.Runtime.Serialization.Primitives": "4.1.0-*", "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-*" }, "frameworks": { "netstandardapp1.3": { "dependencies": { "NETStandard.Library": "1.0.0-*" }, "imports": [ "dnxcore50", "portable-net45+win8" ] } }, "tools": { "dotnet-publish-iis": "1.0.0-*" }}
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<EfDbContext>(options => { options.UseSqlServer(Configuration["data:ConnectionString"]); }); services.AddTransient<ITabNavRepository, TabNavRepository>(); services.AddTransient<ITabNavService, TabNavService>(); } }}
NuGet. Config:
<configuration> <packageSources> <clear /> <add key="AspNetCI" value="https://www.myget.org/F/aspnetcidev/api/v3/index.json" /> <add key="NuGet.org" value="https://api.nuget.org/v3/index.json" /> </packageSources></configuration>
Reference page:
Http://www.yuanjiaocheng.net/mvc/mvc-helper-hiddenfield.html
Http://www.yuanjiaocheng.net/entity/dbset-class.html
Http://www.yuanjiaocheng.net/CSharp/csharp-class.html
Http://www.yuanjiaocheng.net/CSharp/Csharp-queue.html
Http://www.yuanjiaocheng.net/webapi/web-api-controller.html
Http://www.yuanjiaocheng.net/mvc/mvc-architecture.html
Http://www.yuanjiaocheng.net/mvc/action-filters-in-mvc.html
Http://www.yuanjiaocheng.net/CSharp/csharp-extension-method.html
Http://www.yuanjiaocheng.net/webapi/parameter-binding.html
Http://www.yuanjiaocheng.net/mvc/mvc-helper-textbox.html
Http://www.yuanjiaocheng.net/ASPNET-CORE/core-setup-entityframework.html