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>(); } } }
文章列表