Skip to content

Commit d3be9d0

Browse files
Add NSB10 Version of CosmosDB Snippets (#7738)
* Add NSB10 Version of CosmosDB Snippets * Add prerelease.txt * Fix pruning warning (treat as error). * Add NSB10 snippets for CustomChecks * Resolve pruning warnings * Add NSB10 snippets for DataBus * Remove unused packages. * Add prerelease.txt * Add NSB10 snippets for DynamoDB * Add NSB10 snippets for Extensions.Hosting * Resolve pruning warnings * Add NSB10 snippets for Extensions.Logging * Add NSB10 snippets for FileShareDataBus * Add NSB10 snippets for Gateway
1 parent 3070f2e commit d3be9d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1802
-41
lines changed

Snippets/CosmosDB/CosmosDB.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CosmosDB_2", "CosmosDB_2\Co
88
EndProject
99
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CosmosDB_3", "CosmosDB_3\CosmosDB_3.csproj", "{7D300CEB-5AC8-453F-99BF-4166C27C7C60}"
1010
EndProject
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CosmosDB_4", "CosmosDB_4\CosmosDB_4.csproj", "{8973FA97-503D-6465-CB8B-B0EC295626BC}"
12+
EndProject
1113
Global
1214
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1315
Debug|Any CPU = Debug|Any CPU
@@ -26,6 +28,10 @@ Global
2628
{7D300CEB-5AC8-453F-99BF-4166C27C7C60}.Debug|Any CPU.Build.0 = Debug|Any CPU
2729
{7D300CEB-5AC8-453F-99BF-4166C27C7C60}.Release|Any CPU.ActiveCfg = Release|Any CPU
2830
{7D300CEB-5AC8-453F-99BF-4166C27C7C60}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{8973FA97-503D-6465-CB8B-B0EC295626BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{8973FA97-503D-6465-CB8B-B0EC295626BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{8973FA97-503D-6465-CB8B-B0EC295626BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{8973FA97-503D-6465-CB8B-B0EC295626BC}.Release|Any CPU.Build.0 = Release|Any CPU
2935
EndGlobalSection
3036
GlobalSection(SolutionProperties) = preSolution
3137
HideSolutionNode = FALSE

Snippets/CosmosDB/CosmosDB_1/CosmosDB_1.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<ItemGroup Label="Resolves vulnerabilities">
99
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
1010
<PackageReference Include="System.Drawing.Common" Version="4.*" />
11-
<PackageReference Include="System.Net.Http" Version="4.*" />
12-
<PackageReference Include="System.Security.Cryptography.Xml" Version="4.*" />
11+
<PackageReference Include="System.Security.Cryptography.Xml" Version="4.7.1" />
1312
</ItemGroup>
1413
</Project>
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" Version="4.0.0-alpha.2" />
7+
</ItemGroup>
8+
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Azure.Cosmos;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using NServiceBus;
4+
using NServiceBus.Persistence.CosmosDB;
5+
6+
#region CosmosDBCustomClientProvider
7+
8+
class CustomCosmosClientProvider
9+
: IProvideCosmosClient
10+
{
11+
// get fully configured via DI
12+
public CustomCosmosClientProvider(CosmosClient cosmosClient)
13+
{
14+
Client = cosmosClient;
15+
}
16+
public CosmosClient Client { get; }
17+
}
18+
#endregion
19+
20+
class CosmosDBCustomClientProviderRegistration
21+
{
22+
public CosmosDBCustomClientProviderRegistration(EndpointConfiguration endpointConfiguration)
23+
{
24+
#region CosmosDBCustomClientProviderRegistration
25+
26+
endpointConfiguration.RegisterComponents(c => c.AddTransient<IProvideCosmosClient, CustomCosmosClientProvider>());
27+
28+
#endregion
29+
}
30+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Azure.Cosmos;
3+
using NServiceBus;
4+
5+
class MyMessage
6+
{
7+
public string ItemId { get; set; }
8+
}
9+
10+
class UsageHandler : IHandleMessages<MyMessage>
11+
{
12+
ToDoActivity test1 = new ToDoActivity();
13+
ToDoActivity test2 = new ToDoActivity();
14+
ToDoActivity test3 = new ToDoActivity();
15+
16+
public class ToDoActivity
17+
{
18+
public string id { get; set; }
19+
}
20+
21+
#region CosmosDBHandlerSharedTransaction
22+
public Task Handle(MyMessage message, IMessageHandlerContext context)
23+
{
24+
//setup the items for the batch...
25+
26+
var session = context.SynchronizedStorageSession.CosmosPersistenceSession();
27+
28+
session.Batch
29+
.CreateItem(test1)
30+
.ReplaceItem(test2.id, test2)
31+
.UpsertItem(test3)
32+
.DeleteItem("/item/id");
33+
34+
return Task.CompletedTask;
35+
}
36+
#endregion
37+
}
38+
39+
#region CosmosDB-TransactionalBatchRegisteredWithDependencyInjectionResolvedInHandler
40+
41+
class MyHandler : IHandleMessages<MyMessage>
42+
{
43+
public MyHandler(ICosmosStorageSession storageSession)
44+
{
45+
transactionalBatch = storageSession.Batch;
46+
}
47+
48+
public Task Handle(MyMessage message, IMessageHandlerContext context)
49+
{
50+
transactionalBatch.DeleteItem(message.ItemId);
51+
52+
return Task.CompletedTask;
53+
}
54+
55+
private readonly TransactionalBatch transactionalBatch;
56+
}
57+
58+
#endregion
59+
60+
#region CosmosDB-TransactionalBatchRegisteredWithDependencyInjectionResolvedInCustomType
61+
62+
class MyCustomDependency
63+
{
64+
public MyCustomDependency(ICosmosStorageSession storageSession)
65+
{
66+
transactionalBatch = storageSession.Batch;
67+
}
68+
69+
public void DeleteItemInCosmos(string itemId)
70+
{
71+
transactionalBatch.DeleteItem(itemId);
72+
}
73+
74+
private readonly TransactionalBatch transactionalBatch;
75+
}
76+
77+
class MyHandlerWithCustomDependency : IHandleMessages<MyMessage>
78+
{
79+
public MyHandlerWithCustomDependency(MyCustomDependency customDependency)
80+
{
81+
this.customDependency = customDependency;
82+
}
83+
84+
public Task Handle(MyMessage message, IMessageHandlerContext context)
85+
{
86+
customDependency.DeleteItemInCosmos(message.ItemId);
87+
88+
return Task.CompletedTask;
89+
}
90+
91+
private readonly MyCustomDependency customDependency;
92+
}
93+
94+
#endregion

0 commit comments

Comments
 (0)