Found 2587 Articles for Csharp

What is the AddSingleton vs AddScoped vs Add Transient C# Asp.net Core?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:55:59

10K+ Views

There are three ways by which dependencies can be registered in Startup.cs. i.e. AddSingleton, AddScoped and AddTransient.Add SingletonWhen we register a type as singleton, only one instance is available throughout the application and for every request.It is similar to having a static object.The instance is created for the first request and the same is available throughout the application and for each subsequent requests.public void ConfigureServices(IServiceCollection services){    services.AddSingleton() }Add ScopedWhen we register a type as Scoped, one instance is available throughout the application per request. When a new request comes in, the new instance is created. Add scoped specifies that ... Read More

How to handle errors in middleware C# Asp.net Core?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:54:36

2K+ Views

Create a new folder named CustomExceptionMiddleware and a class ExceptionMiddleware.cs inside it.The first thing we need to do is to register our IloggerManager service and RequestDelegate through the dependency injection.The _next parameter of RequestDeleagate type is a function delegate that can process our HTTP requests.After the registration process, we need to create the InvokeAsync() method. RequestDelegate can’t process requests without it.The _next delegate should process the request and the Get action from our controller should generate a successful response. But if a request is unsuccessful (and it is, because we are forcing exception), our middleware will trigger the catch block ... Read More

What is Metapackage in C# Asp.net Core?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:52:07

4K+ Views

It is known that Microsoft.AspNetCore package is one of the packages added to many ASP.NET Core templates.The Microsoft.AspNetCore package is repeatedly included as one of the usual project dependencies when opening a new ASP.NET Core project. It delivers many of the crucial packages to position up a basic ASP.NET Core application.Though, this package does not contain any actual dlls or code itself, it merely contains a series of dependencies on additional packages. By adding this package to your project, you bring in all the relevant packages along with their dlls on which it depends and it is called a metapackage.Specifically, ... Read More

What is the purpose of Program.cs file in C# ASP.NET Core project?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:50:45

1K+ Views

ASP.NET Core web application is actually a console project which starts executing from the entry point public static void Main() in Program class where we can create a host for the web application.public class Program{    public static void Main(string[] args){       BuildWebHost(args).Run();    }    public static IWebHost BuildWebHost(string[] args) =>    WebHost.CreateDefaultBuilder(args)    .UseStartup()    .Build(); }The WebHost is a static class which can be used for creating an instance of IWebHost and IWebHostBuilder with pre-configured defaults.The CreateDefaultBuilder() method creates a new instance of WebHostBuilder with pre-configured defaults. Internally, it configures Kestrel, IISIntegration and other configurations. ... Read More

What is the use of UseIISIntegration in C# Asp.net Core?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:49:24

3K+ Views

All ASP.NET Core applications require a WebHost object that essentially serves as the application and web server. WebHostBuilder is used to configure and create the WebHost. You will normally see UseKestrel() and UseIISIntegration() in the WebHostBuilder setup code.What do these do?UseKestrel() − This registers the IServer interface for Kestrel as the server that will be used to host your application.In the future, there could be other options, including WebListener which will be Windows only.UseIISIntegration() − This tells ASP.NET that IIS will be working as a reverse proxy in front of Kestrel.This then specifies some settings around which port Kestrel should ... Read More

What is the role of IWebHostEnvironment interface in C# ASP.NET Core?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:46:15

6K+ Views

IWebHostEnvironment Provides information about the web hosting environment an application is running in.belongs to namespace Microsoft.AspNetCore.HostingThe IWebHostEnvironment interface need to be injected as dependency in the Controller and then later used throughout the Controller.The IWebHostEnvironment interface have two properties.WebRootPath − Path of the www folder(Gets or sets the absolute path to the directory that contains the web-servable application content files)ContentRootPath − Path of the root folder which contains all the Application files(Gets or sets an IFileProvider pointing at WebRootPath.)UsageWe need to import namesaceusing Microsoft.AspNetCore.Hosting;In the below example, the IWebHostEnvironment is injected in the Controller and assigned to the private property ... Read More

What is Kestral C# Asp.net Core?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:44:47

4K+ Views

Kestrel is a cross-platform web server for ASP.NET Core. It is supported on all platforms and versions that .NET Core supports.It is included by default as internal server in ASP.NET Core. Kestrel can be used, by itself as an edge server i.e Internet-facing web server that can directly process the incoming HTTP requests from the client. In Kestrel, the process used to host the app is dotnet.exe.Kestrel is not used with InProcess hosting model.With Out of Process Hosting model, Kestrel can be used in one of the following 2 ways.Kestrel can be used as the internet facing web serverKestrel can ... Read More

How C# ASP.NET Core Middleware is different from HttpModule?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:43:56

1K+ Views

HttpModules are configured via web.config or global.asax Developer don’t have control on order of execution.As order of modules is mainly based on application life cycle events. The execution order remains same for requests and responses.HttpModules helps you to attach code specific to a application events. HttpModules are tied to System.web.Middleware are configured in Startup.cs code rather than web.config file (entry point for application)Unlike HttpModules, there is full control of what get’s executed and in what order. As they are executed in the order in which they are added.Order of middleware for responses is the reverse from that for requests.Middleware is ... Read More

How to specify service lifetime for register service that added as a dependency C# Asp.net Core?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:41:49

636 Views

Built-in IoC container manages the lifetime of a registered service type. It automatically disposes a service instance based on the specified lifetime.The built-in IoC container supports three kinds of lifetimes −Singleton − IoC container will create and share a single instance of a service throughout the application's lifetime.Transient − The IoC container will create a new instance of the specified service type every time you ask for it.Scoped − IoC container will create an instance of the specified service type once per request and will be shared in a single request.Examplepublic interface ILog{    void info(string str); } class MyConsoleLogger ... Read More

How can we inject the service dependency into the controller C# Asp.net Core?

Nizamuddin Siddiqui
Updated on 25-Sep-2020 10:39:25

1K+ Views

ASP.NET Core injects objects of dependency classes through constructor or method by using built-in IoC container.The built-in container is represented by IServiceProvider implementation that supports constructor injection by default. The types (classes) managed by built-in IoC container are called services.In order to let the IoC container automatically inject our application services, we first need to register them with IoC container.Examplepublic interface ILog{    void info(string str); } class MyConsoleLogger : ILog{    public void info(string str){       Console.WriteLine(str);    } }ASP.NET Core allows us to register our application services with IoC container, in the ConfigureServices method of the ... Read More

Advertisements