.NET跨平台之旅:成功将示例站点升级至ASP.NET Core RC2
发布日期:2021-05-09 01:35:09 浏览次数:17 分类:博客文章

本文共 6612 字,大约阅读时间需要 22 分钟。

ASP.NET Core RC2 终于发布了( )。为了庆祝这次发布,我们将运行在 Ubuntu 服务器上的示例站点  升级到了 ASP.NET Core RC2 ,在这篇博文中分享一下我们在升级过程中遇到的问题。

一、安装.NET Core SDK

删除旧版 dotnet cli:

rm -rf /usr/share/dotnet

安装 .NET Core SDK:

curl -sSL https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain/dotnet-install.sh | bash /dev/stdin --version 1.0.0-preview1-002702 --install-dir ~/dotnet
sudo ln -s ~/dotnet/dotnet /usr/local/bin

二、运行 dotnet restore 命令

遇到2个错误:

1) Unable to resolve 'Microsoft.AspNetCore.IISPlatformHandler (>= 1.0.0)' for '.NETCoreApp,Version=v1.0' 

解决方法:在 project.json 中将 Microsoft.AspNetCore.IISPlatformHandler 改为 Microsoft.AspNetCore.Server.IISIntegration 。

2) Package Newtonsoft.Json 7.0.1 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0) 

解决方法:在 project.json 中将

"tools": {    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-*"}

改为

"tools": {    "Microsoft.AspNetCore.Server.IISIntegration.Tools":{        "version":"1.0.0-*",        "imports": "portable-net45+win8+dnxcore50"    }}

详见博问:

三、运行 dotnet run 命令

遇到了不少错误:

1)

The type or namespace name 'IApplicationEnvironment' could not be found (are you missing a using directive or an assembly reference?)

解决方法:将 Startup.cs 中的 IApplicationEnvironment 改为 IHostingEnvironment,详见 。

2)

'IWebHostBuilder' does not contain a definition for 'UseDefaultHostingConfiguration' and no extension method 'UseDefaultHostingConfiguration' accepting a first argument of type 'IWebHostBuilder' could be found (are you missing a using directive or an assembly reference?)

解决方法:在 Program.cs 中删除 .UseDefaultHostingConfiguration(args)

3)

'IWebHostBuilder' does not contain a definition for 'UseIISPlatformHandlerUrl' and no extension method 'UseIISPlatformHandlerUrl' accepting a first argument of type 'IWebHostBuilder' could be found

解决方法:在 Program.cs 中将 .UseIISPlatformHandlerUrl() 改为 UseIISIntegration()

4)

'ConfigurationBuilder' does not contain a definition for 'SetBasePath' and no extension method 'SetBasePath' accepting a first argument of type 'ConfigurationBuilder' could be found

解决方法:在 project.json 的 dependencies 中添加配置: "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*" ,详见

5)

'IConfigurationBuilder' does not contain a definition for 'AddJsonFile' and no extension method 'AddJsonFile' accepting a first argument of type 'IConfigurationBuilder' could be found

解决方法:在 project.json 的 dependencies 中添加配置: "Microsoft.Extensions.Configuration.Json": "1.0.0-*" ,详见

6)

The type or namespace name 'IRuntimeEnvironment' does not exist in the namespace 'Microsoft.Extensions.PlatformAbstractions'

解决方法:在 _Layout.cshtml 中删除 @inject Microsoft.Extensions.PlatformAbstractions.IRuntimeEnvironment env ,添加命名空间 @using Microsoft.Extensions.PlatformAbstractions 与代码 @{ var env = PlatformServices.Default.Runtime; }

四、代码改进

在 Program.cs 中将 .UseServer("Microsoft.AspNetCore.Server.Kestrel") 改为 .UseKestrel()

五、project.json、Program.cs、Startup.cs中的完整代码

1)project.json

{   "compilationOptions": {        "preserveCompilationContext": true,        "emitEntryPoint": true    },    "dependencies": {        "Microsoft.Extensions.Logging.Console": "1.0.0-*",        "Microsoft.AspNetCore.Server.IISIntegration": "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-*",        "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-*",        "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*",        "Microsoft.Extensions.Configuration.Json": "1.0.0-*",        "Microsoft.NETCore.App": {            "type": "platform",            "version": "1.0.0-*"        }    },    "frameworks": {      "netcoreapp1.0": {        "imports": [          "portable-net45+wp80+win8+wpa81+dnxcore50",          "portable-net45+win8+wp8+wpa81",          "portable-net45+win8+wp8"        ]      }    },    "tools": {        "Microsoft.AspNetCore.Server.IISIntegration.Tools":{            "version": "1.0.0-*",            "imports": "portable-net45+win8+dnxcore50"        }    }}

2)Program.cs

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()                        .UseKestrel()                        .UseUrls("http://*:8001")                        .UseContentRoot(Directory.GetCurrentDirectory())                        .UseIISIntegration()                        .UseStartup
() .Build(); host.Run(); } }}

3)Startup.cs

using System;using System.IO;using System.Linq;using Microsoft.AspNetCore.Builder;using Microsoft.Extensions.DependencyInjection;using Microsoft.EntityFrameworkCore;using CNBlogs.AboutUs.Data;using Microsoft.Extensions.PlatformAbstractions;using Microsoft.Extensions.Configuration;using System.Data.SqlClient;using Microsoft.Extensions.Logging;using CNBlogs.AboutUs.Application;using Microsoft.AspNetCore.Hosting;namespace CNBlogs.AboutUs.Web{    public class Startup    {        public Startup(IHostingEnvironment hostingEnv)        {            IConfigurationBuilder builder = new ConfigurationBuilder()                .SetBasePath(hostingEnv.ContentRootPath)                .AddJsonFile("config.json", false);            Configuration = builder.Build();        }        public IConfiguration Configuration { get; set; }        public void Configure(IApplicationBuilder app,            ILoggerFactory loggerFactory,            IHostingEnvironment env)        {            if(env.IsDevelopment())            {                loggerFactory.AddConsole(LogLevel.Debug);            }            else            {                loggerFactory.AddConsole(LogLevel.Error);            }            app.UseDeveloperExceptionPage();            app.UseMvcWithDefaultRoute();            app.UseStaticFiles();            app.UseRuntimeInfoPage();            if(env.IsDevelopment())            {                app.UseDeveloperExceptionPage();            }        }        public void ConfigureServices(IServiceCollection services)        {            services.AddMvc();            services.AddEntityFrameworkSqlServer()                .AddDbContext
(options => { options.UseSqlServer(Configuration["data:ConnectionString"]); }); services.AddTransient
(); services.AddTransient
(); } }}
上一篇:上周热点回顾(5.16-5.22)
下一篇:上周热点回顾(5.9-5.15)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月24日 21时17分20秒