This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum DbLocation | |
{ | |
Unknown, | |
Local, | |
Azure | |
} | |
public interface IMongoDbConfiguration | |
{ | |
string DatabaseName { get; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public IMongoDatabase GetDatabaseInstance() | |
{ | |
var settings = MongoClientSettings.FromUrl(new MongoUrl(Configuration.ConnectionString)); | |
settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 }; | |
var client = new MongoClient(settings); | |
return client.GetDatabase(Configuration.DatabaseName); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task EnsureDatabaseIsCreatedAsync() | |
{ | |
using (var client = GetDocumentClient()) | |
{ | |
//set the throughput for the database | |
var options = new RequestOptions { OfferThroughput = Configuration.AzureDatabaseInitialThroughput }; | |
//create the database without overriding any existing one | |
await client.CreateDatabaseIfNotExistsAsync(new Microsoft.Azure.Documents.Database { Id = Configuration.DatabaseName }, options); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var partitionKey = "_id"; | |
var partition = new BsonDocument { | |
{"shardCollection", $"{Database.DatabaseNamespace.DatabaseName}.{collectionName}"}, | |
{"key", new BsonDocument {{partitionKey, "hashed"}}} | |
}; | |
var command = new BsonDocumentCommand<BsonDocument>(partition); | |
await Database.RunCommandAsync(command); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var localEnv = new MongoDbConfiguration( | |
databaseName: "LocalDatabaseName", | |
databaseLocation: DbLocation.Local, | |
connectionString: "mongodb://localhost:27017"); | |
var cloudEnv = new MongoDbConfiguration( | |
databaseName: "CloudDatabaseName", | |
databaseLocation: DbLocation.Azure, | |
connectionString: "mongodb://{AzureCosmosAccount}:{AzureCosmosAccountPrimaryKey}==@{AzureCosmosAccount}.documents.azure.com:10255/?ssl=true&replicaSet=globaldb" | |
azureDatabaseInitialThroughput: 2000, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { /*TryCreateShardedCollection*/ } | |
catch | |
{ | |
await Database.CreateCollectionAsync(collectionName); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var database = client.CreateDatabaseQuery().Where(db => db.Id == Configuration.DatabaseName).ToList().FirstOrDefault(); | |
var feedResult = await client.ReadDocumentCollectionFeedAsync(UriFactory.CreateDatabaseUri(database.Id)); | |
var collectionCount = feedResult.Count; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// OneWay Subscription Examples | |
// Subscribe OneWay message with default context, using a weak reference | |
var token = NotificationService.Subscribe<MyMessage>(HandleOneWaySubscriptionAsync); | |
var token = NotificationService.Subscribe(typeof(MyMessage), HandleOneWaySubscriptionAsync); | |
// Subscribe OneWay message with specified context, using a weak reference | |
var token = NotificationService.Subscribe<MyMessage>(HandleOneWaySubscriptionAsync, "myContext"); | |
var token = NotificationService.Subscribe(typeof(MyMessage), HandleOneWaySubscriptionAsync, "myContext"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Unsubscribe message with specified token | |
NotificationService.Unsubscribe(token); | |
// Unsubscribe all messages from a particular message type | |
NotificationService.Unsubscribe<MyMessage>(); | |
NotificationService.Unsubscribe(typeof(MyMessage)); |
OlderNewer