.NETプラットフォーム間の旅:サンプルサイトをASP.にアップグレードしました.NET Core RC2

11992 ワード

ASP.NET Core RC 2がついに発表された(Announcing ASP.NET Core RC 2).今回のリリースを祝うために、Ubuntuサーバ上のサンプルサイトaboutを実行します.cnblogs.comはASPにアップグレードしました.NET Core RC 2では、アップグレード中に発生した問題をこのブログで共有します.
一、取り付け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;}です.
四、コードの改善
プログラムでcs中将UseServer(「Microsoft.AspNetCore.Server.Kestrel」)を.UseKestrel() .
五、プロジェクト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>();
        }
    }
}