In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
I believe many inexperienced people don't know what to do about how to use MediatR in asp.net core. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Using MediatR in Asp.net core
I've started thinking about using MediatR as my domain event implementation. To do this, I created a sample program in asp.net core, which is generally very easy, and if you haven't used MediatR before, or if you're looking for a sample program that uses it, read on.
Install MediatR
Assuming you are using Visual studio, you can install the following two packages through nuget's ui:
If you don't use visual studio, find your own way to install it.
Configure MediatR in the Startup class
Next, in the Startup class, add the following code to the ConfigureServices method:
Services.AddMediatR ()
/ / if you have handlers/events in other assemblies
/ / services.AddMediatR (typeof (SomeHandler) .Assembly, / / typeof (SomeOtherHandler) .Assembly)
Since I have only one project / assembly in my sample project, there is no need to add anything else. If you have more than one assembly to be configured with MediatR, you can configure it as commented out in the code above.
Note: I used the Steve Gordon's helpful article to register my types guide at first, but the MediatR.Extensions.Microsoft.DependencyInjection package has better support and more robust growth (not to mention less code). I suggest using this bag.
MediatR actual combat
Of course, if you want to make sure that MediatR works in your asp.net core project, the highest way is to create some simple types and verify that they achieve the desired results. MediatR supports two message types: Request/Response and Notification. Most of the times I use it is in the Notification mode. Because I want to use it as the domain event of the model (if you are not familiar with the domain event, learn about it first and then come back). To use the behavior expected by notifications, you need to define some types that implement INotification and some handler that implement INotificationHandler. When you release a notification, every handler will be in the response. You can use the following code to practice and see the final result. If you are working on a new project, you can put it into Home Controller.
After the above code is finished, use kestrel to run your application (you can enter cmd under the root directory of the project and type dotnet run to start a console panel, or change the startup mode in visual studio, but usually I go directly to the properties directory of the project to find lauchSettings.json, and delete all the things related to IIS. ) then check the output of your console panel, and you should be able to see the log of the output:
If you just want to use MediatR to post events, that's it. If you want to see how to use it to do request/response, you need to implement several classes:
The above code defines a request and its return type (string,IRequest). Handlers must implement a Handle method that returns the desired response. When you send a request, only one handler will be called and a response with the appropriate type will be returned.
(the author illustrates a problem here by sending a pull request to the author of the package requiring that if there is more than one handler registered, then the first registered should be called, because only the first one has been added to the DI system.)
The current version won't throw any exceptions, but it won't surprise me even if an exception is thrown in a future version.
Either way, to use the content defined above, you just need to add the following code to the About method:
Browse the / About page from the browser and you can see the result of the call. The current version has corrected the results, and you will see the results returned by the first registered handler (PingHandler is the first registered according to the order you coded from top to bottom).
The above is a good start for you to start studying MediatR. I can't wait to apply it to my eShopContainer (Microsoft sample project) project. Check out Microsoft's architecture learning hub here for more books on Microsoft technology.
After reading the above, have you mastered how to use MediatR in asp.net core? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.