. NET Cross-platform tour: Upgrade the sample site from ASP. RC1 to ASP. 1.0

Source: Internet
Author: User
Tags dotnet hosting webhost

Finally will ". NET Cross-platform Tour "sample site about.cnblogs.com from ASP. RC1 to ASP. 5, it has been a bit of a struggle to record this in this blog post.

The biggest change from ASP. 5 to ASP., in addition to renaming, replaces DNX with the dotnet CLI (the command name is dotnet). Therefore, to run the ASP. NET Core program, first install the dotnet CLI, which we installed with the Apt-get install dotnet command on the Ubuntu server.

The command to run the ASP. NET 5 program is the DNX Restore + DNX web, and the command that runs the ASP. NET Core program changes to dotnet Restore + dotnet run. Dotnet running the ASP. DNX is a big difference, and in addition to Project.json and Startup.cs positions, a Program.cs is needed.

To run an ASP. DNX 5 program, you need to configure the appropriate command in the 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 required, but Program.cs is responsible for it, such as the Program.cs code used in our example project is as follows:

usingSystem.IO;usingMicrosoft.AspNetCore.Hosting;usingMicrosoft.AspNetCore.Builder;namespacecnblogs.aboutus.web{ Public classProgram { Public Static voidMain (string[] args) {            varHost =NewWebhostbuilder (). Useserver ("Microsoft.AspNetCore.Server.Kestrel")                        . Useurls ("http://*:8001")                        . Useapplicationbasepath (Directory.GetCurrentDirectory ()). UseDefaultConfiguration (args). Useiisplatformhandlerurl (). Usestartup<Startup>()                        .            Build (); Host.        Run (); }    }}

As can be seen from the above code, the launch of the ASP. NET Core application started by Webhostbuilder (source), but it is not the protagonist, just assistant, prepare some startup parameters, and finally put the boot work to the real protagonist--webhost, if you webhost How to work interested, you can see its source code.

After Program.cs, the next step is physical activity-renaming.

    • Entityframeworkcore.microsoftsqlserver Change to Microsoft.EntityFrameworkCore.SqlServer
    • Microsoft.AspNet.Builder Change to Microsoft.AspNetCore.Builder
    • Microsoft.Data.Entity Change to Microsoft.entityframeworkcore
    • MICROSOFT.ASPNET.MVC Change to MICROSOFT.ASPNETCORE.MVC
    • Microsoft.AspNet.Html.Abstractions Change to Microsoft.AspNetCore.Html
    • Removing the Microsoft.Dnx.Runtime namespace
    • Wait a minute

After the "renaming" of physical activity, the next job is the most laborious--configuration Project.json, and now the Project.json does not support annotations, commissioning configuration becomes more cumbersome.

First of all in the Project.json to add the following emitentrypoint configuration, the Dnx period does not add is OK, now can not.

"Compilationoptions": {        "Emitentrypoint": true}

The first problem encountered is dotnet restore with a not compatible with dnxcore,version=v5.0 error ... It was later resolved by adding the following configuration to the Project.json, but so far it has not been possible to figure out why adding the seemingly unrelated configuration solves the problem (or just superficially).

"Tools": {      "Dotnet-publish-iis": "1.0.0-*"}

A second problem encountered is the dependency Ix-async 1.2.5 does not the support framework dnxcore,version=v5.0. This problem is related to the Entity Framework, so long as the "Microsoft.EntityFrameworkCore.SqlServer" is removed from the dependencies of Project.json, the problem disappears. Later, referring to the source code of the Entity Framework, the following configuration was added in Project.json to resolve the problem:

"netstandard1.3": {      "imports": [        "dotnet5.4",        "Portable-net452+win81"      ]}

The next problem is the ASP. NET Core MVC routing match problem, and when you run the site with dotnet run, there is a 404 error accessing any URL. This is a problem to start with, because from the code in Startup.cs, there is no problem with the configuration of MVC. Later it was doubtful that it might be project.json, so I compared the project.json in the DOTNET-CLI example Project Cli-samples, and tried to add the following configuration, The problem was miraculously solved (this configuration was not going to be studied further).

{"    compilationoptions": {        "Preservecompilationcontext": True    }}

The last question is the most silent, the problem is the access to the ASP. NET Core MVC site error: Could not load file or assembly ' Microsoft.Win32.Registry '. Not only does our project have this problem, but also the HELLOMVC project in Cli-samples. The problem occurs in Microsoft.AspNetCore.DataProtection , and DataProtectionServices.cs does cite Microsoft.Win32.Registry, but we are running on Linux, don't Microsoft.AspNetCore.DataProtection currently support cross-platform?

The entire upgrade process is stuck here, and when we are ready to temporarily abandon the upgrade to ASP. NET Core 1.0, we discovered yesterday that the Prject.json in Cli-samples was updated, and then tried to run the HELLOMVC project, the problem magically solved. Immediately look at the corresponding git commit:

The original netstandard.library was deleted in Dependecies, and the netstandardapp1.3 configuration was added to frameworks. So, according to this modification of the Project.json of our project, the problem is solved immediately, our. NET cross-platform tour of the sample site About.cnblogs.com also successfully run up, the upgrade has finally been completed successfully.

Share the three files in this sample 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:

namespacecnblogs.aboutus.web{ Public classStartup { PublicStartup (iapplicationenvironment appenv) {Iconfigurationbuilder builder=NewConfigurationbuilder (). Setbasepath (Appenv.applicationbasepath). Addjsonfile ("Config.json",false); Configuration=Builder.        Build (); }         PublicIConfiguration Configuration {Get;Set; }  Public voidConfigure (Iapplicationbuilder app, Iloggerfactory loggerfactory) {loggerfactory.addconsole (logleve            L.debug); App.            Usedeveloperexceptionpage (); App.            Usemvcwithdefaultroute (); App.            Usestaticfiles (); App.        Useruntimeinfopage (); }         Public voidconfigureservices (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/>    <AddKey= "Aspnetci"value= "Https://www.myget.org/F/aspnetcidev/api/v3/index.json" />    <AddKey= "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

. NET Cross-platform tour: Upgrade the sample site from ASP. RC1 to ASP. 1.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.