Problem statement
Use everything from this module in one app: send a message through a collection of channels, count every delivery in a shared counter, and tag each request with its own id.
Requirements
The channels, the formatter, the counter, the request context and the DispatchController are already provided. Your task has two parts: register the services in NotificationDispatchApi/Services/ServiceRegistration.cs, and write the constructor and the loop of NotificationDispatcher in NotificationDispatchApi/Services/NotificationDispatcher.cs.
ServiceRegistration
AddApplicationServices registers:
- INotificationChannel with EmailChannel, SmsChannel and PushChannel, in that order, using AddScoped
- IDeliveryCounter with DeliveryCounter, using AddSingleton, so the totals keep growing across requests
- IRequestContext with RequestContext, using AddScoped, so each request has one RequestId that everything in it shares
- INotificationDispatcher with NotificationDispatcher, IMessageFormatter with MessageFormatter, and IMessagePrefixProvider with MessagePrefixProvider, using AddScoped. Each channel receives the formatter, and the formatter receives the prefix provider, through their constructors
NotificationDispatcher
- The constructor receives IEnumerable<INotificationChannel>, IDeliveryCounter and IRequestContext and keeps them in the three fields. The dispatcher uses only these and never creates them with new
- Dispatch goes through channels in order. For each channel, it:
- calls Send(message) once
- sets the RequestId of the returned delivery to the request context's RequestId
- calls Record on the counter with the channel's Name
- adds the delivery to deliveries
The blank-message check and the return are already written.
DispatchController (provided)
- POST /dispatch answers 200 with the dispatch result, plus controllerRequestId: the controller's own RequestId, which matches requestId. A missing or blank message answers 400
- GET /deliveries answers 200 with the total and the count for each channel
- GET /context answers 200 with the current RequestId
Sample Requests & Responses
Your ids will be different.
POST /dispatch
Request body:
{ "message": "server restarted" }
Response (200 OK):
{
"requestId": "556f5e40-3060-43b8-99e6-87846552dc08",
"controllerRequestId": "556f5e40-3060-43b8-99e6-87846552dc08",
"message": "server restarted",
"channelCount": 3,
"deliveries": [
{ "channel": "Email", "message": "server restarted", "formattedMessage": "[dispatch] server restarted", "requestId": "556f5e40-3060-43b8-99e6-87846552dc08" },
{ "channel": "Sms", "message": "server restarted", "formattedMessage": "[dispatch] server restarted", "requestId": "556f5e40-3060-43b8-99e6-87846552dc08" },
{ "channel": "Push", "message": "server restarted", "formattedMessage": "[dispatch] server restarted", "requestId": "556f5e40-3060-43b8-99e6-87846552dc08" }
]
}
A second POST /dispatch gets a new requestId, for example 22644990-e9df-4e4b-b448-83e2f6fc276f.
GET /deliveries (after the two dispatches above)
Response (200 OK):
{
"total": 6,
"perChannel": [
{ "channel": "Email", "count": 2 },
{ "channel": "Push", "count": 2 },
{ "channel": "Sms", "count": 2 }
]
}
POST /dispatch with { "message": " " }
Response (400 Bad Request):
{ "error": "message is required" }
