title: Injecting external dependencies in Microsoft Bot Framework Dialog using Autofac
body: <p>I've been trying to pass a service to a LuisDialog from the MessagesController like so:</p>
  
  <pre><code>public async Task&lt;HttpResponseMessage&gt; Post([FromBody]Activity activity)
  ...
  await Conversation.SendAsync(activity, () =&gt; new ActionDialog(botService));
  </code></pre>
  
  <p>The botService is injected into the MessageController using dependency injection.</p>
  
  <p>When I start a bot conversation I get an error:</p>
  
  <p>Type 'ThetaBot.Services.BotService' in Assembly 'ThetaBot.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.</p>
  
  <p>Looking around for a solution I can find:
  <a href="https://github.com/Microsoft/BotBuilder/issues/106" rel="noreferrer">https://github.com/Microsoft/BotBuilder/issues/106</a></p>
  
  <blockquote>
    <p>I understand your question better now. We have a similar issue with service objects that we want to instantiate from the container rather than from the serialized blob. Here is how we register those objects in the container - we apply special handling during deserialiation for all objects with the key Key_DoNotSerialize:</p>
  </blockquote>
  
  <pre><code>builder
          .RegisterType&lt;BotToUserQueue&gt;()
          .Keyed&lt;IBotToUser&gt;(FiberModule.Key_DoNotSerialize)
          .AsSelf()
          .As&lt;IBotToUser&gt;()
          .SingleInstance();
  </code></pre>
  
  <p>However I cannot find an example or documentation that details how to register your own dependencies into the existing Bot Framework modules.</p>
  
  <p>I also found <a href="https://github.com/Microsoft/BotBuilder/issues/252" rel="noreferrer">https://github.com/Microsoft/BotBuilder/issues/252</a> which indicates that it should be possible to instantiate the dialogs from the container. </p>
  
  <p>I have tried this suggestion:</p>
  
  <pre><code>Func&lt;IDialog&lt;object&gt;&gt; makeRoot = () =&gt; actionDialog;
  await Conversation.SendAsync(activity, makeRoot);
  </code></pre>
  
  <p>Together with:</p>
  
  <pre><code>builder
              .RegisterType&lt;ActionDialog&gt;()
              .Keyed&lt;ActionDialog&gt;(FiberModule.Key_DoNotSerialize)
              .AsSelf()
              .As&lt;ActionDialog&gt;()
              .SingleInstance();
  </code></pre>
  
  <p>This does not work.</p>
  
  <p>I have also tried:</p>
  
  <pre><code>var builder = new ContainerBuilder();
  builder.RegisterModule(new DialogModule_MakeRoot());
  
  // My application module
  builder.RegisterModule(new ApplicationModule());
  
  using (var container = builder.Build())
  using (var scope = DialogModule.BeginLifetimeScope(container, activity))
  {
      await Conversation.SendAsync(activity, () =&gt; scope.Resolve&lt;ActionDialog&gt;());
  }
  </code></pre>
  
  <p>Together with the following in the ApplicationModule:</p>
  
  <pre><code>            builder
              .RegisterType&lt;ActionDialog&gt;()
              .Keyed&lt;ActionDialog&gt;(FiberModule.Key_DoNotSerialize)
              .AsSelf()
              .As&lt;ActionDialog&gt;()
              .SingleInstance();
  </code></pre>
  
  <p>This does not work and I encounter the same issue.</p>
  
  <p>If I simply mark all the services and their dependencies as serializable I can get this to work without the need to use FiberModule.Key_DoNotSerialize.</p>
  
  <p>The question is - what is the correct / preferred / recommended way to register and inject dependencies into Bot Framework Dialogs?</p>