-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEmailMessageHandler.cs
More file actions
97 lines (84 loc) · 3.67 KB
/
EmailMessageHandler.cs
File metadata and controls
97 lines (84 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using CloudNimble.SimpleMessageBus.Core;
using SimpleMessageBus.Samples.Core;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SimpleMessageBus.Samples.AzureWebJobs
{
/// <summary>
/// An example handler that shows how to process <see cref="IMessage">IMessages</see> coming off the queue that require email notifications.
/// </summary>
public class EmailMessageHandler : IMessageHandler
{
/// <summary>
/// Specifies which <see cref="IMessage"/> types are handled by this <see cref="IMessageHandler"/>.
/// </summary>
/// <returns>An <see cref="IEnumerable{Type}"/> containing all of the <see cref="IMessage"/> types this <see cref="IMessageHandler"/> supports.</returns>
public IEnumerable<Type> GetHandledMessageTypes()
{
yield return typeof(NewUserMessage);
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
/// <returns></returns>
public Task OnErrorAsync(IMessage message, Exception exception) => throw new NotImplementedException();
/// <summary>
/// Processes the message envelope, demonstrating the new idempotency pattern.
/// </summary>
/// <param name="messageEnvelope">The message envelope to process.</param>
/// <returns>A <see cref="Task"/> reference for the asynchronous function.</returns>
public async Task OnNextAsync(MessageEnvelope messageEnvelope)
{
var result = false;
var messageType = Type.GetType(messageEnvelope.MessageType);
switch (messageType)
{
case Type newUserMessage when newUserMessage == typeof(NewUserMessage):
var message = messageEnvelope.GetMessage<NewUserMessage>();
// Check if this handler already processed this message successfully
if (message.LastRunSucceeded(GetType().Name))
{
Console.WriteLine($"Message {message.Id} already processed successfully by {GetType().Name}");
return;
}
try
{
result = await SendNewUserEmailAsync(message);
message.UpdateResult(result, GetType().Name);
// Add some metadata for demonstration
if (result)
{
message.Metadata["EmailSentAt"] = DateTime.UtcNow;
message.Metadata["ProcessedBy"] = Environment.MachineName;
}
}
catch (Exception ex)
{
message.UpdateResult(false, GetType().Name);
message.Metadata["LastError"] = ex.Message;
throw;
}
break;
}
//RWM: Throw an exception to get the message tossed in the poison queue.
if (!result)
{
throw new Exception("Message processing failed.");
}
}
/// <summary>
///
/// </summary>
/// <param name="newUserMessage"></param>
/// <returns></returns>
internal async Task<bool> SendNewUserEmailAsync(NewUserMessage newUserMessage)
{
//TODO: Send email here.
Console.WriteLine("The message has been processed.");
return await Task.FromResult(true);
}
}
}