//-------------- An entity representing a counter object -----------------

        public static void CounterEntity([EntityTrigger(EntityName = "Counter")] IDurableEntityContext context)
        {
            switch (context.OperationName)
            {
            case "increment":
                context.SetState(context.GetState <int>() + 1);
                break;

            case "add":
                context.SetState(context.GetState <int>() + context.GetInput <int>());
                break;

            case "get":
                context.Return(context.GetState <int>());
                break;

            case "set":
                context.SetState(context.GetInput <int>());
                break;

            case "delete":
                context.DeleteState();
                break;

            default:
                throw new NotImplementedException("no such entity operation");
            }
        }
Пример #2
0
            public static void Counter([EntityTrigger] IDurableEntityContext ctx)
            {
                int current = ctx.GetState <int>();

                switch (ctx.OperationName)
                {
                case "incr":
                    ctx.SetState(current + 1);
                    break;

                case "add":
                    int amount = ctx.GetInput <int>();
                    ctx.SetState(current + amount);
                    break;

                case "get":
                    ctx.Return(current);
                    break;

                case "set":
                    amount = ctx.GetInput <int>();
                    ctx.SetState(amount);
                    break;

                case "delete":
                    ctx.DeleteState();
                    break;

                default:
                    throw new NotImplementedException("No such entity operation");
                }
            }
        //-------------- a slightly less trivial version of the same -----------------
        // as before with two differences:
        // - "get" throws an exception if the entity does not already exist, i.e. state was not set to anything
        // - a new operation "delete" deletes the entity, i.e. clears all state

        public static void StringStoreEntity2([EntityTrigger(EntityName = "StringStore2")] IDurableEntityContext context)
        {
            switch (context.OperationName)
            {
            case "delete":
                context.DeleteState();
                break;

            case "set":
                context.SetState(context.GetInput <string>());
                break;

            case "get":
                if (!context.HasState)
                {
                    throw new InvalidOperationException("must not call get on a non-existing entity");
                }

                context.Return(context.GetState <string>());
                break;

            default:
                throw new NotImplementedException("no such operation");
            }
        }
Пример #4
0
        public async Task EntityAsync([EntityTrigger] IDurableEntityContext durableEntityContext)
        {
            DurableInterceptor.Context.Value = default;
            var functionContext = durableEntityContext.GetInput <FunctionContext>();
            var result          = await functionContext.InvokeAsync(this.componentContext);

            durableEntityContext.DeleteState();
            durableEntityContext.Return(result);
        }
 public Task Add(AddLocationCommand command)
 {
     if (command is null)
     {
         _context.DeleteState();
     }
     Name      = command.Name;
     Longitude = command.Longitude;
     Latitude  = command.Latitude;
     return(Task.CompletedTask);
 }
