Пример #1
0
        public static void Register(HttpConfiguration config)
        {
            var builder = new ContainerBuilder();

            builder
                .Register(c => new BingTranslatorClient("Test187871", "dAnT3r/eIc8KedBRUgRCV+juxpf4Wl312jn1Bd2SXzk="))
                .As<ITranslator>()
                .InstancePerDependency();

            builder
                .Register(c => new LogBotToUser(new MapToChannelData_BotToUser(
                        c.Resolve<AlwaysSendDirect_BotToUser>(),
                        new List<IMessageActivityMapper> { new KeyboardCardMapper() }), c.Resolve<IActivityLogger>()))
                    .AsSelf()
                    .InstancePerLifetimeScope();

            builder
                .Register(c => new TranslatedBotToUser(c.Resolve<LogBotToUser>(),
                        c.Resolve<ITranslator>()))
                    .As<IBotToUser>()
                    .InstancePerLifetimeScope();

            builder
                .Register(c =>
                {
                    var cc = c.Resolve<IComponentContext>();

                    Func<IPostToBot> makeInner = () =>
                    {
                        var task = cc.Resolve<DialogTask>();
                        IDialogStack stack = task;
                        IPostToBot post = task;
                        post = new ReactiveDialogTask(post, stack, cc.Resolve<IStore<IFiberLoop<DialogTask>>>(), cc.Resolve<Func<IDialog<object>>>());
                        post = new ExceptionTranslationDialogTask(post);
                        post = new LocalizedDialogTask(post);
                        post = new ScoringDialogTask<double>(post, stack, cc.Resolve<TraitsScorable<IActivity, double>>());
                        return post;
                    };

                    IPostToBot outer = new PersistentDialogTask(makeInner, cc.Resolve<IBotData>());
                    outer = new SerializingDialogTask(outer, cc.Resolve<IAddress>(), c.Resolve<IScope<IAddress>>());
                    outer = new PostUnhandledExceptionToUserTask(outer, cc.Resolve<IBotToUser>(), cc.Resolve<ResourceManager>(), cc.Resolve<TraceListener>());
                    outer = new LogPostToBot(outer, cc.Resolve<IActivityLogger>());
                    outer = new TranslatedPostToBot(outer, cc.Resolve<ITranslator>());
                    return outer;
                })
                .As<IPostToBot>()
                .InstancePerLifetimeScope();

            builder.Update(Conversation.Container);
        }
Пример #2
0
        public async Task DialogTaskManager_TwoParallelDialogTasks()
        {
            var dialog            = new Mock <DialogTaskTests.IDialogFrames <string> >(MockBehavior.Strict);
            var secondStackDialog = new Mock <DialogTaskTests.IDialogFrames <object> >(MockBehavior.Strict);

            dialog
            .Setup(d => d.StartAsync(It.IsAny <IDialogContext>()))
            .Returns <IDialogContext>(async context => { context.Wait(dialog.Object.ItemReceived); });

            dialog
            .Setup(d => d.ItemReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()))
            .Returns <IDialogContext, IAwaitable <IMessageActivity> >(async(context, message) =>
            {
                var msg = await message;
                await context.PostAsync(msg.Text);
                context.Wait(dialog.Object.ItemReceived);
            });


            var promptMessage = "Say something!";

            secondStackDialog
            .Setup(d => d.StartAsync(It.IsAny <IDialogContext>()))
            .Returns <IDialogContext>(
                async context =>
                { context.Wait(secondStackDialog.Object.ItemReceived <IMessageActivity>); });

            secondStackDialog
            .Setup(d => d.ItemReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()))
            .Returns <IDialogContext, IAwaitable <IMessageActivity> >(async(context, message) =>
            {
                PromptDialog.Text(context, secondStackDialog.Object.ItemReceived, promptMessage);
            });

            secondStackDialog
            .Setup(d => d.ItemReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <string> >()))
            .Returns <IDialogContext, IAwaitable <string> >(async(context, message) =>
            {
                await context.PostAsync($"from prompt {await message}");
                context.Wait(secondStackDialog.Object.ItemReceived <IMessageActivity>);
            });

            Func <IDialog <object> > makeRoot            = () => dialog.Object;
            Func <IDialog <object> > secondStackMakeRoot = () => secondStackDialog.Object;
            var toBot = MakeTestMessage();

            using (new FiberTestBase.ResolveMoqAssembly(dialog.Object, secondStackDialog.Object))
                using (var container = Build(Options.MockConnectorFactory, dialog.Object, secondStackDialog.Object))
                {
                    string foo = "foo";
                    string bar = "bar";
                    Queue <IMessageActivity> queue;
                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        toBot.Text = foo;
                        DialogModule_MakeRoot.Register(scope, makeRoot);
                        await scope.Resolve <IPostToBot>().PostAsync(toBot, CancellationToken.None);

                        var botData = scope.Resolve <IBotData>();
                        await botData.LoadAsync(CancellationToken.None);

                        var dialogTaskManager = scope.Resolve <IDialogTaskManager>();

                        //create second dialog task
                        var        secondDialogTask   = dialogTaskManager.CreateDialogTask();
                        IPostToBot reactiveDialogTask = new ReactiveDialogTask(secondDialogTask, secondStackMakeRoot);
                        await reactiveDialogTask.PostAsync(toBot, CancellationToken.None);

                        await botData.FlushAsync(CancellationToken.None);

                        queue = scope.Resolve <Queue <IMessageActivity> >();
                        Assert.AreEqual(foo, queue.Dequeue().Text);
                        Assert.AreEqual(promptMessage, queue.Dequeue().Text);
                    }

                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        toBot.Text = bar;
                        DialogModule_MakeRoot.Register(scope, makeRoot);
                        await scope.Resolve <IPostToBot>().PostAsync(toBot, CancellationToken.None);

                        var dialogTaskManager = scope.Resolve <IDialogTaskManager>();
                        Assert.AreEqual(2, dialogTaskManager.DialogTasks.Count);
                        var secondDialogTask = dialogTaskManager.DialogTasks[1];
                        await secondDialogTask.PostAsync(toBot, CancellationToken.None);

                        queue = scope.Resolve <Queue <IMessageActivity> >();
                        Assert.AreEqual(bar, queue.Dequeue().Text);
                        Assert.AreEqual($"from prompt {bar}", queue.Dequeue().Text);
                    }
                }
        }
