只有累積,沒有奇蹟

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  方法,使用方式如下
  1. public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  2. WebHost.CreateDefaultBuilder(args)
  3. .UseStartup<Startup>()
  4. .UseKestrel(options =>
  5. {
  6. options.Limits.MaxConcurrentConnections = 100;
  7. });

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

確認 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 方法
  1. /// <summary>
  2. /// Configures Kestrel options but does not register an IServer. See <see cref="M:Microsoft.AspNetCore.Hosting.WebHostBuilderKestrelExtensions.UseKestrel(Microsoft.AspNetCore.Hosting.IWebHostBuilder)" />.
  3. /// </summary>
  4. /// <param name="hostBuilder">
  5. /// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure.
  6. /// </param>
  7. /// <param name="configureOptions">A callback to configure Kestrel options.</param>
  8. /// <returns>The Microsoft.AspNetCore.Hosting.IWebHostBuilder.</returns>
  9. public static IWebHostBuilder ConfigureKestrel(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, KestrelServerOptions> configureOptions)
  10. {
  11. if (configureOptions == null)
  12. throw new ArgumentNullException(nameof (configureOptions));
  13. return hostBuilder.ConfigureServices((Action<WebHostBuilderContext, IServiceCollection>) ((context, services) => services.Configure<KestrelServerOptions>((Action<KestrelServerOptions>) (options => configureOptions(context, options)))));
  14. }
確定有其方法,重新至專案調整 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 移轉

Related Posts:

  • [NETCore] ASP.NET Core 啟動失敗 - 嘗試存取通訊端被拒絕,因為存取權限不足問題  接獲同事詢問專案無法正常啟用,專案是使用 ASP.NET Core 2.2 開發並搭配 Kestrel 使用,在過去開發時都正常運作但今天忽然就遭遇啟動異常的狀況,在啟用時會跳出錯誤訊息為 'Unable to bind to http://localhost:5000 on the IPv4 loopback interface: '嘗試存取通訊端被拒絕,因為存取權限不足。''  ,這篇… Read More
  • [Tool] Log Parser Studio - 搜尋 Log 好幫手前言 這幾天專案遇到些小亂流,必須找到特定條件的應用程式 Log 做資料的確認及重送機制,雖然系統有使用 ELK但由於專案太舊導致 Log 沒紀錄在 ELK 上面,為了快速找到多台應用程式中的 Log 資訊,在搜尋過程中爬 Log 使用 Log Parser 工具來快速搜尋 Log 資訊,避免過度花費人工的方式逐一進行搜尋 Log,這篇文章就針對 Log Paresr Studio 工具做簡單的介紹與說明,若有問題歡迎提出一起討論或是給予指導。 … Read More
  • [.NETCore] 在 IIS 執行 ASP.NET Core 應用程式前言 在 IIS 執行 ASP.NET Core 專案時與過去 .NET Framework 運作不同,.NET Core Application 預設以 Kestrel 為 HTTP Server 與 IIS 做溝通,流程可以參考下圖所示;因此要將開發好的 .NET Core 網站專案佈署至 IIS 設定方式是不同的,這篇介紹如何將 ASP.NET Core 應用程式佈署至 IIS 的步驟說明,若有問題歡迎提出一起討論或是給予指導 … Read More
  • [NETCore] ASP.NET Core 啟動異常 - HTTP Error 500.30 - ANCM In-Process Start Failure 問題  在開發專案時跳出異常訊息,錯誤訊息為  HTTP Error 500.30 - ANCM In-Process Start Failure ,這篇就針對此案例作簡單紀錄與分享,若是有不清楚或是錯誤的地方歡迎討論予糾正。 解決方法  廢話不多說,先看案發現場的錯誤畫面 執行異常的程式代碼,看起來很單純的代碼 public static void Main(s… Read More
  • [IIS] 程序無法存取檔案,因為檔案正由另一個程序使用。(發生例外狀況於HRESULT:0x80070020)問題 今天要在公司測試 Server 建立測試站台,在完成設定 Application Pool 與站台指定位置後按下啟動,跳出'程序無法存取檔案,因為檔案正由另一個程序使用。(發生例外狀況於HRESULT:0x80070020) 錯誤訊息,訊息內容看似有檔案被 lock 住造成啟動異常,但追根究底之後會發現其異常原因蠻單純的,以下就針對解決此問題的方式做說明,若有問題歡迎提出一起討論或是給予指導。 解決方案 根… Read More

0 意見:

張貼留言

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

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com