. .NET is a simple intermediary pattern implementation, an in-process messaging mechanism (with no other external dependencies). Supports messaging of requests/responses, commands, queries, notifications, and events in synchronous or asynchronous form, and supports intelligent scheduling of messages through C# generics.
First, create a new asp.net Core 3.1 project.
nuget install:
To register the service, add the following in the ConfigureServices method in the Startup file:
Request/response mode
The request response pattern is similar to http requests, the question-and-answer pattern, but the difference is that with the MediatR pattern, the requester does not need to pay attention to who the responder is, and the responder does not need to pay attention to who the requester is.
Create a new PingHandler.cs file with the following code:
We tried to call it in the controller, and the code is as follows:
To start a project, visit:The hyperlink login is visible.You can see the log we output in the console, as shown below:
Notifications mode
This mode is actually publishing subscriptions, subscribers do not need to pay attention to who the publisher is, and publishers do not need to pay attention to who the subscribers are.
Create a new UserInfo class with the following code:
Create a new SendEmail class, subscribe to the user's information, and send an email to the user after receiving the message, the code is as follows:
Create a new InsertDatabase class, and after receiving the message, store the user information into the database with the following code:
We try to send a message from UserInfo through the controller, and the code is as follows:
Try to visit:The hyperlink login is visible., found that both methods received a message from us, as follows:
Reference Documentation:The hyperlink login is visible.
Finally, attach the source code:
Tourists, if you want to see the hidden content of this post, please Reply
|