ASP.NET core pipeline: are components added to the processing pipeline visible somewhere? - asp.net-core

As components are added to the processing ASP.NET core pipeline are these visible in some collection accessible from the WebApplication object?

Related

Caching hundreds of routes in an ASP.NET Core 5 MVC application

I'm migrating an ASP.NET webforms application to ASP.NET Core 5 MVC. The application has hundreds of routes. I need to add these routes to the Configure method in the startup.cs. I think the performance can be hit with hundreds of routes.
A few questions:
Is there any feature in ASP.NET Core 5 MVC that I can use to make the search in O(1) time?
I may stored these routes in a database, and need to be cached to make it faster. What is a native cache available in ASP.NET Core 5 MVC?
Is there a feature in the HTTP pipeline that helps the caching of the routes and selecting the view and controller

Can a Worker Service be called and/or used inside an existing ASPNET.Core web project?

I've been reading and learing about the new Worker Service features provided in .Net Core 3.0. I have been using this link from Microsoft: Background tasks with hosted services in ASP.NET Core
What I don't understand is this, can these worker service concepts be introduced into an existing ASPNET Web Project, like a Razor Pages site? Or must you create a new project and then deploy that project as a service using whatever mechanism the host OS proivdes for that?
Yes, you can host any number of hosted services (IHostedService) within ASP.NET Core applications. With version 3, ASP.NET Core uses the generic host (Host.CreateDefaultBuilder) which is the framework that is hosting these hosted services when the application starts. In fact, the ASP.NET Core web application is an IHostedService itself.
To add additional hosted services to your ASP.NET Core application, just register additional hosted services with your service collection, e.g. within the Startup’s ConfigureServices:
services.AddHostedService<MyHostedService>();
That service will then launch together with the ASP.NET Core web server when the application runs.
The Worker SDK that is mentioned in the documentation is actually a subset of the Web SDK that you are using with ASP.NET Core application. Microsoft.NET.Sdk.Worker is basically Microsoft.NET.Sdk.Web without the web-specific stuff like Razor compilation and wwwroot folder stuff. It basically sets up automatic file globbing e.g. for the appsettings.json and does some other useful things that the core Microsoft.NET.Sdk does not have.
Ultimately this means, that when you are using the Web SDK, then you already have everything the Worker SDK offers. So you do not need to specify the Worker SDK just to host additional background services.

AspNet Core AppInsights Integration

I am integrating app insights into our AspNet Core app(Target Framework .Net 4.7.1). I have two queries regarding app insights integration.
I am using SimpleInjector IOC, so does it make sense to have below line of code to inject AI into Asp Net Core DI?
services.AddApplicationInsightsTelemetry
I'm having my own Logger class which initializes TelemetryCLient and Logger class is injected using SimpleInjector. So removing above line code will cause an issue or lack of feature from ASPNet Core perspective?
In Asp.Net when we use to add AI it uses to add ApplicationInsights.config file which contains TelemetryInitializer's and TelemetryModules. Whats the best parctice in AspNet Core 2.1 for this? How do I add following TelemetryInitializers?
HttpDependenciesParsingTelemetryInitializer
AzureRoleEnvironmentTelemetryInitializer
AzureWebAppRoleEnvironmentTelemetryInitializer
OperationCorrelationTelemetryInitializer
etc...
Thanks in advance!!!
services.AddApplicationInsightsTelemetry is the easiest way to add application insights to your project. It sets up auto-collection modules for Requests, Dependencies etc, sets up default TelemetryInitializers, TelemetryProcessors (for sampling, live metrics etc.)
if you don't use services.AddApplicationInsightsTelemetry, then you have to programmatically setup all modules/initializers/sampling etc yourself.
There is no ApplicationInsights.config file, so pretty much every customization of the config is to be done through code. Following shows how to add/remove telemetry initializers.
https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Custom-Configuration#configure-telemetry-initializers

Web Application vs Web Api project types in Asp.net Core

I know that Asp.Net MVC and Asp.Net Web API were merged into one code in Asp.net Core and they inherit from Controller base class and can all return implementations of IActionResult. it be a View for MVC or Json for web api.
But when i want to create a Asp.net Core project, it offers two templates (Web Application and Web Api ), according to what i said in above, there is no differences between these controllers, why there is two templates? is there any differences that i don't know about it?
The web application template will create folders and import stuff needed for a web application such as jquery, css etc. Web api template will create folders and import stuff for a web api. Also the controllers created by default will have different implementations, for example, web application will be returning views and the views will be created in the appropriate folder.
So although they derive from the same controllers, each type of project requires different dependencies.
If I were you I would go ahead and create one for each type and see the difference.
If you want to have both web api and web application in the same project, use areas. This way your web and api will have separate controllers, folders and models. Also if you want to separate them in the future, it will be easy to do so.
The difference between 2 templates is-
The WebAPI template starts with a Controller class that will allow you to respond to RESTful requests at the /api/Values endpoint.
The Web Application template will give you an MVC framework enabled project with some Razor views, the bootstrap CSS framework and jQuery library installed.
If you want to create project with both MVC and API controllers then I would suggest to go with ASP.NET Core Web Application template and add require dependencies.

Controller life-cycle ASP.NET Core WebApi

I'm using asp.net core to develop a WebApi.
What is the life-cycle of controller in ASP.NET Core? Does it depends on the web server?
Usually an instance of a controller is created per request and if you use dependency injection, all associated services are created for that request.