Skip to content

Commit 48f3158

Browse files
Clone Core_9
1 parent ce51854 commit 48f3158

File tree

12 files changed

+235
-0
lines changed

12 files changed

+235
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32616.157
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "Sample\Sample.csproj", "{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D6DAC310-11C3-4B79-B1E4-AB6078A2E214}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {42F58BDA-0C79-41BB-AA96-F03CAF76FB6F}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using NServiceBus.Pipeline;
2+
using System;
3+
using System.Diagnostics;
4+
using System.Threading.Tasks;
5+
6+
#region add-tags-from-handler-behavior
7+
8+
class TraceCustomExceptionInHandlerBehavior : Behavior<IInvokeHandlerContext>
9+
{
10+
public override async Task Invoke(IInvokeHandlerContext context, Func<Task> next)
11+
{
12+
try
13+
{
14+
await next();
15+
}
16+
catch (MyBusinessException e)
17+
{
18+
Activity.Current?.AddTag("sample.business-exception.reason", e.ReasonCode);
19+
throw;
20+
}
21+
}
22+
}
23+
24+
public class MyBusinessException : Exception
25+
{
26+
public int ReasonCode { get; init; }
27+
}
28+
29+
#endregion
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using NServiceBus.Pipeline;
2+
using System;
3+
using System.Diagnostics;
4+
using System.Threading.Tasks;
5+
6+
#region add-tags-from-outgoing-behavior
7+
8+
class TraceOutgoingMessageSizeBehavior : Behavior<IOutgoingPhysicalMessageContext>
9+
{
10+
public override Task Invoke(IOutgoingPhysicalMessageContext context, Func<Task> next)
11+
{
12+
Activity.Current?.AddTag("sample.messaging.body.size", context.Body.Length);
13+
return next();
14+
}
15+
}
16+
17+
#endregion
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Diagnostics;
2+
3+
#region custom-activity-source
4+
static class CustomActivitySources
5+
{
6+
public const string Name = "Sample.ActivitySource";
7+
public static readonly ActivitySource Main = new(Name);
8+
}
9+
#endregion
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using NServiceBus;
2+
using System;
3+
using System.Threading.Tasks;
4+
5+
class BillOrderHandler : IHandleMessages<BillOrder>
6+
{
7+
public Task Handle(BillOrder message, IMessageHandlerContext context)
8+
{
9+
Console.WriteLine($"Order billed {message.OrderId}");
10+
return Task.CompletedTask;
11+
}
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NServiceBus;
2+
using System;
3+
using System.Threading.Tasks;
4+
5+
class CreateOrderHandler : IHandleMessages<CreateOrder>
6+
{
7+
#region custom-activity-in-handler
8+
public async Task Handle(CreateOrder message, IMessageHandlerContext context)
9+
{
10+
using var activity = CustomActivitySources.Main.StartActivity("Billing Order");
11+
12+
if (message.SimulateFailure)
13+
{
14+
throw new MyBusinessException{ ReasonCode = 1};
15+
}
16+
17+
Console.WriteLine($"Billing order {message.OrderId}");
18+
activity?.AddTag("sample.billing.system", "paypal");
19+
// Calculate order cost
20+
await context.SendLocal(new BillOrder { OrderId = message.OrderId });
21+
22+
Console.WriteLine($"Shipping order {message.OrderId}");
23+
await context.SendLocal(new ShipOrder { OrderId = message.OrderId });
24+
}
25+
#endregion
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using NServiceBus;
2+
using System;
3+
using System.Diagnostics;
4+
using System.Threading.Tasks;
5+
6+
#region add-tags-from-handler
7+
class ShipOrderHandler : IHandleMessages<ShipOrder>
8+
{
9+
public Task Handle(ShipOrder message, IMessageHandlerContext context)
10+
{
11+
Console.WriteLine($"Order shipped {message.OrderId}");
12+
// Figure out what state we are shipping to
13+
Activity.Current?.AddTag("sample.shipping.state", "STATE");
14+
return Task.CompletedTask;
15+
}
16+
}
17+
#endregion
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using NServiceBus;
2+
using System;
3+
4+
class BillOrder : IMessage
5+
{
6+
public Guid OrderId { get; set;}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using NServiceBus;
2+
using System;
3+
4+
class CreateOrder : IMessage
5+
{
6+
public Guid OrderId { get; set; }
7+
public bool SimulateFailure { get; set; }
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using NServiceBus;
2+
using System;
3+
4+
class ShipOrder : IMessage
5+
{
6+
public Guid OrderId { get; set;}
7+
}

0 commit comments

Comments
 (0)