| ASP.NET-Interview-Questions-7 :👈 | 👉:REST-vs-SOAP |
ASP.NET-Interview-Questions-91 to 110 |
Q91. What is DbContext Pooling?
DbContext pooling reuses DbContext instances to reduce allocation overhead. It does not make DbContext a Singleton; a context is leased for use and returned to the pool when disposed.
Q92. When would you use Transient, Scoped, and Singleton?
- Transient for lightweight services that do not need shared state.
- Scoped for request-level services, repositories, and DbContext.
- Singleton for suitable shared services that are thread-safe and do not hold request-specific state.
Q93. What is middleware in ASP.NET Core?
Middleware is a component that participates in processing HTTP requests and responses. Multiple middleware components are arranged into a pipeline.
Q94. What is the ASP.NET Core request pipeline?
It is an ordered sequence of middleware components through which an HTTP request passes before reaching its endpoint.
Q95. What is the difference between Use() and Run()?
Use() can call the next middleware and continue processing. Run() adds terminal middleware and does not accept a next delegate.
Q96. What is the purpose of Map()?
Map() creates a separate middleware pipeline for requests matching a specified path prefix.
Q97. What is short-circuiting?
Short-circuiting occurs when middleware generates a response without calling the next middleware component.
Q98. Why is middleware order important?
Middleware executes in registration order. Incorrect ordering can affect authentication, authorization, exception handling, CORS and other application behavior.
Q99. What is RequestDelegate?
RequestDelegate is a delegate that accepts an HttpContext and returns a Task. It represents a component in the HTTP request pipeline.
Q100. What is HttpContext?
HttpContext provides access to the current HTTP request, response, user, connection and request-specific services.
Q101. How do you create custom middleware?
Create a class that accepts RequestDelegate in its constructor and implements an Invoke or InvokeAsync method. Register it using app.UseMiddleware<T>().
Q102. What is the difference between conventional middleware and IMiddleware?
Conventional middleware is activated through the middleware pipeline and normally uses constructor injection for dependencies suitable for its lifetime. IMiddleware is activated through DI, allowing its lifetime to be managed by the container.
Q103. Can middleware access scoped services?
Yes. Conventional middleware can receive scoped dependencies through its InvokeAsync() method parameters. Alternatively, use IMiddleware registered with an appropriate lifetime.
Q104. What happens if middleware does not call next()?
The remaining middleware components and endpoint are not executed for that request.
Q105. Why should exception middleware be registered early?
It needs to wrap downstream middleware so that it can catch exceptions thrown by those components.
Q106. What is the difference between Map() and MapWhen()?
Map() branches based on a path prefix. MapWhen() branches based on a custom condition involving the request context.
Q107. Can middleware modify the HTTP response?
Yes. Middleware can modify response headers, status codes and body content, provided the response has not already started where applicable.
Q108. What is the onion model in middleware?
It describes how middleware executes before the next component and then resumes after the downstream component completes. The response therefore unwinds in reverse order.
Q109. What is the purpose of HttpContext.Items?
It stores temporary key-value data that can be shared between middleware and other components during a single HTTP request.
Q110. What is the difference between middleware and filters?
Middleware operates at the HTTP request pipeline level and can run before endpoint execution. MVC filters operate within the MVC action execution pipeline and provide access to MVC-specific contexts and stages.
| ASP.NET-Interview-Questions-7 :👈 | 👉:REST-vs-SOAP |