Skip to content

Commit d95c6d7

Browse files
Snippets for the Core folder for NServiceBus 10 (#7734)
* Duplicate of Core 9 with the extra files from Core 9.1 * Add project to the solution * Update to file scoped namespaces * DataBus is not part of Core anymore, we should point to ClaimCheck instead * Delete not needed override * Fix DataBus obsoletes * Delete the copied over upgrade guides * Remove version number from the namespace and some mild formatting * Remove unnecessary obsolete implementation * Delete unused snippets * Adding back some deleted files * Tweaks --------- Co-authored-by: Brandon Ording <[email protected]>
1 parent d3be9d0 commit d95c6d7

File tree

228 files changed

+7032
-332
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

228 files changed

+7032
-332
lines changed

Snippets/Core/Core.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core_9", "Core_9\Core_9.csp
1212
EndProject
1313
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core_9.1", "Core_9.1\Core_9.1.csproj", "{2FFFC06C-64E4-4A61-B3A0-71B8B4EBF041}"
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core_10", "Core_10\Core_10.csproj", "{4DB583A4-41C0-4BD1-A902-DC3945EBA985}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -38,6 +40,10 @@ Global
3840
{2FFFC06C-64E4-4A61-B3A0-71B8B4EBF041}.Debug|Any CPU.Build.0 = Debug|Any CPU
3941
{2FFFC06C-64E4-4A61-B3A0-71B8B4EBF041}.Release|Any CPU.ActiveCfg = Release|Any CPU
4042
{2FFFC06C-64E4-4A61-B3A0-71B8B4EBF041}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{4DB583A4-41C0-4BD1-A902-DC3945EBA985}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44+
{4DB583A4-41C0-4BD1-A902-DC3945EBA985}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{4DB583A4-41C0-4BD1-A902-DC3945EBA985}.Release|Any CPU.ActiveCfg = Release|Any CPU
46+
{4DB583A4-41C0-4BD1-A902-DC3945EBA985}.Release|Any CPU.Build.0 = Release|Any CPU
4147
EndGlobalSection
4248
GlobalSection(SolutionProperties) = preSolution
4349
HideSolutionNode = FALSE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Core.AssemblyScanning;
2+
3+
using NServiceBus;
4+
5+
public class DisableAssemblyFileScanning
6+
{
7+
public void DisableFileScanning(EndpointConfiguration endpointConfiguration)
8+
{
9+
#region disable-file-scanning
10+
endpointConfiguration.AssemblyScanner().ScanFileSystemAssemblies = false;
11+
#endregion
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Core.Audit;
2+
3+
using System;
4+
using System.Threading.Tasks;
5+
using NServiceBus.Pipeline;
6+
7+
#region AddAuditData
8+
public class CustomAuditDataBehavior : Behavior<IAuditContext>
9+
{
10+
public override Task Invoke(IAuditContext context, Func<Task> next)
11+
{
12+
context.AuditMetadata["myKey"] = "MyValue";
13+
return next();
14+
}
15+
}
16+
#endregion
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace Core.Audit;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
using NServiceBus.Audit;
7+
using NServiceBus.Pipeline;
8+
9+
#region custom-audit-action
10+
11+
public class EnableExternalBodyStorageBehavior : Behavior<IAuditContext>
12+
{
13+
private readonly IExternalBodyStorage storage;
14+
15+
public EnableExternalBodyStorageBehavior(IExternalBodyStorage storage)
16+
{
17+
this.storage = storage;
18+
}
19+
20+
public async override Task Invoke(IAuditContext context, Func<Task> next)
21+
{
22+
var message = context.Message;
23+
var bodyUrl = await storage.StoreBody(message.MessageId, message.Body);
24+
25+
context.AuditMetadata["body-url"] = bodyUrl;
26+
27+
context.AuditAction = new SkipAuditMessageBody();
28+
29+
await next();
30+
}
31+
32+
class SkipAuditMessageBody : RouteToAudit
33+
{
34+
public override IReadOnlyCollection<IRoutingContext> GetRoutingContexts(IAuditActionContext context)
35+
{
36+
var routingContexts = base.GetRoutingContexts(context);
37+
38+
foreach (var routingContext in routingContexts)
39+
{
40+
// clear out the message body
41+
routingContext.Message.UpdateBody(ReadOnlyMemory<byte>.Empty);
42+
}
43+
44+
return routingContexts;
45+
}
46+
}
47+
}
48+
49+
#endregion
50+
51+
public interface IExternalBodyStorage
52+
{
53+
Task<string> StoreBody(string messageId, ReadOnlyMemory<byte> body);
54+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Core.Audit;
2+
3+
using NServiceBus;
4+
5+
class Usage
6+
{
7+
Usage(EndpointConfiguration endpointConfiguration)
8+
{
9+
#region AuditWithCode
10+
11+
endpointConfiguration.AuditProcessedMessagesTo("targetAuditQueue");
12+
13+
#endregion
14+
}
15+
16+
17+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
namespace Core;
2+
3+
using System.Threading.Tasks;
4+
using NServiceBus;
5+
6+
class BasicUsageOfIBus
7+
{
8+
async Task Send(EndpointConfiguration endpointConfiguration)
9+
{
10+
#region BasicSend
11+
12+
var endpointInstance = await Endpoint.Start(endpointConfiguration);
13+
var message = new MyMessage();
14+
await endpointInstance.Send(message);
15+
16+
#endregion
17+
}
18+
19+
#region SendFromHandler
20+
21+
public class MyMessageHandler :
22+
IHandleMessages<MyMessage>
23+
{
24+
public Task Handle(MyMessage message, IMessageHandlerContext context)
25+
{
26+
var otherMessage = new OtherMessage();
27+
return context.Send(otherMessage);
28+
}
29+
}
30+
31+
#endregion
32+
33+
async Task SetDestination(IEndpointInstance endpoint)
34+
{
35+
#region BasicSendSetDestination
36+
37+
var options = new SendOptions();
38+
options.SetDestination("MyDestination");
39+
await endpoint.Send(new MyMessage(), options);
40+
41+
#endregion
42+
}
43+
44+
async Task SpecificInstance(IEndpointInstance endpoint)
45+
{
46+
#region BasicSendSpecificInstance
47+
48+
var options = new SendOptions();
49+
options.RouteToSpecificInstance("MyInstance");
50+
var message = new MyMessage();
51+
await endpoint.Send(message, options);
52+
53+
#endregion
54+
}
55+
56+
async Task ThisEndpoint(IEndpointInstance endpoint)
57+
{
58+
#region BasicSendToAnyInstance
59+
60+
var options = new SendOptions();
61+
options.RouteToThisEndpoint();
62+
await endpoint.Send(new MyMessage(), options);
63+
// or
64+
await endpoint.SendLocal(new MyMessage());
65+
66+
#endregion
67+
}
68+
69+
async Task ThisInstance(IEndpointInstance endpoint)
70+
{
71+
#region BasicSendToThisInstance
72+
73+
var options = new SendOptions();
74+
options.RouteToThisInstance();
75+
var message = new MyMessage();
76+
await endpoint.Send(message, options);
77+
78+
#endregion
79+
}
80+
81+
async Task SendReplyToThisInstance(IEndpointInstance endpoint)
82+
{
83+
#region BasicSendReplyToThisInstance
84+
85+
var options = new SendOptions();
86+
options.RouteReplyToThisInstance();
87+
var message = new MyMessage();
88+
await endpoint.Send(message, options);
89+
90+
#endregion
91+
}
92+
93+
async Task SendReplyToAnyInstance(IEndpointInstance endpoint)
94+
{
95+
#region BasicSendReplyToAnyInstance
96+
97+
var options = new SendOptions();
98+
options.RouteReplyToAnyInstance();
99+
var message = new MyMessage();
100+
await endpoint.Send(message, options);
101+
102+
#endregion
103+
}
104+
105+
async Task SendReplyTo(IEndpointInstance endpoint)
106+
{
107+
#region BasicSendReplyToDestination
108+
109+
var options = new SendOptions();
110+
options.RouteReplyTo("MyDestination");
111+
var message = new MyMessage();
112+
await endpoint.Send(message, options);
113+
114+
#endregion
115+
}
116+
117+
async Task ReplySendReplyToThisInstance(IMessageHandlerContext context)
118+
{
119+
#region BasicReplyReplyToThisInstance
120+
121+
var options = new ReplyOptions();
122+
options.RouteReplyToThisInstance();
123+
var myMessage = new MyMessage();
124+
await context.Reply(myMessage, options);
125+
126+
#endregion
127+
}
128+
129+
async Task ReplySendReplyToAnyInstance(IMessageHandlerContext context)
130+
{
131+
#region BasicReplyReplyToAnyInstance
132+
133+
var options = new ReplyOptions();
134+
options.RouteReplyToAnyInstance();
135+
var myMessage = new MyMessage();
136+
await context.Reply(myMessage, options);
137+
138+
#endregion
139+
}
140+
141+
async Task ReplySendReplyTo(IMessageHandlerContext context)
142+
{
143+
#region BasicReplyReplyToDestination
144+
145+
var options = new ReplyOptions();
146+
options.RouteReplyTo("MyDestination");
147+
var myMessage = new MyMessage();
148+
await context.Reply(myMessage, options);
149+
150+
#endregion
151+
}
152+
153+
public class MyMessage
154+
{
155+
}
156+
157+
public class OtherMessage
158+
{
159+
}
160+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Core;
2+
3+
using System.Threading.Tasks;
4+
using NServiceBus;
5+
using Transports;
6+
7+
class BestPracticesConfiguration
8+
{
9+
void DisableFeature(EndpointConfiguration endpointConfiguration)
10+
{
11+
#region DisableBestPracticeEnforcementPerEndpoint
12+
13+
var routing = endpointConfiguration.UseTransport(new TransportDefinition());
14+
routing.DoNotEnforceBestPractices();
15+
16+
#endregion
17+
}
18+
19+
async Task DisablePerMessage(IPipelineContext context)
20+
{
21+
#region DisableBestPracticeEnforcementPerMessage
22+
23+
var options = new SendOptions();
24+
25+
options.DoNotEnforceBestPractices();
26+
27+
await context.Send(new MyEvent(), options);
28+
29+
#endregion
30+
}
31+
32+
class MyEvent
33+
{
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Common;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
public static class HeaderWriter
9+
{
10+
public static string ToFriendlyString<TRootTypeToReplace>(IReadOnlyDictionary<string, string> headers)
11+
{
12+
var stringBuilder = new StringBuilder();
13+
foreach (var header in headers.OrderBy(x => x.Key))
14+
{
15+
var value = header.Value;
16+
value = value?.Replace("\r\n", "\n")
17+
.Replace("\n", "\r\n ")
18+
.Replace("`", "")
19+
.Replace(Environment.MachineName, "MACHINENAME")
20+
.Replace(Environment.UserName, "USERNAME");
21+
stringBuilder.Append($"{header.Key} = {value}\r\n");
22+
}
23+
var type = typeof(TRootTypeToReplace);
24+
return stringBuilder.ToString()
25+
.Replace($"{type.Name}+", "MyNamespace.")
26+
.Replace($", {type.Assembly.GetName().Name},", ", MyAssembly,");
27+
}
28+
}

0 commit comments

Comments
 (0)