Skip to content

Commit 8e672ac

Browse files
Add NServiceBus 10 version (#7614)
1 parent f653a3c commit 8e672ac

17 files changed

+299
-0
lines changed
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+
<ProjectReference Include="..\Shared\Shared.csproj" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
using NServiceBus.Logging;
4+
5+
public class OrderCompletedHandler :
6+
IHandleMessages<OrderCompleted>
7+
{
8+
static ILog log = LogManager.GetLogger<OrderCompletedHandler>();
9+
10+
public Task Handle(OrderCompleted message, IMessageHandlerContext context)
11+
{
12+
log.Info($"Received OrderCompleted for OrderId {message.OrderId}");
13+
return Task.CompletedTask;
14+
}
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NServiceBus;
4+
5+
Console.Title = "Client";
6+
var endpointConfiguration = new EndpointConfiguration("Samples.NHibernate.Client");
7+
endpointConfiguration.EnableInstallers();
8+
endpointConfiguration.UseTransport(new LearningTransport());
9+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
10+
11+
var endpointInstance = await Endpoint.Start(endpointConfiguration);
12+
13+
Console.WriteLine("Press 'enter' to send a StartOrder messages");
14+
Console.WriteLine("Press any other key to exit");
15+
16+
while (true)
17+
{
18+
var key = Console.ReadKey();
19+
Console.WriteLine();
20+
21+
if (key.Key != ConsoleKey.Enter)
22+
{
23+
break;
24+
}
25+
26+
var orderId = Guid.NewGuid();
27+
var startOrder = new StartOrder
28+
{
29+
OrderId = orderId
30+
};
31+
await endpointInstance.Send("Samples.NHibernate.Server", startOrder);
32+
Console.WriteLine($"StartOrder Message sent with OrderId {orderId}");
33+
}
34+
35+
await endpointInstance.Stop();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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}") = "Server", "Server\Server.csproj", "{963CD471-E882-45C4-A884-96592FA6E730}"
6+
EndProject
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{A7BBC993-9A52-4CA8-9CD8-22FBEAFF7D1B}"
8+
EndProject
9+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{65572BB2-1E39-463A-87BF-53B84DBA7D94}"
10+
EndProject
11+
Global
12+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
13+
Debug|Any CPU = Debug|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{963CD471-E882-45C4-A884-96592FA6E730}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{963CD471-E882-45C4-A884-96592FA6E730}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{A7BBC993-9A52-4CA8-9CD8-22FBEAFF7D1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{A7BBC993-9A52-4CA8-9CD8-22FBEAFF7D1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{65572BB2-1E39-463A-87BF-53B84DBA7D94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{65572BB2-1E39-463A-87BF-53B84DBA7D94}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
EndGlobalSection
23+
GlobalSection(SolutionProperties) = preSolution
24+
HideSolutionNode = FALSE
25+
EndGlobalSection
26+
GlobalSection(ExtensibilityGlobals) = postSolution
27+
SolutionGuid = {C1C82675-E1DD-470C-965E-315B5E508EE9}
28+
EndGlobalSection
29+
EndGlobal
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class CompleteOrder
2+
{
3+
public string OrderDescription { get; set; }
4+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+

2+
using System;
3+
using System.Threading.Tasks;
4+
using NServiceBus;
5+
using NServiceBus.Logging;
6+
7+
#region OrderSaga
8+
9+
public class OrderSaga :
10+
Saga<OrderSagaData>,
11+
IAmStartedByMessages<StartOrder>,
12+
IHandleTimeouts<CompleteOrder>
13+
{
14+
static ILog log = LogManager.GetLogger<OrderSaga>();
15+
16+
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<OrderSagaData> mapper)
17+
{
18+
mapper.ConfigureMapping<StartOrder>(message => message.OrderId)
19+
.ToSaga(sagaData => sagaData.OrderId);
20+
}
21+
22+
public Task Handle(StartOrder message, IMessageHandlerContext context)
23+
{
24+
Data.OrderId = message.OrderId;
25+
var orderDescription = $"The saga for order {message.OrderId}";
26+
Data.OrderDescription = orderDescription;
27+
log.Info($"Received StartOrder message {Data.OrderId}. Starting Saga");
28+
29+
var shipOrder = new ShipOrder
30+
{
31+
OrderId = message.OrderId
32+
};
33+
34+
log.Info("Order will complete in 5 seconds");
35+
var timeoutData = new CompleteOrder
36+
{
37+
OrderDescription = orderDescription
38+
};
39+
40+
return Task.WhenAll(
41+
context.SendLocal(shipOrder),
42+
RequestTimeout(context, TimeSpan.FromSeconds(5), timeoutData)
43+
);
44+
}
45+
46+
public Task Timeout(CompleteOrder state, IMessageHandlerContext context)
47+
{
48+
log.Info($"Saga with OrderId {Data.OrderId} completed");
49+
var orderCompleted = new OrderCompleted
50+
{
51+
OrderId = Data.OrderId
52+
};
53+
MarkAsComplete();
54+
return context.Publish(orderCompleted);
55+
}
56+
}
57+
#endregion
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using NServiceBus;
3+
4+
#region sagadata
5+
public class OrderSagaData :
6+
ContainSagaData
7+
{
8+
public virtual Guid OrderId { get; set; }
9+
public virtual string OrderDescription { get; set; }
10+
}
11+
#endregion
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
public class OrderShipped
4+
{
5+
public virtual Guid Id { get; set; }
6+
public virtual DateTime ShippingDate { get; set; }
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using NHibernate.Mapping.ByCode.Conformist;
2+
3+
public class OrderShippedMap :
4+
ClassMapping<OrderShipped>
5+
{
6+
public OrderShippedMap()
7+
{
8+
Id(x => x.Id);
9+
Property(x => x.ShippingDate);
10+
}
11+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NHibernate.Cfg;
4+
using NHibernate.Dialect;
5+
using NHibernate.Driver;
6+
using NHibernate.Mapping.ByCode;
7+
using NServiceBus;
8+
using NServiceBus.Persistence;
9+
10+
Console.Title = "Server";
11+
12+
#region Config
13+
14+
var endpointConfiguration = new EndpointConfiguration("Samples.NHibernate.Server");
15+
var persistence = endpointConfiguration.UsePersistence<NHibernatePersistence>();
16+
17+
// for SqlExpress use Data Source=.\SqlExpress;Initial Catalog=Samples.NHibernate;Integrated Security=True;Max Pool Size=100;Encrypt=false
18+
var connectionString = @"Server=localhost,1433;Initial Catalog=Samples.NHibernate;User Id=SA;Password=yourStrong(!)Password;Max Pool Size=100;Encrypt=false";
19+
var hibernateConfig = new Configuration();
20+
hibernateConfig.DataBaseIntegration(x =>
21+
{
22+
x.ConnectionString = connectionString;
23+
x.Dialect<MsSql2012Dialect>();
24+
x.Driver<MicrosoftDataSqlClientDriver>();
25+
});
26+
27+
AddMappings(hibernateConfig);
28+
29+
persistence.UseConfiguration(hibernateConfig);
30+
31+
#endregion
32+
33+
endpointConfiguration.UseTransport(new LearningTransport());
34+
endpointConfiguration.EnableInstallers();
35+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
36+
37+
var endpointInstance = await Endpoint.Start(endpointConfiguration);
38+
39+
Console.WriteLine("Press any key to exit");
40+
Console.ReadKey();
41+
42+
await endpointInstance.Stop();
43+
44+
static void AddMappings(Configuration nhConfiguration)
45+
{
46+
var mapper = new ModelMapper();
47+
mapper.AddMappings(typeof(OrderShipped).Assembly.GetTypes());
48+
nhConfiguration.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
49+
}

0 commit comments

Comments
 (0)