public void Add <T>(string key, T value) where T : class
        {
            MoMoBotAssert.KeyNotNullOrEmpty(key);
            MoMoBotAssert.ValueNotNull(value);

            base.Add(key, value);
        }
Пример #2
0
        public Task <T> ReadAsync <T>(string key, Func <T> def)
        {
            MoMoBotAssert.KeyNotNullOrEmpty(key);

            lock (_synclock)
            {
                if (_memory.TryGetValue(key, out var state))
                {
                    if (state != null)
                    {
                        return(Task.FromResult(state.ToObject <T>(StateJsonSerializer)));
                    }
                }
                if (def != null)
                {
                    var result = def.Invoke();

                    if (result != null)
                    {
                        WriteAsync(new Dictionary <string, object> {
                            { key, result }
                        });
                        return(Task.FromResult(result));
                    }
                }
                return(default);
Пример #3
0
        private async Task <InvokeResponse> ProcessActivityAsync(string authHeader, Activity activity, BotCallbackHandler callback, CancellationToken cancellationToken)
        {
            MoMoBotAssert.NotNull(activity);
            using (var context = new TurnContext(activity))
            {
                //context.TurnState.Add<IIdentity>(BotIdentityKey, identity);
                await callback?.Invoke(context, cancellationToken);

                if (activity.Type == ActivityTypes.Invoke)
                {
                    var activityInvokeResponse = context.TurnState.Get <Activity>(InvokeReponseKey);
                    if (activityInvokeResponse == null)
                    {
                        return(new InvokeResponse {
                            Status = (int)HttpStatusCode.NotImplemented
                        });
                    }
                    else
                    {
                        return((InvokeResponse)activityInvokeResponse.Value);
                    }
                }
                return(null);
            }
        }
Пример #4
0
 public BotState(IStorage storage, string contextServiceKey)
 {
     MoMoBotAssert.NotNull(storage);
     MoMoBotAssert.NotNull(contextServiceKey);
     _storage           = storage;
     _contextServiceKey = contextServiceKey;
 }
Пример #5
0
 public Task DeleteAsync(string[] keys, CancellationToken cancellationToken = default)
 {
     MoMoBotAssert.NotNull(keys);
     lock (_synclock)
     {
         foreach (var key in keys)
         {
             _memory.Remove(key);
         }
     }
     return(Task.CompletedTask);
 }
        public T Get <T>(string key) where T : class
        {
            MoMoBotAssert.KeyNotNullOrEmpty(key);

            if (TryGetValue(key, out var service))
            {
                if (service is T result)
                {
                    return(result);
                }
            }
            return(null);
        }
Пример #7
0
        public async Task <DialogContext> CreateContextAsync(TurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            MoMoBotAssert.NotNull(turnContext);

            // ToDo: Component Dialog doesn't call this code path. This needs to be cleaned up in 4.1.
            if (_dialogState == null)
            {
                // Note: This shouldn't ever trigger, as the _dialogState is set in the constructor and validated there.
                throw new InvalidOperationException($"DialogSet.CreateContextAsync(): DialogSet created with a null IStatePropertyAccessor.");
            }

            // Load/initialize dialog state
            var state = await _dialogState.GetAsync(turnContext, () => { return(new DialogState()); }, cancellationToken).ConfigureAwait(false);

            // Create and return context
            return(new DialogContext(this, turnContext, state));
        }
Пример #8
0
        public Task <IDictionary <string, object> > ReadAsync(string[] keys, CancellationToken cancellationToken = default)
        {
            MoMoBotAssert.NotNull(keys);
            var storeItems = new Dictionary <string, object>(keys.Length);

            lock (_synclock)
            {
                foreach (var key in keys)
                {
                    if (_memory.TryGetValue(key, out var state))
                    {
                        if (state != null)
                        {
                            storeItems.Add(key, state.ToObject <object>(StateJsonSerializer));
                        }
                    }
                }
            }

            return(Task.FromResult <IDictionary <string, object> >(storeItems));
        }
Пример #9
0
 public void Add(BotState botState)
 {
     MoMoBotAssert.NotNull(botState);
     BotStates.Add(botState);
 }
Пример #10
0
 public IStatePropertyAccessor <T> CreateProperty <T>(string name)
 {
     MoMoBotAssert.NameNullOrWhiteSpace(name);
     return(new StatePropertyAccessor <T>(this, name));
 }