/// <summary> /// This is the main entry point for your service's partition replica. /// RunAsync executes when the primary replica for this partition has write status. /// </summary> /// <param name="cancellationToken">Canceled when Service Fabric terminates this partition's replica.</param> protected override async Task RunAsync(CancellationToken cancellationToken) { var queryModelBuilder = GetQueryModelBuilder(); var queue = await StateManager.GetOrAddAsync <IReliableQueue <string> >("queryModelBuilderQueue"); var count = (int)await queue.GetCountAsync(); if (count > 0) { _semaphore.Release(count); } while (true) { cancellationToken.ThrowIfCancellationRequested(); using (ITransaction tx = StateManager.CreateTransaction()) { ConditionalResult <string> dequeueReply = await queue.TryDequeueAsync(tx); if (dequeueReply.HasValue) { string message = dequeueReply.Value; await queryModelBuilder.Handle(CqrsApplication.GetService <IDeserializer>().CreateEvent(JObject.Parse(message))); await tx.CommitAsync(); } } await _semaphore.WaitAsync(cancellationToken); } }
public override async Task Invoke(IOwinContext context) { if (context.Request.Method.Equals("post", StringComparison.OrdinalIgnoreCase) && context.Request.Path.GetExtension().Equals("command", StringComparison.OrdinalIgnoreCase)) { try { // Deserialize the command and dispatch it var command = DeserializeFromBody(context.Request.Body); await CqrsApplication.GetService <CommandBus>().Dispatch(command); context.Response.StatusCode = 200; await context.Response.WriteAsync("{ \"commandAccepted\": true }"); } catch (AggregateException aEx) { var ex = aEx.InnerExceptions.FirstOrDefault(); ExceptionHandler(ex ?? aEx); context.Response.StatusCode = 500; await context.Response.WriteAsync($"{{ \"commandAccepted\": false, \"error\": \"{ex?.Message}\" }}"); } catch (Exception ex) { ExceptionHandler(ex); context.Response.StatusCode = 500; await context.Response.WriteAsync($"{{ \"commandAccepted\": false, \"error\": \"{ex.Message}\" }}"); } } else { await Next.Invoke(context); } }
public override async Task Invoke(IOwinContext context) { if (context.Request.Method.Equals("get", StringComparison.OrdinalIgnoreCase) && context.Request.Path.GetExtension().Equals("query", StringComparison.OrdinalIgnoreCase)) { try { // Deserialize the querystring and dispatch it var query = DeserializeFromQueryString(context.Request.Path.ToString(), context.Request.Query); var result = await CqrsApplication.GetService <QueryBus>().Dispatch(query); context.Response.StatusCode = 200; await context.Response.WriteAsync(result?.ToString() ?? "null"); } catch (AggregateException aEx) { var ex = aEx.InnerExceptions.FirstOrDefault(); ExceptionHandler(ex ?? aEx); context.Response.StatusCode = 500; await context.Response.WriteAsync($"{{ \"queryAccepted\": false, \"error\": \"{ex?.Message}\" }}"); } catch (Exception ex) { ExceptionHandler(ex); context.Response.StatusCode = 500; await context.Response.WriteAsync($"{{ \"queryAccepted\": false, \"error\": \"{ex.Message}\" }}"); } } else { await Next.Invoke(context); } }
private Command DeserializeFromBody(Stream body) { using (var sr = new StreamReader(body)) { using (JsonReader jsonReader = new JsonTextReader(sr)) { return(CqrsApplication.GetService <IDeserializer>().CreateCommand(JObject.Load(jsonReader))); } } }
private static async Task RunInProcessSample() { CqrsApplication.Bootstrap( Handlers.CommandHandlers, Handlers.QueryHandlers, Handlers.QueryModelBuilders); var iphoneId = Guid.Parse("d0174342-71b0-4deb-b5b8-d1064d07ec3c"); await CqrsApplication.GetService <CommandBus>().Dispatch(new CreateArticleCommand { ArticleId = iphoneId, Description = "iPhone 6S 64GB Space Gray", Price = 850.99m }); var document = await CqrsApplication.GetService <QueryBus>().Dispatch(new GetArticleQuery { ArticleId = iphoneId }); System.Console.WriteLine(document ?? "null"); System.Console.ReadKey(); }
public async Task <string> Get(Guid id) { var result = await CqrsApplication.GetService <QueryRepository>().Get(id); return(result?.ToString()); }
private static Command Deserialize(string command) { return(CqrsApplication.GetService <IDeserializer>().CreateCommand(JObject.Parse(command))); }
private Query DeserializeFromQueryString(string name, IReadableStringCollection query) { return(CqrsApplication.GetService <IDeserializer>().CreateQuery(name, query.Select(kv => new KeyValuePair <string, IEnumerable <string> >(kv.Key, kv.Value)))); }