private async Task SetEntityStateAsync(EntityActorState state) { await this.StateManager.SetStateAsync <EntityActorState>(ActorStatePropertyName, state); // Just to make sure though it is probably not needed await this.SaveStateAsync(); }
/// <summary> /// This method is called whenever an actor is activated. /// An actor is activated the first time any of its methods are invoked. /// </summary> protected override async Task OnActivateAsync() { this.ActorLocationService = ServiceFactory.GetActorLocationService(); this.SettingService = ServiceFactory.GetSettingService(); this.OltpConnectorService = ServiceFactory.GetOltpConnectorService(this.SettingService); var error = ""; var message = ""; var handler = HandlersFactory.GetProfilerHandler(SettingService); handler.Start(LOG_TAG, "OnActivateAsync", GetActorProperties()); try { EntityActorState state = await GetEntityStateAsync(); if (state == null) { // This is the first time this actor has ever been activated. state = new EntityActorState(); // Set the actor's initial state values. state.Purchases = 0; state.Cancellations = 0; state.SoldItems = 0; state.Revenue = 0; state.Tax = 0; state.Shipping = 0; // Use the actor ID (which packs the enity type and business key) to load the state entity List <string> actorIdParts = this.Id.GetStringId().Split('|').Select(s => s).ToList(); if (actorIdParts == null || actorIdParts.Count != 2) { message = string.Format("Actor ID {0} state could not be determined!!!!", this.Id.GetStringId()); handler.Info(message); state.Entity = new Entity(EntityTypes.SalesOffice, -1); } else { state.Entity = new Entity(); try { EntityTypes type = (EntityTypes)Int32.Parse(actorIdParts.ToArray()[0]); int businessKey = Int32.Parse(actorIdParts.ToArray()[1]); var entity = await OltpConnectorService.GetEntity(type, businessKey); if (entity != null) { state.Entity = await OltpConnectorService.GetEntity(type, businessKey); } } catch (Exception) { message = string.Format("Actor ID {0} state could not be parsed to determine actor entity!!!!", this.Id.GetStringId()); handler.Info(message); } } // Make sure the state is saved await SetEntityStateAsync(state); } state = await this.StateManager.GetStateAsync <EntityActorState>(ActorStatePropertyName); message = string.Format("Actor {0} activated", this.Id.GetStringId()); handler.Info(message); } catch (Exception ex) { error = ex.Message; } finally { handler.Stop(error); } }