Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
ankitvijay / AddCommentHandler.cs
Created Oct 9, 2021
NServiceBus Cosmos Sample - AddCommentHandler
View AddCommentHandler.cs
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 Oct 9, 2021
NServiceBus Cosmos Sample - AddPostHandler
View AddPostHandler.cs
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 Oct 9, 2021
NServiceBus Cosmos Sample - Domain
View 1_Post.cs
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 Oct 9, 2021
NServiceBus Cosmos Sample - Worker Host
View Program.cs
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 Oct 9, 2021
NServiceBus Cosmos Sample - PostIdAsPartitionKeyBehaviour
View PostIdAsPartitionKeyBehaviour.cs
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 Oct 8, 2021
NServiceBus Cosmos Sample - Messages
View 1_IPostIdPartitionKey.cs
public interface IPostIdPartitionKey
{
string PostId { get; set; }
}
@ankitvijay
ankitvijay / Program.cs
Created Oct 8, 2021
NServiceBus Cosmos Sample - Add API
View Program.cs
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 Oct 8, 2021
NServiceBus Cosmos Sample - Use Swagger
View Program.cs
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 Oct 8, 2021
NServiceBus Cosmos Sample - Use NServiceBus
View Program.cs
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 Aug 26, 2021
Switch Expression Test
View SwitchExpressionTest.cs
using System;
Console.WriteLine("GetBoolean 1:" + (GetBoolean1("Nah") == null));
Console.WriteLine("GetBoolean 2:" + (GetBoolean2("Nah") == null));
static Boolean? GetBoolean2(string boolString)
{
return boolString switch
{