Skip to content

Instantly share code, notes, and snippets.

@zleao
zleao / zoft.NotificationService.PublishExamples.cs
Created January 10, 2021 15:05
Examples of Publish using zoft.NotificationService
// OneWay Publish
// Publish an one-way message for default or specified context
NotificationService.PublishAsync(myMessage);
NotificationService.PublishAsync(myMessage, "myContext");
// Adds a message to the pending notification queue
NotificationService.DelayedPublishAsync(myMessage);
// Adds a message to the pending notification queue for the specified context
NotificationService.DelayedPublishAsync(myMessage, "myContext");
@zleao
zleao / zoft.NotificationService.UnsubscriptionExamples.cs
Created January 10, 2021 14:48
Examples of unsubscriptions using zoft.NotificationService
// Unsubscribe message with specified token
NotificationService.Unsubscribe(token);
// Unsubscribe all messages from a particular message type
NotificationService.Unsubscribe<MyMessage>();
NotificationService.Unsubscribe(typeof(MyMessage));
@zleao
zleao / zoft.NotificationService.SubscriptionExamples.cs
Last active January 10, 2021 14:45
Examples of subscriptions using zoft.NotificationService
// 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");
@zleao
zleao / PortableMongoDB.GetCollectionCount.cs
Created July 14, 2020 12:27
Portable MongoDB snippet to get a count of the existing collections in a database
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;
@zleao
zleao / PortableMongoDB.CreateCollections.cs
Created July 14, 2020 12:12
Metacode for hybruid support of collection creation for portable MongoDB
try { /*TryCreateShardedCollection*/ }
catch
{
await Database.CreateCollectionAsync(collectionName);
}
@zleao
zleao / PortableMongoDB.SampleConfigurations.cs
Created July 14, 2020 09:19
Sample environment configurations for the PortableMongoDB solution
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,
@zleao
zleao / PortableMongoDB.SharedThroughputScaleDown.cs
Last active July 14, 2020 12:29
Scale down the shared throughput of a database
...
//Get CollectionCount
...
var offer = documentClient.CreateOfferQuery().Where(r => r.ResourceLink == database.SelfLink).ToList().FirstOrDefault();
if(offer != null)
{
offer = new OfferV2(offer, Math.Max(400, (collectionCount * 100)));
await documentClient.ReplaceOfferAsync(offer);
}
@zleao
zleao / PortableMongoDB.CreateShardedCollections.cs
Last active July 14, 2020 12:03
Create sharded collections with MongoDB.Driver
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);
@zleao
zleao / PortableMongoDB.CreateDatabase.azurecosmosdb.cs
Created July 11, 2020 15:29
Database creation with throughput in Azure Cosmos DB v3.2 using DocumentDB nuget package
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);
}
@zleao
zleao / PortableMongoDB.CreateDatabase.onpremise.cs
Created July 11, 2020 15:19
Code necessary to create a databse using the MongoDB.Driver
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);
}