In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about sample analysis of service registration and pipelines in ASP.Net Core MVC. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
DI container, it depends on the interface, so we first create a new interface, called IWelcome , the interface defines a method, called GetWelcomMsg, and then create a class, to implement this interface, so that one of our most basic services is done. The code structure and code are shown below (I created a folder to store these services):
After the code is written, we try to inject a reference to this interface into the Configure method, because when we get the configuration information, we also inject it directly into this method, so let's try it. The code is as follows:
Everything's fine, the method's working, let's run it and see if it works.
Not surprising at all, it reported the error, the error message is as follows:
This means that the service IWelcome cannot be resolved and cannot be injected.
So why IConfiguration,IHostingEnvironment,IApplicationBuilder and so on, we can directly inject the use of it, that is ASP.Net Core MVC inside the source package, has automatically registered for us these services, we can use directly. We want to use custom services or components, we need to register ourselves.
When it comes to custom service registration, there are several ways to register services, or different registration methods. There are three ways to register for services.
We look at the Startup class, we will find that it has a ConfigureServices method, see the name, it is used to configure services, there is an IServiceCollection interface parameter, this interface is the DI service interface management object provided by default. Net Core, all our registered services can be managed or operated through this interface. If you use third-party packages, such as Autofac says otherwise, the use of Autofac is not described here.
Then we can register our custom services through the IServiceCollection interface. As mentioned above, there are three methods to register services, namely:
1. Add Singleton (Singleton registration service, that is, in the whole application, only one interface object will be created, all requests will use this interface object, there may be concurrency problems)
2. AddTransient (Pipeline Mechanism Registry Service. An interface object is created for each request, and is used by the current request.
3. AddScoped (Session Mechanism Registration Service. That is, only one interface object will be created in the current session. What is a session, can be simply understood as the current browser open to close, this process is a whole session process)
Here, we first register the service using AddSingleton singleton pattern, as follows:
services.AddSingleton();
The first generic parameter is the interface, and the second generic parameter is the interface implementation class.
Then run another of our programs, you can see that the browser output our string normally.
As for the above three different life cycle registration methods, how to use, depending on the purpose of the service, generally I recommend using AddScoped registration service
Here, we have a simple service from definition to implementation to registration to use completed. It's not that simple.
Let's briefly talk about ASP.Net Core MVC pipeline
Here I will only briefly explain the ASP.Net Core MVC pipeline running process, will not explain how to implement from listening port to forwarding to ASP.Net Core MVC
For example, we now initiate a Get request, when MVC receives this request, our pipeline process will start running, it will trigger Logger this middleware first, that is, logging middleware, which can obtain a lot of information, such as request header information, parameter information, etc., and even we can modify this information. This information will also be printed on the console, if you use the dotnet run command to run this MVC program, there are no screenshots here, you can try it yourself
After logging middleware is executed, other middleware will be executed if we enable other middleware. For example, if we enable authentication middleware, then after logging middleware is executed, authentication middleware will be executed to authenticate, if authentication succeeds, continue to the next step, if authentication fails, do other processing, such as redirecting to the login page, etc.
After this step, the MVC pipeline enters the route matching process. If the match is successful, it returns HTML or JSON or XML data normally, etc. If the match fails, it returns a 404 error, etc.
Thank you for reading! About "ASP.Net Core MVC service registration and pipeline sample analysis" This article is shared here, I hope the above content can be of some help to everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.