.NET跨平台之旅:成功将示例站点升级至ASP.NET Core RC2
ASP.NET Core RC2 终于发布了( Announcing ASP.NET Core RC2 )。为了庆祝这次发布,我们将运行在 Ubuntu 服务器上的示例站点 about.cnblogs.com 升级到了 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" } }
详见博问:http://q.cnblogs.com/q/82384/
三、运行 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,详见 http://q.cnblogs.com/q/82388/ 。
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-*" ,详见 http://q.cnblogs.com/q/82391/
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-*" ,详见 http://q.cnblogs.com/q/82395/
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<Startup>() .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<EfDbContext>(options => { options.UseSqlServer(Configuration["data:ConnectionString"]); }); services.AddTransient<ITabNavRepository, TabNavRepository>(); services.AddTransient<ITabNavService, TabNavService>(); } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· DeepSeek本地性能调优
· autohue.js:让你的图片和背景融为一体,绝了!
· 10亿数据,如何做迁移?
2015-05-18 上周热点回顾(5.11-5.17)
2014-05-18 云计算之路-阿里云上:对“黑色n秒”问题的最终猜想——CPU C-states引起的