autofac configuration in a json file inside bot framework

441 views Asked by At

I am using a autofac json file which has all my dependencies listed. For reference below is the json file.

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

  ]
}

My RootDialog class is as below:

[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);
    }
}

and inside the MessageController.cs, am resolving the dependency as below:

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;
    }

The problem is, when is register the module through below code in Global.asax:

        var config = new ConfigurationBuilder();

        config.AddJsonFile("autofac.json");

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

        builder.RegisterModule(module);

it gives as error as The type DigitalAssistant.Dialogs.RootDialog is not assignable to service Microsoft.Bot.Builder.Dialogs.IDialog.

What am i missing here, please help.

2

There are 2 answers

0
Harsh Raj On

Try adding the additional handeling while deserializing using the key-FiberModule.Key_DoNotSerialize like this

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

Or maybe you can try this approach.

In GlobalAsax.cs

builder.RegisterModule(new TestModule());

In 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");
        }
    }

In 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);
                }
        }
    }
1
Harsh Raj On

I am not very clear what your problem is because here i cannot see the dialog and classes for reference. But as per my understanding i would suggest you to check whether your dialog is implementing the necessary interface or not. Also your dialog is marked as serializable or not.