Skip to content

Commit 243fca0

Browse files
authored
Add NServiceBus 10 version of the Transactional session CosmosDB snippets (#7767)
* Add previously released version * Add NServiceBus 10 version of the Transactional session CosmosDB snippets
1 parent fe1ceb8 commit 243fca0

File tree

8 files changed

+200
-0
lines changed

8 files changed

+200
-0
lines changed

Snippets/TransactionalSession.CosmosDB/CosmosTS.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CosmosTS_1", "CosmosTS_1\Co
66
EndProject
77
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CosmosTS_2", "CosmosTS_2\CosmosTS_2.csproj", "{0C3CBD7B-503C-4F25-9AFC-5DDE1BD70362}"
88
EndProject
9+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CosmosTS_3", "CosmosTS_3\CosmosTS_3.csproj", "{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}"
10+
EndProject
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CosmosTS_4", "CosmosTS_4\CosmosTS_4.csproj", "{946086C4-7FF7-401A-B15D-43C828F6C212}"
12+
EndProject
913
Global
1014
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1115
Debug|Any CPU = Debug|Any CPU
@@ -40,6 +44,30 @@ Global
4044
{0C3CBD7B-503C-4F25-9AFC-5DDE1BD70362}.Release|x64.Build.0 = Release|Any CPU
4145
{0C3CBD7B-503C-4F25-9AFC-5DDE1BD70362}.Release|x86.ActiveCfg = Release|Any CPU
4246
{0C3CBD7B-503C-4F25-9AFC-5DDE1BD70362}.Release|x86.Build.0 = Release|Any CPU
47+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Debug|x64.ActiveCfg = Debug|Any CPU
50+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Debug|x64.Build.0 = Debug|Any CPU
51+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Debug|x86.ActiveCfg = Debug|Any CPU
52+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Debug|x86.Build.0 = Debug|Any CPU
53+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
54+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Release|Any CPU.Build.0 = Release|Any CPU
55+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Release|x64.ActiveCfg = Release|Any CPU
56+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Release|x64.Build.0 = Release|Any CPU
57+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Release|x86.ActiveCfg = Release|Any CPU
58+
{503BB695-D7C9-439A-8E83-A3E5B3A56AA4}.Release|x86.Build.0 = Release|Any CPU
59+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
60+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Debug|Any CPU.Build.0 = Debug|Any CPU
61+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Debug|x64.ActiveCfg = Debug|Any CPU
62+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Debug|x64.Build.0 = Debug|Any CPU
63+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Debug|x86.ActiveCfg = Debug|Any CPU
64+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Debug|x86.Build.0 = Debug|Any CPU
65+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Release|Any CPU.ActiveCfg = Release|Any CPU
66+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Release|Any CPU.Build.0 = Release|Any CPU
67+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Release|x64.ActiveCfg = Release|Any CPU
68+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Release|x64.Build.0 = Release|Any CPU
69+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Release|x86.ActiveCfg = Release|Any CPU
70+
{946086C4-7FF7-401A-B15D-43C828F6C212}.Release|x86.Build.0 = Release|Any CPU
4371
EndGlobalSection
4472
GlobalSection(SolutionProperties) = preSolution
4573
HideSolutionNode = FALSE
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using NServiceBus;
3+
using NServiceBus.TransactionalSession;
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.Azure.Cosmos;
7+
8+
public class CosmosDBConfig
9+
{
10+
public void Configure(EndpointConfiguration config)
11+
{
12+
#region enabling-transactional-session-cosmos
13+
14+
var persistence = config.UsePersistence<CosmosPersistence>();
15+
persistence.EnableTransactionalSession();
16+
17+
#endregion
18+
}
19+
20+
private async Task OpenDefault(IServiceProvider serviceProvider)
21+
{
22+
#region open-transactional-session-cosmos
23+
24+
using var childScope = serviceProvider.CreateScope();
25+
var session = childScope.ServiceProvider.GetService<ITransactionalSession>();
26+
await session.Open(
27+
new CosmosOpenSessionOptions(
28+
new PartitionKey("MyPartitionKey")));
29+
30+
// use the session
31+
32+
await session.Commit();
33+
34+
#endregion
35+
}
36+
37+
public async Task OpenContainerInfo(IServiceProvider serviceProvider)
38+
{
39+
#region open-transactional-session-cosmos-container
40+
41+
using var childScope = serviceProvider.CreateScope();
42+
var session = childScope.ServiceProvider.GetService<ITransactionalSession>();
43+
await session.Open(
44+
new CosmosOpenSessionOptions(
45+
new PartitionKey("MyPartitionKey"),
46+
new ContainerInformation(
47+
"MyContainer",
48+
new PartitionKeyPath("/path/to/partition/key"))));
49+
50+
// use the session
51+
52+
await session.Commit();
53+
54+
#endregion
55+
}
56+
57+
public async Task UseSession(ITransactionalSession session)
58+
{
59+
#region use-transactional-session-cosmos
60+
await session.Open(
61+
new CosmosOpenSessionOptions(
62+
new PartitionKey("MyPartitionKey")));
63+
64+
// add messages to the transaction:
65+
await session.Send(new MyMessage());
66+
67+
// access the database:
68+
var cosmosSession = session.SynchronizedStorageSession.CosmosPersistenceSession();
69+
70+
await session.Commit();
71+
#endregion
72+
}
73+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageReference Include="NServiceBus.Persistence.CosmosDB.TransactionalSession" Version="3.*" />
7+
</ItemGroup>
8+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using NServiceBus;
2+
3+
public class MyMessage : ICommand
4+
{
5+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using NServiceBus;
3+
using NServiceBus.TransactionalSession;
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.Azure.Cosmos;
7+
8+
public class CosmosDBConfig
9+
{
10+
public void Configure(EndpointConfiguration config)
11+
{
12+
#region enabling-transactional-session-cosmos
13+
14+
var persistence = config.UsePersistence<CosmosPersistence>();
15+
persistence.EnableTransactionalSession();
16+
17+
#endregion
18+
}
19+
20+
private async Task OpenDefault(IServiceProvider serviceProvider)
21+
{
22+
#region open-transactional-session-cosmos
23+
24+
using var childScope = serviceProvider.CreateScope();
25+
var session = childScope.ServiceProvider.GetService<ITransactionalSession>();
26+
await session.Open(
27+
new CosmosOpenSessionOptions(
28+
new PartitionKey("MyPartitionKey")));
29+
30+
// use the session
31+
32+
await session.Commit();
33+
34+
#endregion
35+
}
36+
37+
public async Task OpenContainerInfo(IServiceProvider serviceProvider)
38+
{
39+
#region open-transactional-session-cosmos-container
40+
41+
using var childScope = serviceProvider.CreateScope();
42+
var session = childScope.ServiceProvider.GetService<ITransactionalSession>();
43+
await session.Open(
44+
new CosmosOpenSessionOptions(
45+
new PartitionKey("MyPartitionKey"),
46+
new ContainerInformation(
47+
"MyContainer",
48+
new PartitionKeyPath("/path/to/partition/key"))));
49+
50+
// use the session
51+
52+
await session.Commit();
53+
54+
#endregion
55+
}
56+
57+
public async Task UseSession(ITransactionalSession session)
58+
{
59+
#region use-transactional-session-cosmos
60+
await session.Open(
61+
new CosmosOpenSessionOptions(
62+
new PartitionKey("MyPartitionKey")));
63+
64+
// add messages to the transaction:
65+
await session.Send(new MyMessage());
66+
67+
// access the database:
68+
var cosmosSession = session.SynchronizedStorageSession.CosmosPersistenceSession();
69+
70+
await session.Commit();
71+
#endregion
72+
}
73+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageReference Include="NServiceBus.Persistence.CosmosDB.TransactionalSession" Version="4.0.0-alpha.2" />
7+
</ItemGroup>
8+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using NServiceBus;
2+
3+
public class MyMessage : ICommand
4+
{
5+
}

Snippets/TransactionalSession.CosmosDB/CosmosTS_4/prerelease.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)