Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
ankitvijay / AddCommentHandler.cs
Created October 9, 2021 22:22
NServiceBus Cosmos Sample - AddCommentHandler
public class AddCommentHandler : IHandleMessages<AddComment>
{
public async Task Handle(AddComment message, IMessageHandlerContext context)
{
var cosmosSession = context.SynchronizedStorageSession.CosmosPersistenceSession();
var postResource = await cosmosSession.Container.ReadItemAsync<Post>(message.PostId,
new PartitionKey(message.PostId));
if (postResource == null)
{
@ankitvijay
ankitvijay / AddPostHandler.cs
Last active October 9, 2021 22:15
NServiceBus Cosmos Sample - AddPostHandler
public class AddPostHandler : IHandleMessages<AddPost>
{
public async Task Handle(AddPost message, IMessageHandlerContext context)
{
var cosmosSession = context.SynchronizedStorageSession.CosmosPersistenceSession();
var post = new Post(message.PostId, message.Title, message.Description, message.Author);
cosmosSession.Batch.CreateItem(post);
await context.Publish(new PostCreated
{
PostId = post.PostId,
@ankitvijay
ankitvijay / 1_Post.cs
Last active October 9, 2021 22:03
NServiceBus Cosmos Sample - Domain
public class Post
{
public Post(string postId,
string title,
string description,
string author)
{
Id = postId;
PostId = postId;
Title = title;
@ankitvijay
ankitvijay / Program.cs
Last active October 9, 2021 21:31
NServiceBus Cosmos Sample - Worker Host
await Host.CreateDefaultBuilder(args)
.UseNServiceBus((_ =>
{
var endpointConfiguration = new EndpointConfiguration("Samples.Worker");
var transport = endpointConfiguration.UseTransport<LearningTransport>();
transport.Transactions(TransportTransactionMode.SendsAtomicWithReceive);
endpointConfiguration.UsePersistence<CosmosPersistence>()
// Using Cosmos emulator
.CosmosClient(new CosmosClient(
@ankitvijay
ankitvijay / PostIdAsPartitionKeyBehaviour.cs
Last active October 9, 2021 21:31
NServiceBus Cosmos Sample - PostIdAsPartitionKeyBehaviour
public class PostIdAsPartitionKeyBehavior: Behavior<IIncomingLogicalMessageContext>
{
public override Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
{
if (context.Message.Instance is IPostIdPartitionKey partitionKey)
{
var partitionKeyValue = partitionKey.PostId;
context.Extensions.Set(new PartitionKey(partitionKeyValue));
return next();
@ankitvijay
ankitvijay / 1_IPostIdPartitionKey.cs
Created October 8, 2021 20:55
NServiceBus Cosmos Sample - Messages
public interface IPostIdPartitionKey
{
string PostId { get; set; }
}
@ankitvijay
ankitvijay / Program.cs
Created October 8, 2021 20:49
NServiceBus Cosmos Sample - Add API
app.MapPost("/create", async (IMessageSession messageSession, [FromBody] Post post) =>
{
var addPostCommand = new Messages.AddPost(post.Title, post.Description, post.Author);
await messageSession.Send(addPostCommand);
return Results.Accepted(null, new { addPostCommand.PostId });
});
app.MapPost("/add-comment", async (IMessageSession messageSession, [FromBody] Comment comment) =>
{
var addCommentCommand = new Messages.AddComment(comment.PostId, comment.Content, comment.CommentBy);
@ankitvijay
ankitvijay / Program.cs
Created October 8, 2021 20:49
NServiceBus Cosmos Sample - Use Swagger
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Posts Api", Version = "v1" });
});
var app = builder.Build();
.
.
app.UseHttpsRedirection()
@ankitvijay
ankitvijay / Program.cs
Created October 8, 2021 20:47
NServiceBus Cosmos Sample - Use NServiceBus
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseNServiceBus(_ =>
{
var endpointConfiguration = new EndpointConfiguration("Samples.Api");
var transport = endpointConfiguration.UseTransport<LearningTransport>();
var routing = transport.Routing();
routing.RouteToEndpoint(Assembly.Load("Messages"), "Samples.Worker");
endpointConfiguration.SendOnly();
return endpointConfiguration;
});
@ankitvijay
ankitvijay / SwitchExpressionTest.cs
Last active August 26, 2021 07:11
Switch Expression Test
using System;
Console.WriteLine("GetBoolean 1:" + (GetBoolean1("Nah") == null));
Console.WriteLine("GetBoolean 2:" + (GetBoolean2("Nah") == null));
static Boolean? GetBoolean2(string boolString)
{
return boolString switch
{