0

我正在使用一个 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 一样。

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

4

2 回答 2

0

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

于 2018-06-05T08:34:10.630 回答
0

尝试在使用 key-FiberModule.Key_DoNotSerialize 反序列化时添加额外的处理,如下所示

       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);
                }
        }
    }
于 2018-06-05T09:54:55.223 回答