Пример #3
0
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            builder.RegisterModule(new FiberModule<DialogTask>());

            // every lifetime scope is driven by a message

            builder
                .Register((c, p) => p.TypedAs<IMessageActivity>())
                .AsSelf()
                .InstancePerMatchingLifetimeScope(LifetimeScopeTag);

            // components not marked as [Serializable]
            builder
                .RegisterType<MicrosoftAppCredentials>()
                .AsSelf()
                .SingleInstance();

            builder
                .RegisterType<BotIdResolver>()
                .As<IBotIdResolver>()
                .SingleInstance();

            builder
                .Register(c => new ConnectorClientFactory(c.Resolve<IMessageActivity>(), c.Resolve<MicrosoftAppCredentials>()))
                .As<IConnectorClientFactory>()
                .InstancePerLifetimeScope();

            builder
                .Register(c => c.Resolve<IConnectorClientFactory>().MakeConnectorClient())
                .As<IConnectorClient>()
                .InstancePerLifetimeScope();

            builder
                .Register(c => c.Resolve<IConnectorClientFactory>().MakeStateClient())
                .As<IStateClient>()
                .InstancePerLifetimeScope();

            builder
               .Register(c => new DetectChannelCapability(c.Resolve<IMessageActivity>()))
               .As<IDetectChannelCapability>()
               .InstancePerLifetimeScope();

            builder
                .Register(c => c.Resolve<IDetectChannelCapability>().Detect())
                .As<IChannelCapability>()
                .InstancePerLifetimeScope();

            builder.RegisterType<ConnectorStore>()
                .As<IBotDataStore<BotData>>()
                .AsSelf()
                .InstancePerLifetimeScope();

            // If bot wants to use InMemoryDataStore instead of
            // ConnectorStore, the below registration should be used
            /*builder.RegisterType<InMemoryDataStore>()
                .As<IBotDataStore<BotData>>()
                .AsSelf()
                .SingleInstance(); */

            builder.RegisterType<CachingBotDataStore_LastWriteWins>()
                .As<IBotDataStore>()
                .AsSelf()
                .InstancePerLifetimeScope();

            builder
                .RegisterType<JObjectBotData>()
                .As<IBotData>()
                .InstancePerLifetimeScope();

            builder
                .Register(c => new BotDataBagStream(c.Resolve<IBotData>().PrivateConversationData, BlobKey))
                .As<Stream>()
                .InstancePerLifetimeScope();

            builder
                .RegisterType<DialogTask>()
                .AsSelf()
                .As<IDialogStack>()
                .InstancePerLifetimeScope();

            // Scorable implementing "/deleteprofile"
            builder
                .RegisterType<DeleteProfileScorable>()
                .As<IScorable<double>>()
                .InstancePerLifetimeScope();

            builder
                .Register(c =>
                {
                    var cc = c.Resolve<IComponentContext>();

                    Func<IPostToBot> makeInner = () =>
                    {
                        var task = cc.Resolve<DialogTask>();
                        IDialogStack stack = task;
                        IPostToBot post = task;
                        post = new ReactiveDialogTask(post, stack, cc.Resolve<IStore<IFiberLoop<DialogTask>>>(), cc.Resolve<Func<IDialog<object>>>());
                        post = new LocalizedDialogTask(post);
                        post = new ScoringDialogTask<double>(post, stack, cc.Resolve<IComparer<double>>(), cc.Resolve<ITraits<double>>(), cc.Resolve<IScorable<double>[]>());
                        return post;
                    };

                    var outer = new PersistentDialogTask(makeInner, cc.Resolve<IMessageActivity>(), cc.Resolve<IConnectorClient>(), cc.Resolve<IBotToUser>(), cc.Resolve<IBotData>());
                    return outer;
                })
                .As<IPostToBot>()
                .InstancePerLifetimeScope();

            builder
                .RegisterType<AlwaysSendDirect_BotToUser>()
                .AsSelf()
                .As<IBotToUser>()
                .InstancePerLifetimeScope();

            builder
                .RegisterType<DialogContext>()
                .As<IDialogContext>()
                .InstancePerLifetimeScope();
        }
