Found 33676 Articles for Programming

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

638 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

What are the various JSON files available in C# ASP.NET Core?

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

7K+ Views

ASP.net Core is re-architected from prior versions of ASP.net, where the configuration was relying on System.Configuration and xml configurations in web.config file. In ASP.net Core, a new easy way to declare and access the global settings for solution, project specific settings, client specific settings etc..The new configuration model, works with XML, INI and JSON files.Different configuration json files in ASP.net Core There are mainly 6 configuration JSON files in ASP.net Core.global.json launchsettings.json appsettings.json bundleconfig.json project.json bower.jsonglobal.jsonExampleYou can define the solution level settings in global.json file.{    "projects": [ "src", "test" ],    "sdk": {       "version": "1.0.0-preview2-003121"   ... Read More

How to enable Session in C# ASP.NET Core?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 13:14:50

18K+ Views

Session is a feature in ASP.NET Core that enables us to save/store the user data.Session stores the data in the dictionary on the Server and SessionId is used as a key. The SessionId is stored on the client at cookie. The SessionId cookie is sent with every request.The SessionId cookie is per browser and it cannot be shared between the browsers.There is no timeout specified for SessionId cookie and they are deleted when the Browser session ends.At the Server end, session is retained for a limited time. The default session timeout at the Server is 20 minutes but it is ... Read More

What is routing in C# ASP.NET Core?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 13:13:10

5K+ Views

Routing is used to map requests to route handlers.Routes are configured when the application starts up, and can extract values from the URL that will be used for request processing.Routing basicsRouting uses routes (implementations of IRouter)map incoming requests to route handlersgenerate URLs used in responsesRouting is connected to the middleware pipeline by the RouterMiddleware class. ASP.NET MVC adds routing to the middleware pipeline as part of its configurationURL matchingIncoming requests enter the RouterMiddleware which calls the RouteAsync method on each route in sequence.The IRouter instance chooses whether to handle the request by setting the RouteContext Handler to a non-null RequestDelegate.If ... Read More

What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run() C# Asp.net Core?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 13:12:02

5K+ Views

We can configure middleware in the Configure method of the Startup class using IApplicationBuilder instance.Run() is an extension method on IApplicationBuilder instance which adds a terminal middleware to the application's request pipeline.The Run method is an extension method on IApplicationBuilder and accepts a parameter of RequestDelegate.signature of the Run methodpublic static void Run(this IApplicationBuilder app, RequestDelegate handler)signature of RequestDelegatepublic delegate Task RequestDelegate(HttpContext context);Examplepublic class Startup{    public Startup(){    }    public void Configure(IApplicationBuilder app, IHostingEnvironment env,    ILoggerFactory loggerFactory){       //configure middleware using IApplicationBuilder here..       app.Run(async (context) =>{          await context.Response.WriteAsync("Hello ... Read More

What is the use of "Map" extension while adding middleware to C# ASP.NET Core pipeline?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 13:09:03

3K+ Views

Middleware are software components that are assembled into an application pipeline to handle requests and responses.Each component chooses whether to pass the request on to the next component in the pipeline, and can perform certain actions before and after the next component is invoked in the pipeline.Map extensions are used as convention for branching the pipeline.The Map extension method is used to match request delegates based on a request’s path. Map simply accepts a path and a function that configures a separate middleware pipeline.In the following example, any request with the base path of /maptest will be handled by the ... Read More

What is the use of the Configure() method of startup class in C# Asp.net Core?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 13:06:30

2K+ Views

The configure method is present inside startup class of ASP.NET Core applicationThe Configure method is a place where you can configure application request pipeline for your application using IApplicationBuilder instance that is provided by the built-in IoC containerThe Configure method by default has these three parameters IApplicationBuilder, IWebHostEnvironment and ILoggerFactory .At run time, the ConfigureServices method is called before the Configure method. This is to register custom service with the IoC container which can be used in Configure method.IWebHostEnvironment :Provides information about the web hosting environment an application is running in.IApplicationBuilder:Defines a class that provides the mechanisms to configure an ... Read More

How to run an external application through a C# application?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 13:04:32

5K+ Views

An external application can be run from a C# application using Process. A process is a program that is running on your computer. This can be anything from a small background task, such as a spell-checker or system events handler to a full-blown application like Notepad etc.Each process provides the resources needed to execute a program. Each process is started with a single thread, known as the primary thread. A process can have multiple threads in addition to the primary thread. Processes are heavily dependent on system resources available while threads require minimal amounts of resource, so a process is ... Read More

Advertisements