Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using RawRabbit.Enrichers.MessageContext;
using RawRabbit.Pipe;
using RawRabbit.WebExample.Shared;
using Serilog;
namespace RawRabbit.WebExample.Controllers
{
public abstract class BusControllerBase : ApiController
{
protected readonly IBusClient BusClient;
private string _sessionCookie = "rawrabbit-cookie";
private readonly ILogger _logger = Log.ForContext<BusControllerBase>();
protected BusControllerBase(IBusClient busClient)
{
BusClient = busClient;
}
protected Task PublishAsync<TMessage>(TMessage message = default(TMessage))
{
return BusClient.PublishAsync(message, CreateCustomContext);
}
private void CreateCustomContext(IPipeContext pipe)
{
var ctx = new CustomContext
{
ExecutionId = Guid.NewGuid(),
Origin = HttpContext.Current.Request.Url.ToString(),
SessionId = GetSessionIdFromCookie(pipe)
};
_logger.Information(
"Creating message context for execution {executionId} on {sessionId} initiated from {origin}",
ctx.ExecutionId, ctx.SessionId, ctx.Origin);
pipe.UseMessageContext(ctx);
}
private Guid GetSessionIdFromCookie(IPipeContext pipe)
{
if (HttpContext.Current.Request.Cookies.AllKeys.Contains(_sessionCookie))
{
var cookieValue = HttpContext.Current.Request.Cookies[_sessionCookie].Value;
return Guid.Parse(cookieValue);
}
var sessionId = Guid.NewGuid();
HttpContext.Current.Response.SetCookie(new HttpCookie(_sessionCookie, sessionId.ToString()));
return sessionId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment