Skip to content

Commit 6db75e5

Browse files
committed
Make a TLDR snippet
1 parent 06ccda5 commit 6db75e5

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

Snippets/Core/Core_10/Handlers/ConventionBasedHandlers.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
using System.Net.Http;
12
using System.Threading;
23
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Configuration;
35
using NServiceBus;
46

57
namespace OuterNS.InnerNS;
68

79
public record MyMessage();
10+
public record Msg1();
11+
public record Msg2();
812

913
#region SimpleConventionBasedHandler
1014

@@ -45,7 +49,31 @@ public async Task Handle(MyMessage message, IMessageHandlerContext context)
4549

4650
#endregion
4751

52+
53+
#region ConventionHandlersTldrSample
54+
55+
[Handler]
56+
public class HandlersByConvention
57+
{
58+
public async Task Handle(Msg1 message,
59+
IMessageHandlerContext context,
60+
CancellationToken cancellationToken)
61+
{
62+
}
63+
64+
public static async Task Handle(Msg2 message,
65+
IMessageHandlerContext context,
66+
DatabaseService database,
67+
IConfiguration configuration,
68+
IHttpClientFactory httpClientFactory,
69+
CancellationToken cancellationToken)
70+
{
71+
}
72+
}
73+
#endregion
74+
4875
public class DatabaseService;
76+
public class
4977

5078
public class ConventionHandlerRegistration
5179
{
@@ -86,7 +114,4 @@ public static void Setup(EndpointConfiguration endpointConfiguration)
86114
}
87115

88116
[HandlerRegistryExtensions(EntryPointName = "SampleProject")]
89-
public static partial class RegistrationExtensions
90-
{
91-
92-
}
117+
public static partial class RegistrationExtensions;

nservicebus/handlers/convention-based.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ reviewed: 2026-05-04
77
related:
88
---
99

10-
Starting with NServiceBus version 10.2.0, NServiceBus supports **convention-based message handlers** that do not implement `IHandleMessages<T>`. These handlers are not discovered through traditional assembly scanning, but instead are either added to an NServiceBus endpoint declaratively, or with help from Roslyn analyzers and source generators.
10+
Starting with NServiceBus version 10.2.0, NServiceBus supports **convention-based message handlers** that do not implement `IHandleMessages<T>`, enabling handlers to be expressed like this:
11+
12+
snippet: ConventionHandlersTldrSample
13+
14+
And then registered on the endpoint like this:
15+
16+
snippet: ConventionHandlerAddAllFromAssembly
17+
18+
Convention-based handlers are not discovered through traditional assembly scanning, but instead are either added to an NServiceBus endpoint declaratively, or with help from Roslyn analyzers and source generators.
1119

1220
## Handler structure
1321

@@ -27,8 +35,7 @@ These requirements must be met for a class to be recognized as a convention-base
2735
- After the first two parameters, any additional parameters must be either a `CancellationToken` or Services registered in the host's `IServiceCollection`.
2836
- It can be an instance or static method.
2937
- A handler class may contain multiple handler methods, differing by the message type, but these methods become an inseparable unit. It is not possible to register one handler method on a class but not the other.
30-
31-
TODO: What happens if there are 2 handler methods for the same message type but with different additional params?
38+
- If multiple handler methods use the same message type as the first parameter, they will all be executed on the same message.
3239

3340
## Registering handlers
3441

0 commit comments

Comments
 (0)