Injecting external dependencies in Microsoft Bot Framework Dialog using Autofac

前端 未结 1 1301
北海茫月
北海茫月 2021-02-14 08:28

I\'ve been trying to pass a service to a LuisDialog from the MessagesController like so:

public async Task Post([FromBody]Activity act         


        
1条回答
  •  情歌与酒
    2021-02-14 09:00

    In the Global.asax.cs, you can do do the following to register your services/dialogs:

    ContainerBuilder builder = new ContainerBuilder();
    
    builder.RegisterType()
      .As>()
      .InstancePerDependency();
    
    builder.RegisterType()
        .Keyed>(FiberModule.Key_DoNotSerialize)
        .AsImplementedInterfaces()
        .SingleInstance();
    
    builder.RegisterType()
        .Keyed(FiberModule.Key_DoNotSerialize)
        .AsImplementedInterfaces()
        .SingleInstance();
    
    builder.Update(Conversation.Container);
    

    In your controller, you can then resolve your main dialog as:

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

    0 讨论(0)
提交回复
热议问题