Пример #6
0
        public static Task LightUntypedRun(
            [EntityTrigger] IDurableEntityContext ctx,
            ILogger log)
        {
            try
            {
                if (!ctx.HasState)
                {
                    //ctx.SetState(new { State = LightState.Off, HexColor = "#eeeeee"});
                    ctx.SetState(new Props {
                        State = LightState.Off, HexColor = "#eeeeee"
                    });
                }

                //var currentState = ctx.GetState<dynamic>();
                var currentState = ctx.GetState <Props>();

                switch (ctx.OperationName)
                {
                case "On":
                    currentState.State = LightState.On;
                    break;

                case "Off":
                    currentState.State = LightState.Off;
                    break;

                case "Color":
                    var hexColor = ctx.GetInput <string>();
                    currentState.HexColor = hexColor;
                    break;

                case "Get":
                    ctx.Return(currentState);
                    break;

                case "End":
                    ctx.DeleteState();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(Task.CompletedTask);
        }
        //-------------- An entity representing a phone book, using a typed C# dictionary -----------------

        public static void PhoneBookEntity2([EntityTrigger(EntityName = "PhoneBook2")] IDurableEntityContext context)
        {
            if (!context.HasState)
            {
                context.SetState(new Dictionary <string, decimal>());
            }

            var state = context.GetState <Dictionary <string, decimal> >();

            switch (context.OperationName)
            {
            case "set":
            {
                var(name, number) = context.GetInput <(string, decimal)>();
                state[name]       = number;
                break;
            }

            case "remove":
            {
                var name = context.GetInput <string>();
                state.Remove(name);
                break;
            }

            case "lookup":
            {
                var name = context.GetInput <string>();
                context.Return(state[name]);
                break;
            }

            case "dump":
            {
                context.Return(state);
                break;
            }

            case "clear":
            {
                context.DeleteState();
                break;
            }

            default:
                throw new NotImplementedException("no such entity operation");
            }
        }
Пример #8
0
        public static Task FaultyEntityFunction([EntityTrigger] IDurableEntityContext context)
        {
            // we use an untyped call to test existence without creating the entity
            if (context.OperationName == "exists")
            {
                context.Return(context.HasState);
                return(Task.CompletedTask);
            }
            else if (context.OperationName == "deletewithoutreading")
            {
                context.DeleteState();
                return(Task.CompletedTask);
            }

            return(context.DispatchAsync <FaultyEntity>());
        }
        public Task Create(CreatePasadasCommand command)
        {
            if (string.IsNullOrWhiteSpace(command.HostId))
            {
                _entityContext.DeleteState();
                return(Task.CompletedTask);
            }
            HostId     = command.HostId;
            LocationId = command.LocationId;

            var entityId = new EntityId(nameof(PosadasAllAggregatorAggregator),
                                        PosadasAllAggregatorAggregator.EntityId);

            _entityContext.SignalEntity <IPasadasAllAggregator>(entityId, proxy =>
                                                                proxy.Add(_entityContext.EntityKey));

            return(Task.CompletedTask);
        }
        //-------------- an entity that stores text, and whose state is
        //                  saved/restored to/from storage when the entity is deactivated/activated -----------------
        //
        // it offers three operations:
        // "clear" sets the current value to empty
        // "append" appends the string provided in the content to the current value
        // "get" returns the current value
        // "deactivate" destructs the entity (after saving its current state in the backing storage)

        public static async Task BlobBackedTextStoreEntity(
            [EntityTrigger(EntityName = "BlobBackedTextStore")] IDurableEntityContext context)
        {
            if (!context.HasState)
            {
                // try to load state from existing blob
                var currentFileContent = await TestHelpers.LoadStringFromTextBlobAsync(
                    context.EntityKey);

                context.SetState(new StringBuilder(currentFileContent ?? ""));
            }

            var state = context.GetState <StringBuilder>();

            switch (context.OperationName)
            {
            case "clear":
                state.Clear();
                break;

            case "append":
                state.Append(context.GetInput <string>());
                break;

            case "get":
                context.Return(state.ToString());
                break;

            case "deactivate":
                // first, store the current value in a blob
                await TestHelpers.WriteStringToTextBlob(
                    context.EntityKey, state.ToString());

                // then, destruct this entity (and all of its state)
                context.DeleteState();
                break;

            default:
                throw new NotImplementedException("no such operation");
            }
        }
        public static void Cart(
            [EntityTrigger(EntityName = "Cart")] IDurableEntityContext ctx,
            ILogger log)
        {
            var userAction = ctx.OperationName;

            //If Cart doesn't exist it will initialize with an empty item list
            var cart = ctx.GetState(() => new Cart(ctx.EntityKey));

            switch (userAction)
            {
            case "add":
                var item = ctx.GetInput <Item>();
                //Duplicate detection. Don't add duplicates.
                if (CheckForDuplicate(cart, item.ETag))
                {
                    return;
                }
                cart.Items.Add(ctx.GetInput <Item>());
                cart.TotalAmount += item.Price;
                log.LogInformation($"Added item in cart: {item.Id} in Cart: {ctx.ToString()}");
                break;

            case "remove":
                item = ctx.GetInput <Item>();
                cart.Items.Remove(item);
                cart.TotalAmount -= item.Price;
                log.LogInformation($"Removed item in cart: {item.Id} in Cart: {ctx.ToString()}");
                break;

            case "purchased":
                //Checked out - Cart is no longer needed
                log.LogInformation($"Cart is destructed: {ctx.ToString()}");
                ctx.DeleteState();
                return;

            default:
                return;
            }
            ctx.SetState(cart);
        }
Пример #12
0
        public static void Run([EntityTrigger] IDurableEntityContext context)
        {
            // Ensure that a state exists.
            if (!context.HasState)
            {
                context.SetState(0L);
            }

            // Handle operation.
            switch (context.OperationName.ToLowerInvariant())
            {
            case "set":
                context.SetState(context.GetInput <long>());
                break;

            case "get":
                context.Return(context.GetState <long>());
                break;

            case "delete":
                context.DeleteState();
                break;
            }
        }
Пример #13
0
        public static Task FaultyEntityFunctionWithoutDispatch([EntityTrigger] IDurableEntityContext context)
        {
            switch (context.OperationName)
            {
            case "exists":
                context.Return(context.HasState);
                break;

            case "deletewithoutreading":
                context.DeleteState();
                break;

            case "Get":
                if (!context.HasState)
                {
                    context.Return(0);
                }
                else
                {
                    context.Return(context.GetState <FaultyEntity>().Value);
                }

                break;

            case "GetNumberIncrementsSent":
                context.Return(context.GetState <FaultyEntity>().NumberIncrementsSent);
                break;

            case "Set":
                var state = context.GetState <FaultyEntity>() ?? new FaultyEntity();
                state.Value = context.GetInput <int>();
                context.SetState(state);
                break;

            case "SetToUnserializable":
                var state1 = context.GetState <FaultyEntity>() ?? new FaultyEntity();
                state1.SetToUnserializable();
                context.SetState(state1);
                break;

            case "SetToUnDeserializable":
                var state2 = context.GetState <FaultyEntity>() ?? new FaultyEntity();
                state2.SetToUnDeserializable();
                context.SetState(state2);
                break;

            case "SetThenThrow":
                var state3 = context.GetState <FaultyEntity>() ?? new FaultyEntity();
                state3.Value = context.GetInput <int>();
                context.SetState(state3);
                throw new FaultyEntity.SerializableKaboom();

            case "Send":
                var state4 = context.GetState <FaultyEntity>() ?? new FaultyEntity();
                state4.Send(context.GetInput <EntityId>());
                context.SetState(state4);
                return(Task.CompletedTask);

            case "SendThenThrow":
                var state5 = context.GetState <FaultyEntity>() ?? new FaultyEntity();
                state5.Send(context.GetInput <EntityId>());
                context.SetState(state5);
                throw new FaultyEntity.SerializableKaboom();

            case "SendThenMakeUnserializable":
                var state6 = context.GetState <FaultyEntity>() ?? new FaultyEntity();
                state6.Send(context.GetInput <EntityId>());
                context.SetState(state6);
                state6.SetToUnserializable();
                return(Task.CompletedTask);

            case "Delete":
                context.DeleteState();
                break;

            case "DeleteThenThrow":
                context.DeleteState();
                throw new FaultyEntity.SerializableKaboom();

            case "Throw":
                throw new FaultyEntity.SerializableKaboom();

            case "ThrowUnserializable":
                throw new FaultyEntity.UnserializableKaboom();

            case "ThrowUnDeserializable":
                throw new FaultyEntity.UnDeserializableKaboom();
            }

            return(Task.CompletedTask);
        }