This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18805|Reply: 1

[.NET Core] ASP.NET Core uses the MediatR intermediary model

[Copy link]
Posted on 6/19/2020 5:53:28 PM | | | |
. .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, pleaseReply





Previous:Angular project release issue
Next:OpenLDAP Series (1) Introduction to LDAP
Posted on 6/20/2020 4:13:27 PM |
6666
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com