Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
ankitvijay / AlwaysOn.ps
Last active June 15, 2023 10:33
IIS Background Worker Setting
## IIS WebAdmin Module
Import-Module WebAdministration
$AppPoolInstance = Get-Item IIS:\AppPools\$AppPool
Write-Output "Set Site PreLoadEnabled to true"
Set-ItemProperty IIS:\Sites\$Site -name applicationDefaults.preloadEnabled -value True
Write-Output "Set Recycling.periodicRestart.time = 0"
$AppPoolInstance.Recycling.periodicRestart.time = [TimeSpan]::Parse("0");
@ankitvijay
ankitvijay / .gitignore
Created December 27, 2018 20:29
Gitignore file to ignore the local appSettings
# Ignore local appSettings
appSettings.Local.json
@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 / 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 / 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 / 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()