|
1 | 1 | namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus |
2 | 2 | { |
3 | | - using System; |
| 3 | + using Autofac; |
| 4 | + using Microsoft.Azure.ServiceBus; |
| 5 | + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; |
4 | 6 | using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; |
5 | 7 | using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; |
6 | 8 | using Microsoft.Extensions.Logging; |
7 | | - using Microsoft.Azure.ServiceBus; |
8 | 9 | using Newtonsoft.Json; |
| 10 | + using Newtonsoft.Json.Linq; |
| 11 | + using System; |
| 12 | + using System.Reflection; |
9 | 13 | using System.Text; |
10 | 14 | using System.Threading.Tasks; |
11 | | - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; |
12 | | - using System.Reflection; |
13 | | - using Microsoft.Azure.ServiceBus.Filters; |
14 | | - using Autofac; |
15 | | - using Newtonsoft.Json.Linq; |
16 | 15 |
|
17 | 16 | public class EventBusServiceBus : IEventBus |
18 | 17 | { |
@@ -129,11 +128,25 @@ private void RegisterSubscriptionClientMessageHandler() |
129 | 128 | _subscriptionClient.RegisterMessageHandler( |
130 | 129 | async (message, token) => |
131 | 130 | { |
132 | | - var eventName = message.Label; |
| 131 | + var eventName = $"{message.Label}{INTEGRATION_EVENT_SUFIX}"; |
133 | 132 | var messageData = Encoding.UTF8.GetString(message.Body); |
134 | 133 | await ProcessEvent(eventName, messageData); |
| 134 | + |
| 135 | + // Complete the message so that it is not received again. |
| 136 | + await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken); |
135 | 137 | }, |
136 | | - new MessageHandlerOptions() { MaxConcurrentCalls = 10, AutoComplete = true }); |
| 138 | + new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 10, AutoComplete = false }); |
| 139 | + } |
| 140 | + |
| 141 | + private Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs) |
| 142 | + { |
| 143 | + Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}."); |
| 144 | + var context = exceptionReceivedEventArgs.ExceptionReceivedContext; |
| 145 | + Console.WriteLine("Exception context for troubleshooting:"); |
| 146 | + Console.WriteLine($"- Endpoint: {context.Endpoint}"); |
| 147 | + Console.WriteLine($"- Entity Path: {context.EntityPath}"); |
| 148 | + Console.WriteLine($"- Executing Action: {context.Action}"); |
| 149 | + return Task.CompletedTask; |
137 | 150 | } |
138 | 151 |
|
139 | 152 | private async Task ProcessEvent(string eventName, string message) |
@@ -169,13 +182,13 @@ private void RemoveDefaultRule() |
169 | 182 | try |
170 | 183 | { |
171 | 184 | _subscriptionClient |
172 | | - .RemoveRuleAsync(SubscriptionClient.DefaultRule) |
| 185 | + .RemoveRuleAsync(RuleDescription.DefaultRuleName) |
173 | 186 | .GetAwaiter() |
174 | 187 | .GetResult(); |
175 | 188 | } |
176 | 189 | catch (MessagingEntityNotFoundException) |
177 | 190 | { |
178 | | - _logger.LogInformation($"The messaging entity {SubscriptionClient.DefaultRule} Could not be found."); |
| 191 | + _logger.LogInformation($"The messaging entity { RuleDescription.DefaultRuleName } Could not be found."); |
179 | 192 | } |
180 | 193 | } |
181 | 194 | } |
|
0 commit comments