Skip to content

Commit 9ce78b4

Browse files
Sample Update - Transactional Session With Send-only endpoint (#7191)
* WIP Transactional Session Sample * WIP * WIP * Change to console title * Summary reduced to under 160 chars * Summary reduced to under 160 chars - Attempt 2 * fixing content checks * fixing errors with integrity tests * fix content checks * fixing content checks * nuget alias of txsession sqlp * Tweaks * Fix link * formatting --------- Co-authored-by: Andreas Öhlund <[email protected]>
1 parent e647c03 commit 9ce78b4

21 files changed

+182
-92
lines changed

samples/transactional-session/aspnetcore-webapi/SqlPersistenceTS_8/CoreWebAPI.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@ VisualStudioVersion = 17.2.32630.192
44
MinimumVisualStudioVersion = 15.0.26730.12
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication", "WebApplication\WebApplication.csproj", "{EC947750-3A9B-4822-A7D9-BC7E837784A1}"
66
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Processor", "Processor\Processor.csproj", "{B5E4035E-FFEC-41E6-ABAE-7B2835531E6B}"
8+
EndProject
9+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Receiver", "Receiver\Receiver.csproj", "{D9CD05FE-0755-4B65-832A-2B99EEED80BA}"
10+
EndProject
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Data", "Data\Data.csproj", "{97E67D4C-4B76-4BE3-8BBC-F6F3221984C7}"
12+
EndProject
713
Global
814
GlobalSection(SolutionConfigurationPlatforms) = preSolution
915
Debug|Any CPU = Debug|Any CPU
1016
EndGlobalSection
1117
GlobalSection(ProjectConfigurationPlatforms) = postSolution
1218
{EC947750-3A9B-4822-A7D9-BC7E837784A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1319
{EC947750-3A9B-4822-A7D9-BC7E837784A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{B5E4035E-FFEC-41E6-ABAE-7B2835531E6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{B5E4035E-FFEC-41E6-ABAE-7B2835531E6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{D9CD05FE-0755-4B65-832A-2B99EEED80BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{D9CD05FE-0755-4B65-832A-2B99EEED80BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{97E67D4C-4B76-4BE3-8BBC-F6F3221984C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{97E67D4C-4B76-4BE3-8BBC-F6F3221984C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
1426
EndGlobalSection
1527
GlobalSection(SolutionProperties) = preSolution
1628
HideSolutionNode = FALSE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
5+
<LangVersion>12.0</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="NServiceBus" Version="9.*" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.*" />
12+
</ItemGroup>
13+
</Project>

samples/transactional-session/aspnetcore-webapi/SqlPersistenceTS_8/WebApplication/MyDataContext.cs renamed to samples/transactional-session/aspnetcore-webapi/SqlPersistenceTS_8/Data/MyDataContext.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
using Microsoft.EntityFrameworkCore;
22

3-
public class MyDataContext : DbContext
3+
public class MyDataContext(DbContextOptions options) : DbContext(options)
44
{
5-
public MyDataContext(DbContextOptions options)
6-
: base(options)
7-
{
8-
}
9-
105
public DbSet<MyEntity> MyEntities { get; set; }
116

127
protected override void OnModelCreating(ModelBuilder modelBuilder)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class MyEntity
2+
{
3+
public required string Id { get; set; }
4+
public bool Processed { get; set; }
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class MyMessage : IMessage
2+
{
3+
public string? EntityId { get; set; }
4+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>exe</OutputType>
5+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
6+
<LangVersion>12.0</LangVersion>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<PackageReference Include="NServiceBus" Version="9.*" />
12+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="3.*"/>
13+
<PackageReference Include="NServiceBus.Persistence.Sql.TransactionalSession" Version="8.*"/>
14+
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.*" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.Data.SqlClient;
2+
using Microsoft.Extensions.Hosting;
3+
using NServiceBus.TransactionalSession;
4+
5+
Console.Title = "Processor";
6+
7+
var builder = Host.CreateApplicationBuilder(args);
8+
9+
// for SqlExpress use Data Source=.\SqlExpress;Initial Catalog=nservicebus;Integrated Security=True;Encrypt=false
10+
const string ConnectionString = @"Server=localhost,1433;Initial Catalog=nservicebus;User Id=SA;Password=yourStrong(!)Password;Encrypt=false";
11+
12+
var endpointConfiguration = new EndpointConfiguration("Processor");
13+
14+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
15+
16+
endpointConfiguration.EnableInstallers();
17+
18+
endpointConfiguration.UseTransport(new LearningTransport { TransportTransactionMode = TransportTransactionMode.ReceiveOnly });
19+
20+
var persistence = endpointConfiguration.UsePersistence<SqlPersistence>();
21+
persistence.SqlDialect<SqlDialect.MsSqlServer>();
22+
persistence.ConnectionBuilder(() => new SqlConnection(ConnectionString));
23+
24+
#region txsession-nsb-processor-configuration
25+
persistence.EnableTransactionalSession();
26+
endpointConfiguration.EnableOutbox();
27+
#endregion
28+
29+
builder.UseNServiceBus(endpointConfiguration);
30+
31+
await builder.Build().RunAsync();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Logging;
3+
4+
#region txsession-handler
5+
6+
public class MyHandler(MyDataContext dataContext, ILogger<MyHandler> logger) : IHandleMessages<MyMessage>
7+
{
8+
public async Task Handle(MyMessage message, IMessageHandlerContext context)
9+
{
10+
logger.LogInformation("Message received at endpoint");
11+
12+
var entity = await dataContext.MyEntities.Where(e => e.Id == message.EntityId)
13+
.FirstAsync(cancellationToken: context.CancellationToken);
14+
entity.Processed = true;
15+
}
16+
}
17+
18+
#endregion
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.Data.SqlClient;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
6+
Console.Title = "Receiver";
7+
8+
var builder = Host.CreateApplicationBuilder(args);
9+
10+
// for SqlExpress use Data Source=.\SqlExpress;Initial Catalog=nservicebus;Integrated Security=True;Encrypt=false
11+
const string ConnectionString = @"Server=localhost,1433;Initial Catalog=nservicebus;User Id=SA;Password=yourStrong(!)Password;Encrypt=false";
12+
13+
builder.Services.AddDbContext<MyDataContext>(o => o.UseSqlServer(new SqlConnection(ConnectionString)));
14+
15+
var endpointConfiguration = new EndpointConfiguration("Sample.Receiver");
16+
17+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
18+
19+
endpointConfiguration.EnableInstallers();
20+
21+
endpointConfiguration.UseTransport(new LearningTransport { TransportTransactionMode = TransportTransactionMode.ReceiveOnly });
22+
23+
builder.UseNServiceBus(endpointConfiguration);
24+
25+
await builder.Build().RunAsync();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>exe</OutputType>
5+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
6+
<LangVersion>12.0</LangVersion>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="3.*" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.*" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\Data\Data.csproj" />
16+
</ItemGroup>
17+
</Project>

0 commit comments

Comments
 (0)