Пример #4
0
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            builder.RegisterModule(new FiberModule<DialogTask>());

            // every lifetime scope is driven by a message

            builder
                .Register((c, p) => p.TypedAs<Message>())
                .AsSelf()
                .InstancePerMatchingLifetimeScope(LifetimeScopeTag);

            // components not marked as [Serializable]

            builder
                .RegisterType<ConnectorClientCredentials>()
                .AsSelf()
                .SingleInstance();

            builder
                .Register(c => new DetectEmulatorFactory(c.Resolve<Message>(), new Uri("http://localhost:9000"), c.Resolve<ConnectorClientCredentials>()))
                .As<IConnectorClientFactory>()
                .InstancePerLifetimeScope();

            builder
                .Register(c => c.Resolve<IConnectorClientFactory>().Make())
                .As<IConnectorClient>()
                .InstancePerLifetimeScope();

            builder
               .Register(c => new DetectChannelCapability(c.Resolve<Message>()))
               .As<IDetectChannelCapability>()
               .InstancePerLifetimeScope();

            builder
                .Register(c => c.Resolve<IDetectChannelCapability>().Detect())
                .As<IChannelCapability>()
                .InstancePerLifetimeScope();

            builder.RegisterType<MessageBackedStore>()
                .As<IBotDataStore>()
                .InstancePerLifetimeScope();

            builder
                .RegisterType<JObjectBotData>()
                .As<IBotData>()
                .InstancePerLifetimeScope();

            builder
                .Register(c => new BotDataBagStream(c.Resolve<IBotData>().PerUserInConversationData, BlobKey))
                .As<Stream>()
                .InstancePerLifetimeScope();

            builder
                .RegisterType<DialogTask>()
                .AsSelf()
                .As<IDialogStack>()
                .InstancePerLifetimeScope();

            builder
                .Register(c =>
                {
                    var cc = c.Resolve<IComponentContext>();

                    Func<IPostToBot> makeInner = () =>
                    {
                        var task = cc.Resolve<DialogTask>();
                        IDialogStack stack = task;
                        IPostToBot post = task;
                        post = new ReactiveDialogTask(post, stack, cc.Resolve<IStore<IFiberLoop<DialogTask>>>(), cc.Resolve<Func<IDialog<object>>>());
                        post = new LocalizedDialogTask(post);
                        post = new ScoringDialogTask<double>(post, stack, cc.Resolve<IComparer<double>>(), cc.Resolve<ITraits<double>>(), cc.Resolve<IScorable<double>[]>());
                        return post;
                    };

                    var outer = new PersistentDialogTask(makeInner, cc.Resolve<Message>(), cc.Resolve<IConnectorClient>(), cc.Resolve<IBotToUser>(), cc.Resolve<IBotData>());
                    return outer;
                })
                .As<IPostToBot>()
                .InstancePerLifetimeScope();

            builder
                .RegisterType<SendLastInline_BotToUser>()
                .AsSelf()
                .As<IBotToUser>()
                .InstancePerLifetimeScope();

            builder
                .RegisterType<DialogContext>()
                .As<IDialogContext>()
                .InstancePerLifetimeScope();
        }