寫.NET Core遇到Unable to resolve service for type通常是忘記註冊物件給DI容器
Startup.cs的ConfigureServices(IServiceCollection services)加入以下這一行
//TagsHelper使用
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
錯誤訊息:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.Infrastructure.IActionContextAccessor' while attempting to activate
2020年5月23日 星期六
2020年2月9日 星期日
C#和Java的泛型差異
http://www.jprl.com/Blog/archive/development/2007/Aug-31.html
雖然很舊的文章,但是很詳細的比較了C#和Java泛型的差異
最近在看Essential C# 6這本書,第11章(C# 7是在第12章)有提到Java和C#泛型機制的不同。搜尋了一些資料,小弟我大致整理如下,歡迎先進補充指正,謝謝
JDK 5加入泛型,為了避免修改JVM規格影響向下相容,是在編譯器實作泛型(不是在JVM實作),另外Java的泛型不支援Primitive Type(等同C#的Value Type)
Java採用Code Sharing實作,對每一個泛型類或泛型方法只會生成一份object code(byte code),Java compiler透過type erasure實作,也導致無法在runtime時期取得泛型類或泛型方法的實際型別
C#和C++ Template採用Code Specialization實作,對每一個泛型類或泛型方法都會產生一份object code
C# 2.0加入泛型,.NET是在CLI(common language implementation)加入泛型支援,性能較好,且可以在runtime時期取得泛型類或泛型方法的實際型別資訊
參考資料:
Java魔法堂:解讀基於Type Erasure的泛型
https://www.cnblogs.com/fsjohnhuang/p/4288614.html
聊聊Java泛型類型擦除及Flink類型暗示(type hint)機制
https://www.jianshu.com/p/cfb8c1386f69
Type Erasure
https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
https://stackoverflow.com/questions/355060/c-sharp-vs-java-generics
https://stackoverflow.com/questions/31693/what-are-the-differences-between-generics-in-c-sharp-and-java-and-templates-i
C# vs Java : Generics
http://toddgerspacher.blogspot.com/2017/07/c-vs-java-generics.html
Generics in Java
https://en.wikipedia.org/wiki/Generics_in_Java#Problems_with_type_erasure
Generics in the Run Time (C# Programming Guide)
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generics-in-the-run-time
.NET Column: Introducing Generics in the CLR
https://docs.microsoft.com/en-us/archive/msdn-magazine/2003/september/net-column-introducing-generics-in-the-clr
Finally, the research group at Microsoft has modified a version of the shared source common language implementation (CLI), code-named "Rotor," to include generics support.
https://dotnetfiddle.net/MEK7FM
雖然很舊的文章,但是很詳細的比較了C#和Java泛型的差異
最近在看Essential C# 6這本書,第11章(C# 7是在第12章)有提到Java和C#泛型機制的不同。搜尋了一些資料,小弟我大致整理如下,歡迎先進補充指正,謝謝
JDK 5加入泛型,為了避免修改JVM規格影響向下相容,是在編譯器實作泛型(不是在JVM實作),另外Java的泛型不支援Primitive Type(等同C#的Value Type)
Java採用Code Sharing實作,對每一個泛型類或泛型方法只會生成一份object code(byte code),Java compiler透過type erasure實作,也導致無法在runtime時期取得泛型類或泛型方法的實際型別
C#和C++ Template採用Code Specialization實作,對每一個泛型類或泛型方法都會產生一份object code
C# 2.0加入泛型,.NET是在CLI(common language implementation)加入泛型支援,性能較好,且可以在runtime時期取得泛型類或泛型方法的實際型別資訊
參考資料:
Java魔法堂:解讀基於Type Erasure的泛型
https://www.cnblogs.com/fsjohnhuang/p/4288614.html
聊聊Java泛型類型擦除及Flink類型暗示(type hint)機制
https://www.jianshu.com/p/cfb8c1386f69
Type Erasure
https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
https://stackoverflow.com/questions/355060/c-sharp-vs-java-generics
https://stackoverflow.com/questions/31693/what-are-the-differences-between-generics-in-c-sharp-and-java-and-templates-i
C# vs Java : Generics
http://toddgerspacher.blogspot.com/2017/07/c-vs-java-generics.html
Generics in Java
https://en.wikipedia.org/wiki/Generics_in_Java#Problems_with_type_erasure
Generics in the Run Time (C# Programming Guide)
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generics-in-the-run-time
.NET Column: Introducing Generics in the CLR
https://docs.microsoft.com/en-us/archive/msdn-magazine/2003/september/net-column-introducing-generics-in-the-clr
Finally, the research group at Microsoft has modified a version of the shared source common language implementation (CLI), code-named "Rotor," to include generics support.
https://dotnetfiddle.net/MEK7FM
2020年2月4日 星期二
2020年2月2日 星期日
reference types(參考型別)在哪種狀況下不同變數會被彼此影響?
微軟官網文件
『With reference types, it is possible for two variables to reference the same object and thus possible for operations on one variable to affect the object referenced by the other variable』
https://docs.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/types-and-variables
做了一個比較
Java也有同樣的特性,Java叫做Primitive Type (基本型態)、Class Type / Reference Type (類別型態 / 參考型態)
但是Java沒有struct類型(型別)
使用reference types(變數會被彼此影響)
https://dotnetfiddle.net/zURcCT
使用value types(變數不會被彼此影響)
https://dotnetfiddle.net/nNaCPE
自己整理的C#和Java比較
https://hackmd.io/SVIGNsLLRkqYxywgJpcgTg
『With reference types, it is possible for two variables to reference the same object and thus possible for operations on one variable to affect the object referenced by the other variable』
https://docs.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/types-and-variables
做了一個比較
Java也有同樣的特性,Java叫做Primitive Type (基本型態)、Class Type / Reference Type (類別型態 / 參考型態)
但是Java沒有struct類型(型別)
使用reference types(變數會被彼此影響)
https://dotnetfiddle.net/zURcCT
使用value types(變數不會被彼此影響)
https://dotnetfiddle.net/nNaCPE
自己整理的C#和Java比較
https://hackmd.io/SVIGNsLLRkqYxywgJpcgTg
2020年1月1日 星期三
MVC的TempData 則是可雙向交換的資料,ViewData 是單向丟給 View 的資料
https://dotblogs.com.tw/regionbbs/2012/01/19/asp_net_mvc_view_object_differences
ViewData 和 TempData 是在 MVC 中暫存資料的一種物件,和 Web Form 的 ViewState 很像
所以,ViewData 是單向丟給 View 的資料,而 TempData 則是可雙向交換的資料,將 TempData 視為 Session,就可以更明白它的用處了。
https://ithelp.ithome.com.tw/articles/10219477
SessionStateTempDataProvider 控制儲存TempData
https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc/SessionStateTempDataProvider.cs
關鍵字:
ViewData、TempData、ViewDataDictionary、TempDataDictionary、ITempDataProvider、Session、SessionStateTempDataProvider、__ControllerTempData、TempDataSessionStateKey
ViewData 和 TempData 是在 MVC 中暫存資料的一種物件,和 Web Form 的 ViewState 很像
所以,ViewData 是單向丟給 View 的資料,而 TempData 則是可雙向交換的資料,將 TempData 視為 Session,就可以更明白它的用處了。
https://ithelp.ithome.com.tw/articles/10219477
SessionStateTempDataProvider 控制儲存TempData
上面說到
TempData字典集合生命週期除了當下請求, 導頁後仍可續存.原因是在SessionStateTempDataProvider將資料存在Session中controllerContext.HttpContext.Session["__ControllerTempData"]
可以透過上面程式碼取得當前的
https://dotblogs.com.tw/wadehuang36/2010/10/02/tempdataTempData字典集合物件.
預設的TempDataProvider是SessionStateTempDataProvider,你沒有看錯,預設是用Session來存放TempData,Session不是使用者存放資料,而且存留時間預設在20分鐘的嗎?
所以SessionStateTempDataProvider有做一些手段,Controller起來時,從Session載入TempData,然後刪除Session,所以在Action時是不會看到TempData的Session,在讀取TempData時,會記錄用了那些Key,在Controller結束時,會把沒有過的TempData在存回Session中,所以一直沒有讀取的TempData是會存在到Session消失的。
原始碼:https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc/SessionStateTempDataProvider.cs
關鍵字:
ViewData、TempData、ViewDataDictionary、TempDataDictionary、ITempDataProvider、Session、SessionStateTempDataProvider、__ControllerTempData、TempDataSessionStateKey
訂閱:
文章 (Atom)