Injecting external dependencies in Microsoft Bot Framework Dialog using Autofac

前端 未结 1 1303
北海茫月
北海茫月 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<IntroDialog>()
      .As<IDialog<object>>()
      .InstancePerDependency();
    
    builder.RegisterType<JobsMapper>()
        .Keyed<IMapper<DocumentSearchResult, GenericSearchResult>>(FiberModule.Key_DoNotSerialize)
        .AsImplementedInterfaces()
        .SingleInstance();
    
    builder.RegisterType<AzureSearchClient>()
        .Keyed<ISearchClient>(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<IDialog<object>>());
    }
    
    0 讨论(0)
提交回复
热议问题