Consider this C# code:
4 phương ánCó giải thíchDễ
services.AddScoped<IRepository, CustomerRepository>();In an ASP.NET Core web app, how often is CustomerRepository created?
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "Once per request". AddScoped đăng ký vòng đời Scoped, ASP.NET Core tạo một scope cho mỗi request HTTP nên CustomerRepository được tạo một lần trong mỗi request, mọi controller và dịch vụ cùng request dùng chung nó, và nó được Dispose khi request kết thúc, rất hợp với repository dùng DbContext cũng Scoped. "Once per application" sai vì đó là Singleton đăng ký bằng AddSingleton. "Once per controller" sai vì DI không có vòng đời gắn với controller, thể hiện Scoped được chia sẻ giữa controller và mọi dịch vụ khác trong cùng request chứ không riêng cho controller. "Every time it’s injected" sai vì đó là hành vi của Transient đăng ký bằng AddTransient.
Cùng chương 4
With AddTransient<EmailService, EmailService>(), injecting into a controller twice will produce:Có lời giảiCâu hỏiConsider this C# code: public IActionResult Save(Person person) { if (ModelState.IsValid) return Content("Valid"); return Content("Error"); } If no value for Age is passed and Age is int, what happens?Có lời giảiCâu hỏiConsider this C# code: public class Person { public int Age { get; set; } } public IActionResult Save(Person person) { if (!ModelState.IsValid) { return Content("Invalid!"); } return Content("OK"); } Input: Age = "abc"? What is the output?Có lời giảiCâu hỏiCả 24 câu của chủ đề24 câu