public async Task AddUsage(AddUsageMessage message) { var currentStatistics = await this.StateManager.GetStateAsync <BottleFillerStatistics>(BottleFillerStatisticsPropertyName); currentStatistics.TotalGallons += message.NumberOfGallons; currentStatistics.CurrentFilterGallonsUsed += message.NumberOfGallons; await this.StateManager.SetStateAsync(BottleFillerStatisticsPropertyName, currentStatistics); try { string applicationInstance = "ServiceFabricPoc.BottleFiller"; var serviceUri = new Uri("fabric:/" + applicationInstance + "/EventWriterActorService"); IEventWriterActor bottleFiller = ActorProxy.Create <IEventWriterActor>(new ActorId(Guid.NewGuid()), serviceUri); var eventMessage = new UsageAddedEventMessage() { ActorId = this.Id.ToString(), EventTime = DateTimeOffset.UtcNow, NumberOfGallons = message.NumberOfGallons }; await bottleFiller.Write(eventMessage, "usageaddedeventstream"); } catch (Exception ex) { currentStatistics.TotalGallons -= message.NumberOfGallons; currentStatistics.CurrentFilterGallonsUsed -= message.NumberOfGallons; await this.StateManager.SetStateAsync <BottleFillerStatistics>(BottleFillerStatisticsPropertyName, currentStatistics); throw; } }
public async Task Post(AddUsage usage) { var activationContext = FabricRuntime.GetActivationContext(); string applicationInstance = "ServiceFabricPoc.BottleFiller"; var serviceUri = new Uri("fabric:/" + applicationInstance + "/" + BottleFillerServiceName); //We create a unique Guid that is associated with a customer order, as well as with the actor that represents that order's state. IBottleFillerActor bottleFiller = ActorProxy.Create <IBottleFillerActor>(new ActorId(Guid.Parse(usage.Id)), serviceUri); var message = new AddUsageMessage() { NumberOfGallons = usage.NumberOfGallons }; await bottleFiller.AddUsage(message); }