只有累積,沒有奇蹟

2019年3月18日 星期一

[.NETCore] 'IWebHostBuilder' does not contain a definition for 'ConfigureKestrel' and no accessible extension method 'ConfigureKestrel'

問題
Kestrel web server 是一個輕量級的 Web Server 服務,同事求救在實作 How to use Kestrel in ASP.NET Core apps 其中的 Kestrel option 的 ConfigureKestrel 設定時發生錯誤,顯示不支援 ConfigureKestrel 方法簽章,以下就針對解決此問題的方式說簡單說明,若有問題歡迎提出一起討論或是給予指導。

解決方案
錯誤訊息文字為 : 'IWebHostBuilder' does not contain a definition for 'ConfigureKestrel' and no accessible extension method 'ConfigureKestrel' accepting a first argument of type 'IWebHostBuilder' could be found (are you missing a using directive or an assembly reference?)"  
在確認 從 ASP.NET Core 2.1 至 2.2 移轉 線上文件提到, 2.2 版開始 Kestrel Web Server 設定有所調整,原因是為了避免衝突 IIS 同處理序主控模型,忽然間恍然大悟發現是版本更新造成,下列先列出兩個不同版本所用的 API 簽章差異

ASP.NET Core 2.1
在 .NET Core 2.1 中要設定 Kestrel Web Server 設定的話要用  UseKestrel  方法,使用方式如下
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseKestrel(options =>
        {
            options.Limits.MaxConcurrentConnections = 100;
        });

ASP.NET Core 2.2
在 .NET Core 2.2 中要設定 Kestrel option 則需要使用  ConfigureKestrel  方法,使用方式如下
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureKestrel((context, options) =>
        {
            // Set properties and call methods on options
        });

確認 ASP.NET Core 版本
兩個版本使用方式不同,因此第一步是確認目前同事Visual Studio 使用的 target Framwork 版本為何,可以再專案按右鍵,點選屬性,得知目前使用版本為 2.1 且沒有並未安裝新版 

下載 .NET Core 2.2
安裝傳送門 : 請點我 
點選下載 .NET Core SDK & Runtime,接著進行安裝其安裝檔,無止盡點擊進行下一步

設定 Target Framework
下載完畢 ASP.NET Core 2.2 SDK & Runtime 之後,來確認在  Microsoft.AspNetCore.Hosting  namespace 底下是否有 ConfigureKestrel 方法
/// <summary>
/// Configures Kestrel options but does not register an IServer. See <see cref="M:Microsoft.AspNetCore.Hosting.WebHostBuilderKestrelExtensions.UseKestrel(Microsoft.AspNetCore.Hosting.IWebHostBuilder)" />.
/// </summary>
/// <param name="hostBuilder">
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure.
/// </param>
/// <param name="configureOptions">A callback to configure Kestrel options.</param>
/// <returns>The Microsoft.AspNetCore.Hosting.IWebHostBuilder.</returns>
public static IWebHostBuilder ConfigureKestrel(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, KestrelServerOptions> configureOptions)
{
  if (configureOptions == null)
    throw new ArgumentNullException(nameof (configureOptions));
  return hostBuilder.ConfigureServices((Action<WebHostBuilderContext, IServiceCollection>) ((context, services) => services.Configure<KestrelServerOptions>((Action<KestrelServerOptions>) (options => configureOptions(context, options)))));
}
確定有其方法,重新至專案調整 target Framework 為 2.2 版,在重新建置專案後即可正常 Build 成功,成功解決,打完收工 ! 

後記
在 ASP.NET Core 推行速度相當快,在練習以及查詢網路資料時得先確定方法是否是舊的 API 與應用,才可以避免下次踩到相同地雷,MSDN 官網文章也有提到各版本移轉時的說明,詳細可以參考看 從 ASP.NET Core 2.1 至 2.2 移轉,或許下次從 2.2 升級到 3.0 或更新版本問題時可以先看一下版本升級說明也可以找到答案 :)

參考
從 ASP.NET Core 2.1 至 2.2 移轉

0 意見:

張貼留言

Copyright © m@rcus 學習筆記 | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com