Skip to content

Commit 871a656

Browse files
MongoDB v6 upgrade guide and snippets (#7804)
* Simple sample update * Start upgrade guide * Langversion * Update snippets and upgrade guide * Snippets and partials * Redundancy * Clearer guidance * Fix another langversion * Apply suggestions from code review Co-authored-by: Adam <[email protected]> * Outbox Sample * Fix package reference * DisableReadFallback * Linking * Stable --------- Co-authored-by: Daniel Marbach <[email protected]> Co-authored-by: Adam <[email protected]>
1 parent 60d4dfd commit 871a656

Some content is hidden

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

54 files changed

+1134
-16
lines changed

Snippets/MongoDB/MongoDB.sln

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB_5", "MongoDB_5\Mong
1212
EndProject
1313
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB_6", "MongoDB_6\MongoDB_6.csproj", "{58940814-447D-457B-B541-1BFB51820F7F}"
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB_7", "MongoDB_7\MongoDB_7.csproj", "{2146CB07-4B91-40AA-B52B-69A6A6302743}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,8 @@ Global
2729
{D114D094-6ABB-4922-832B-ECBF8A213228}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{58940814-447D-457B-B541-1BFB51820F7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2931
{58940814-447D-457B-B541-1BFB51820F7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{2146CB07-4B91-40AA-B52B-69A6A6302743}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{2146CB07-4B91-40AA-B52B-69A6A6302743}.Debug|Any CPU.Build.0 = Debug|Any CPU
3034
EndGlobalSection
3135
GlobalSection(SolutionProperties) = preSolution
3236
HideSolutionNode = FALSE
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using MongoDB.Driver;
3+
using NServiceBus;
4+
using NServiceBus.Storage.MongoDB;
5+
6+
#region MongoDBClientProvider
7+
8+
class CustomMongoClientProvider
9+
: IMongoClientProvider
10+
{
11+
// get fully configured via DI
12+
public CustomMongoClientProvider(IMongoClient mongoClient)
13+
{
14+
Client = mongoClient;
15+
}
16+
public IMongoClient Client { get; }
17+
}
18+
#endregion
19+
20+
class CustomMongoClientProviderRegistration
21+
{
22+
public CustomMongoClientProviderRegistration(EndpointConfiguration endpointConfiguration)
23+
{
24+
#region MongoDBCustomClientProviderRegistration
25+
26+
endpointConfiguration.RegisterComponents(c => c.AddTransient<IMongoClientProvider, CustomMongoClientProvider>());
27+
28+
#endregion
29+
}
30+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net10.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
44
</PropertyGroup>
55
<ItemGroup>
6-
<PackageReference Include="NServiceBus.Storage.MongoDB" Version="6.0.0-alpha.2" />
6+
<PackageReference Include="NServiceBus.Storage.MongoDB" Version="6.*" />
77
</ItemGroup>
88
</Project>

Snippets/MongoDB/MongoDB_6/Usage.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,28 @@ void TimeToKeepOutboxDeduplicationData(EndpointConfiguration endpointConfigurati
4747
{
4848
#region MongoDBOutboxCleanup
4949

50+
var outbox = endpointConfiguration.EnableOutbox();
51+
outbox.TimeToKeepOutboxDeduplicationData(TimeSpan.FromDays(30));
52+
53+
#endregion
54+
}
55+
56+
void DisableReadFallback(EndpointConfiguration endpointConfiguration)
57+
{
58+
#region MongoDBDisableReadFallback
59+
60+
var outbox = endpointConfiguration.EnableOutbox();
61+
outbox.DisableReadFallback();
62+
63+
#endregion
64+
}
65+
66+
void DisableInstaller(EndpointConfiguration endpointConfiguration)
67+
{
68+
#region MongoDBDisableInstaller
69+
5070
var persistence = endpointConfiguration.UsePersistence<MongoPersistence>();
51-
persistence.TimeToKeepOutboxDeduplicationData(TimeSpan.FromDays(30));
71+
persistence.DisableInstaller();
5272

5373
#endregion
5474
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using MongoDB.Driver;
3+
using NServiceBus;
4+
using NServiceBus.Storage.MongoDB;
5+
6+
#region MongoDBClientProvider
7+
8+
class CustomMongoClientProvider
9+
: IMongoClientProvider
10+
{
11+
// get fully configured via DI
12+
public CustomMongoClientProvider(IMongoClient mongoClient)
13+
{
14+
Client = mongoClient;
15+
}
16+
public IMongoClient Client { get; }
17+
}
18+
#endregion
19+
20+
class CustomMongoClientProviderRegistration
21+
{
22+
public CustomMongoClientProviderRegistration(EndpointConfiguration endpointConfiguration)
23+
{
24+
#region MongoDBCustomClientProviderRegistration
25+
26+
endpointConfiguration.RegisterComponents(c => c.AddTransient<IMongoClientProvider, CustomMongoClientProvider>());
27+
28+
#endregion
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using MongoDB.Bson;
4+
using MongoDB.Driver;
5+
6+
class DocumentVersion
7+
{
8+
async Task UpdateWithVersion(IMongoCollection<BsonDocument> collection, string versionFieldName, UpdateDefinitionBuilder<BsonDocument> updateBuilder, int currentVersion, Guid documentId)
9+
{
10+
#region MongoDBUpdateWithVersion
11+
12+
UpdateDefinition<BsonDocument> updateDefinition = updateBuilder.Inc(versionFieldName, 1);
13+
FilterDefinition<BsonDocument> filterDefinition = Builders<BsonDocument>.Filter.Eq("_id", documentId)
14+
& Builders<BsonDocument>.Filter.Eq(versionFieldName, currentVersion);
15+
16+
//Define other update operations on the document
17+
18+
var modifiedDocument = await collection.FindOneAndUpdateAsync(
19+
filter: filterDefinition,
20+
update: updateDefinition,
21+
options: new FindOneAndUpdateOptions<BsonDocument, BsonDocument> { IsUpsert = false, ReturnDocument = ReturnDocument.After });
22+
23+
if (modifiedDocument == null)
24+
{
25+
//The document was not updated because the version was already incremented.
26+
}
27+
28+
#endregion
29+
}
30+
}
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.Storage.MongoDB" Version="7.0.0-alpha.1" />
7+
</ItemGroup>
8+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
4+
class SharedTransaction : IHandleMessages<MyMessage>
5+
{
6+
#region MongoDBHandlerSharedTransaction
7+
8+
public Task Handle(MyMessage message, IMessageHandlerContext context)
9+
{
10+
var session = context.SynchronizedStorageSession.GetClientSession();
11+
var collection = session.Client.GetDatabase("mydatabase").GetCollection<MyBusinessObject>("mycollection");
12+
return collection.InsertOneAsync(session, new MyBusinessObject(), null, context.CancellationToken);
13+
}
14+
15+
#endregion
16+
}
17+
18+
class MyMessage
19+
{
20+
}
21+
22+
class MyBusinessObject
23+
{
24+
}
25+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus.Storage.MongoDB;
3+
4+
public class SharedTransactionDI
5+
{
6+
#region MongoDBSharedTransactionDI
7+
class MyService
8+
{
9+
IMongoSynchronizedStorageSession sharedSession;
10+
11+
// Resolved from DI container
12+
public MyService(IMongoSynchronizedStorageSession sharedSession)
13+
{
14+
this.sharedSession = sharedSession;
15+
}
16+
17+
public Task Create()
18+
{
19+
return sharedSession.MongoSession.Client
20+
.GetDatabase("mydatabase")
21+
.GetCollection<MyBusinessObject>("mycollection")
22+
.InsertOneAsync(sharedSession.MongoSession, new MyBusinessObject());
23+
}
24+
}
25+
#endregion
26+
27+
class MyBusinessObject
28+
{
29+
}
30+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using MongoDB.Driver;
3+
using NServiceBus;
4+
5+
public class Usage
6+
{
7+
Usage(EndpointConfiguration endpointConfiguration)
8+
{
9+
#region MongoDBUsage
10+
11+
endpointConfiguration.UsePersistence<MongoPersistence>();
12+
13+
#endregion
14+
}
15+
16+
void MongoClient(EndpointConfiguration endpointConfiguration)
17+
{
18+
#region MongoDBClient
19+
20+
var persistence = endpointConfiguration.UsePersistence<MongoPersistence>();
21+
persistence.MongoClient(new MongoClient("SharedMongoUrl"));
22+
23+
#endregion
24+
}
25+
26+
void DatabaseName(EndpointConfiguration endpointConfiguration)
27+
{
28+
#region MongoDBDatabaseName
29+
30+
var persistence = endpointConfiguration.UsePersistence<MongoPersistence>();
31+
persistence.DatabaseName("DatabaseName");
32+
33+
#endregion
34+
}
35+
36+
void UseTransactions(EndpointConfiguration endpointConfiguration)
37+
{
38+
#region MongoDBDisableTransactions
39+
40+
var persistence = endpointConfiguration.UsePersistence<MongoPersistence>();
41+
persistence.UseTransactions(false);
42+
43+
#endregion
44+
}
45+
46+
void TimeToKeepOutboxDeduplicationData(EndpointConfiguration endpointConfiguration)
47+
{
48+
#region MongoDBOutboxCleanup
49+
50+
var outbox = endpointConfiguration.EnableOutbox();
51+
outbox.TimeToKeepOutboxDeduplicationData(TimeSpan.FromDays(30));
52+
53+
#endregion
54+
}
55+
56+
void DisableReadFallback(EndpointConfiguration endpointConfiguration)
57+
{
58+
#region MongoDBDisableReadFallback
59+
60+
var outbox = endpointConfiguration.EnableOutbox();
61+
outbox.DisableReadFallback();
62+
63+
#endregion
64+
}
65+
66+
void DisableInstaller(EndpointConfiguration endpointConfiguration)
67+
{
68+
#region MongoDBDisableInstaller
69+
70+
var persistence = endpointConfiguration.UsePersistence<MongoPersistence>();
71+
persistence.DisableInstaller();
72+
73+
#endregion
74+
}
75+
76+
void SBMakoCompatibility(EndpointConfiguration endpointConfiguration)
77+
{
78+
#region MongoDBSBMakoCompatibility
79+
80+
var persistence = endpointConfiguration.UsePersistence<MongoPersistence>();
81+
var compatibility = persistence.CommunityPersistenceCompatibility();
82+
compatibility.CollectionNamingConvention(type => type.Name);
83+
compatibility.VersionElementName("DocumentVersion");
84+
85+
#endregion
86+
}
87+
88+
void TekmavenCompatibility(EndpointConfiguration endpointConfiguration)
89+
{
90+
#region MongoDBTekmavenCompatibility
91+
92+
var persistence = endpointConfiguration.UsePersistence<MongoPersistence>();
93+
var compatibility = persistence.CommunityPersistenceCompatibility();
94+
compatibility.VersionElementName("Version");
95+
96+
#endregion
97+
}
98+
}

0 commit comments

Comments
 (0)