We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept
What is the difference between ConfigureServices and Configure methods?
  • ConfigureServices() and Configure() methods are used to register services and configure components within the request pipeline respectively.
  • ConfigureServices() takes a parameter of type IServiceCollection.
  • Configure() takes a parameter of type IApplicationBuilder with possible parameters of any Service which is registered in the ConfigureServices() method.
  • an application should contain an ConfigureServices() method with an optional Configure() method.
public class Startup
{
    public void ConfigureServices(IServiceCollection services) 
    {
    }
    public void Configure(IApplicationBuilder app)
    {
    }
}

ASP.NET Core •  Added 2 months ago

How do you configure a middleware?

A logical component (a class or a method) can be configured into a middleware by registering it under the Configure() method of the Startup class. It can be done as below.

app.UseMiddleware<MyClass>();

for a class which takes a constructor parameter of RequestDelegate and uses it to navigate to next component, or a simple method delegate within the Configure method as.

app.Use(async (context, next) => {
    // some logic
    await next.Invoke();
});

ASP.NET Core •  Added 2 months ago

What is a middleware?

A middleware is a logical component that sits on the path of a request-response pipeline and is invoked for every request to execute a particular responsibility / functionality on that request. It has access to the HttpContext of the current request and so can be able to validate or reject an incoming request for some condition.

In ASP.NET Core, all the components of the framework such as controllers, authorizers and loggers are internally executed as middlewares.

Developers can also define and include own logical components to be executed to validate or emphasize on an incoming request by registering these as middlewares.

ASP.NET Core •  Added 2 months ago

What is the difference between a Transient and a Scoped service?

A Transient service instance is created every time a calling method or dependent class requests for it from the container, whereas a Scoped service instance is created for a request and is reused by the container for all calling methods or classes within that request. Hence a Scoped service has a create-once-per-request scope while the Transient has a create-once-per-service-invoke scope.

ASP.NET Core •  Added 2 months ago

What is the difference between a Singleton and a Transient service?

A Singleton service instance once created is reused for the entire application lifetime, whereas a Transient service instance is created every time a calling method or dependent class requests for it from the container.

ASP.NET Core •  Added 2 months ago

What is a Scoped service?

A scoped service instance is created only once within a request scope and is reused for all calls within that request.

services.AddScoped<IMyClass, MyClass>();

ASP.NET Core •  Added 2 months ago

What is a Transient service?

A transient service instance is created each time a calling component method or dependent class requests for an instance from the container.

services.AddTransient<IMyClass, MyClass>();

ASP.NET Core •  Added 2 months ago

What is a Singleton service?

A singleton service instance is created only once during entire application lifetime, common across all requests.

services.AddSingleton<IMyClass, MyClass>();

ASP.NET Core •  Added 2 months ago

What are service lifetimes?

A service lifetime defines how a service instance is maintained or created by the IoC container when a calling method or constructor requests for a new service instance. There are three types of services based on how their instance lifetime is managed:

  1. Singleton: New instance is created only once during its entire lifetime, across requests
  2. Transient: New Instance created each time a component requests for an object
  3. Scoped: New instance is created only once per request and is reused for all calls within a request

ASP.NET Core •  Added 2 months ago

How do you register services?

A class can be registered as a service by adding it to the IServiceCollection instance under the ConfigureServices() method in the Startup class.

A class MyClass can be registered as a service in the below ways:

services.AddT<MyClass>(new MyClass()); (or)
services.AddT<new MyClass());
services.AddT<IMyClass, MyClass>(); (or)
services.AddT<IMyClass>(new MyClass());

Where T can be Singleton, Transient or Scoped.

ASP.NET Core •  Added 2 months ago

What is meant by a service?

A service is an interface which is resolved for a configured concrete implementation with a specified lifetime, which is managed by the IoC container and is injected via constructor or method whenever requested by the calling method or class.

It is configured using the ConfigureServices() method in the Startup class.

ASP.NET Core •  Added 2 months ago