#botframework依赖注入

时间:2016-12-20 16:23:53

标签: c# dependency-injection autofac botframework

我正在尝试使用Autofac通过依赖注入为聊天机器人中的对话框类提供服务。我在Global.asax.cs文件中配置了Autofac容器,如下所示:

cur_best_option <- reactive({
    return(get_default_option())
})

get_options <- reactive({
    option_list <- some_function()
    option_list
})

output$category <- renderUI({
    option_list <- get_options()
    selectInput("selected_category", "Category", 
                choices=c("", option_list), 
                selected=cur_best_option(), multiple=FALSE)
})

# exactly the same content 
output$category2 <- renderUI({
    option_list <- get_options()
    selectInput("selected_category", "Category", 
                choices=c("", option_list), 
                selected=cur_best_option(), multiple=FALSE)
})

# update the table per dropdown list selection
myTabData <- reactive({
    my_option <- cur_best_option()
    .... # do table update accordingly
})

我的MessageController类中的代码成功解析了对话框:

var builder = new ContainerBuilder();
var config = GlobalConfiguration.Configuration;

builder
    .RegisterType<RootDialog>()
    .As<IDialog<object>>()
    .InstancePerDependency();

builder
    .RegisterType<GoogleAnalyticsService>()
    .Keyed<IAnalyticsService>(FiberModule.Key_DoNotSerialize)
    .AsImplementedInterfaces()
    .SingleInstance();

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

builder.RegisterWebApiFilterProvider(config);

builder.Update(Conversation.Container);

首次解析对话框并运行构造函数时,在Visual Studio中进行调试时,会成功注入IAnalyticsService的实现。

using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
    await Conversation.SendAsync(activity, () => scope.Resolve<IDialog<object>>());
}

但是,在此阶段之后尝试访问_analyticsService变量时(即处理用户发送的消息时),它始终为null。我已经尝试在其上设置NonSerializable属性,但这没有任何区别。进一步的调试也表明构造函数没有再被击中。

有人可以建议为什么这个变量为null,以及该问题的任何潜在解决方案?

1 个答案:

答案 0 :(得分:0)

再次清洁和重建后,我的解决方案奏效了。令人沮丧。