bot框架内的json文件中的autofac配置

时间:2018-06-05 08:14:03

标签: c# botframework autofac autofac-configuration

我正在使用autofac json文件,其中列出了所有依赖项。以下是json文件的参考。

{
  "defaultAssembly": "DigitalAssistant.Wharf",
  "components": [
    {
      "type": "DigitalAssistant.Dialogs.RootDialog, DigitalAssistant.Wharf",
      "services": [
        {
          "type": "Microsoft.Bot.Builder.Dialogs.IDialog, Microsoft.Bot.Builder"
        }
      ],
      "injectProperties": true,
      "instanceScope": "perlifetimescope"
    }

  ]
}

我的RootDialog类如下:

[Serializable]
public class RootDialog : IDialog<object>
{
    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        // Calculate something for us to return
        int length = (activity.Text ?? string.Empty).Length;

        // Return our reply to the user
        await context.PostAsync($"You sent {activity.Text} which was {length} characters");

        context.Wait(MessageReceivedAsync);
    }
}

并在MessageController.cs内部解析依赖关系,如下所示:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.GetActivityType() == ActivityTypes.Message)
        {
            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
            {
                var dialog = scope.Resolve<IDialog<object>>();
                await Conversation.SendAsync(activity, () => dialog);
            }
        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

问题是,何时通过Global.asax中的以下代码注册模块:

        var config = new ConfigurationBuilder();

        config.AddJsonFile("autofac.json");

        var module = new ConfigurationModule(config.Build());

        builder.RegisterModule(module);

它给出了错误,因为DigitalAssistant.Dialogs.RootDialog类型不能分配给服务Microsoft.Bot.Builder.Dialogs.IDialog。

我在这里缺少什么,请帮助。

2 个答案:

答案 0 :(得分:0)

我不太清楚你的问题是什么,因为在这里我看不到对话框和类供参考。 但根据我的理解,我建议你检查你的对话框是否正在实现必要的界面。您的对话框也被标记为可序列化。

答案 1 :(得分:0)

尝试使用key-FiberModule.Key_DoNotSerialize进行反序列化时添加额外的handeling

       builder
            .RegisterType<DialogClass>()
            .Keyed<IDialogInterface>(FiberModule.Key_DoNotSerialize)
            .AsSelf()
            .As<IDialogInterface>()
            .SingleInstance();

或许你可以尝试这种方法。

在GlobalAsax.cs

builder.RegisterModule(new TestModule());

在TestModule.cs

public sealed class TestModule : Module
    {
     protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);
            builder.RegisterType<DialogCreator>().As<IDialogCreator>();
            builder.RegisterType<RootDialog>().Keyed<IDialog<object>>("RootDialog");
        }
    }

在MessagesController.cs中

public class MessagesController : ApiController
    {
        IDialogCreator DialogCreator;
        public MessagesController(IDialogCreator DialogCreator)
        {
            this.DialogCreator = DialogCreator;
        }
        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            try
            {
                HttpResponseMessage response;
                if (activity.Type == ActivityTypes.Message)
                {
                    var RootDialog = DialogCreator.GetDialog("RootDialog"); or // scope.ResolveNamed<IDialog<object>>("RootDialog");
                    await Conversation.SendAsync(activity, () => RootDialog);
                }
                else
                {
                    HandleSystemMessage(activity);
                }
        }
    }