Skip to content

Commit 360590a

Browse files
Add NServiceBus 10 dependency injection externally managed mode sample (#7543)
* Add NServiceBus 10 dependency injection externally managed mode sample
1 parent fcdc406 commit 360590a

File tree

8 files changed

+118
-0
lines changed

8 files changed

+118
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 16
3+
VisualStudioVersion = 16.0.29728.190
4+
MinimumVisualStudioVersion = 15.0.26730.12
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "Sample\Sample.csproj", "{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
EndGlobalSection
15+
GlobalSection(SolutionProperties) = preSolution
16+
HideSolutionNode = FALSE
17+
EndGlobalSection
18+
GlobalSection(ExtensibilityGlobals) = postSolution
19+
SolutionGuid = {3673C976-8AFC-4169-A103-E016521D0868}
20+
EndGlobalSection
21+
EndGlobal
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using NServiceBus.Logging;
2+
3+
public class Greeter
4+
{
5+
private static readonly ILog log = LogManager.GetLogger<Greeter>();
6+
7+
public void SayHello()
8+
{
9+
log.Info("Hello from Greeter.");
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
4+
#region InjectingMessageSession
5+
public class MessageSender(IMessageSession messageSession)
6+
{
7+
public Task SendMessage()
8+
{
9+
var myMessage = new MyMessage();
10+
return messageSession.SendLocal(myMessage);
11+
}
12+
}
13+
#endregion
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
#region InjectingDependency
4+
public class MyHandler(Greeter greeter) :
5+
IHandleMessages<MyMessage>
6+
{
7+
public Task Handle(MyMessage message, IMessageHandlerContext context)
8+
{
9+
greeter.SayHello();
10+
return Task.CompletedTask;
11+
}
12+
}
13+
#endregion
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using NServiceBus;
2+
3+
public record MyMessage : IMessage;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using NServiceBus;
5+
6+
static class Program
7+
{
8+
static async Task Main()
9+
{
10+
Console.Title = "ExternallyManagedContainer";
11+
12+
var endpointConfiguration = new EndpointConfiguration("Sample");
13+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
14+
endpointConfiguration.UseTransport(new LearningTransport());
15+
16+
#region ContainerConfiguration
17+
18+
// ServiceCollection is provided by Microsoft.Extensions.DependencyInjection
19+
var serviceCollection = new ServiceCollection();
20+
21+
// most dependencies may now be registered
22+
serviceCollection.AddSingleton<Greeter>();
23+
serviceCollection.AddSingleton<MessageSender>();
24+
25+
// EndpointWithExternallyManagedContainer.Create accepts an IServiceCollection,
26+
// which is inherited by ServiceCollection
27+
var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer
28+
.Create(endpointConfiguration, serviceCollection);
29+
30+
// if IMessageSession is required as dependency, it may now be registered
31+
serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value);
32+
33+
#endregion
34+
35+
using (var serviceProvider = serviceCollection.BuildServiceProvider())
36+
{
37+
var endpoint = await endpointWithExternallyManagedContainer.Start(serviceProvider);
38+
39+
var sender = serviceProvider.GetRequiredService<MessageSender>();
40+
await sender.SendMessage();
41+
42+
Console.WriteLine("Press any key to exit");
43+
Console.ReadKey();
44+
await endpoint.Stop();
45+
}
46+
}
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
9+
</ItemGroup>
10+
</Project>

samples/dependency-injection/externally-managed-mode/Core_10/prerelease.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)