Câu hỏi phỏng vấn .NET Core
Câu hỏi

When using DI in Controller shall I ca...

Câu trả lời

When using Dependency Injection (DI) in an ASP.NET Core Controller, you generally should not call IDisposable on any injected service manually. The DI container in ASP.NET Core is responsible for managing the lifecycle of the services it creates, including disposing of them when appropriate.

Here are the key points to consider:

  1. Service Lifetimes:

    • Transient: Services are created each time they are requested. They are disposed of immediately after use.
    • Scoped: Services are created once per request and are disposed of at the end of the request.
    • Singleton: Services are created once and live for the duration of the application. They are disposed of when the application shuts down.
  2. Automatic Disposal:

    • The DI container will automatically call Dispose on IDisposable services it creates when their lifetime ends. For example, scoped services are disposed of at the end of the request, and singleton services are disposed of when the application shuts down.
  3. Manual Disposal:

    • If you manually create an instance of a service within your code (i.e., not through the DI container), you are responsible for disposing of it. This is typically done using a using statement or explicitly callin...
senior

senior

Gợi ý câu hỏi phỏng vấn

junior

What is a .NET application domain?

middle

What is Kestrel?

senior

What's the difference between gRPC and WCF?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào