title: How to use Autofac as DI container in Avalonia.ReactiveUI?
body: <p>I am trying to override the DI container used in Avalonia and ReactiveUI with Autofac. So far I have tried following the <a href="https://github.com/reactiveui/splat/blob/main/src/Splat.Autofac/README.md" rel="nofollow noreferrer">instructions in the <code>Splat.Autofac</code> repository</a>, but I can't get Avalonia to work.</p>
  <p>As a working example, I took the <a href="https://github.com/reactiveui/ReactiveUI.Samples/tree/main/avalonia/hello-world/HelloWorld" rel="nofollow noreferrer">HelloWorld example from the ReactiveUI.Samples</a> repository.</p>
  <p>I could think of two places to override Splat. In <code>App.xaml.cs</code> in the <code>OnFrameworkInitializationCompleted</code> method:</p>
  <pre><code>using System.Reflection;
  
  using Autofac;
  
  using Avalonia;
  using Avalonia.Markup.Xaml;
  using Avalonia.ReactiveUI;
  using Avalonia.Threading;
  
  using ReactiveUI;
  
  using Splat;
  using Splat.Autofac;
  
  namespace ReactiveAvalonia.HelloWorld {
      public class App : Application {
          public override void Initialize() {
              AvaloniaXamlLoader.Load(this);
          }
  
          public override void OnFrameworkInitializationCompleted()
          {
              // Build a new Autofac container.
              var builder = new ContainerBuilder();
              builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();
  
              // Use Autofac for ReactiveUI dependency resolution.
              // After we call the method below, Locator.Current and
              // Locator.CurrentMutable start using Autofac locator.
              AutofacDependencyResolver resolver = new AutofacDependencyResolver(builder);
              Locator.SetLocator(resolver);
  
              // These .InitializeX() methods will add ReactiveUI platform 
              // registrations to your container. They MUST be present if
              // you *override* the default Locator.
              Locator.CurrentMutable.InitializeSplat();
              Locator.CurrentMutable.InitializeReactiveUI();
  
              var container = builder.Build();
              resolver.SetLifetimeScope(container);
          }
      }
  }
  </code></pre>
  <p>But when running this, the startup fails with the following exception:</p>
  <blockquote>
  <p>System.ArgumentException: 'Don't know how to detect when ReactiveAvalonia.HelloWorld.MainView is activated/deactivated, you may need to implement IActivationForViewFetcher'</p>
  </blockquote>
  <p>My other idea was to do the overriding in <code>Program.cs</code>'s <code>Main</code>:</p>
  <pre><code>using System.Reflection;
  
  using Autofac;
  
  using Avalonia;
  using Avalonia.Controls;
  using Avalonia.Logging.Serilog;
  using Avalonia.ReactiveUI;
  using Avalonia.Threading;
  
  using ReactiveUI;
  
  using Splat;
  using Splat.Autofac;
  
  namespace ReactiveAvalonia.HelloWorld {
  
      // You may want to start here:
      // https://reactiveui.net/docs/getting-started/
  
      class Program {
          // http://avaloniaui.net/docs/reactiveui/
          // https://github.com/AvaloniaUI/Avalonia/wiki/Application-lifetimes
          public static AppBuilder BuildAvaloniaApp() {
              return AppBuilder
                  .Configure&lt;App&gt;()
                  .UseReactiveUI()
                  .UsePlatformDetect()
                  .LogToDebug();
          }
  
          private static void AppMain(Application app, string[] args) {
              app.Run(new MainView());
          }
  
          public static void Main(string[] args) {
              // Build a new Autofac container.
              var builder = new ContainerBuilder();
              builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();
  
              // Use Autofac for ReactiveUI dependency resolution.
              // After we call the method below, Locator.Current and
              // Locator.CurrentMutable start using Autofac locator.
              AutofacDependencyResolver resolver = new AutofacDependencyResolver(builder);
              Locator.SetLocator(resolver);
  
              // These .InitializeX() methods will add ReactiveUI platform 
              // registrations to your container. They MUST be present if
              // you *override* the default Locator.
              Locator.CurrentMutable.InitializeSplat();
              Locator.CurrentMutable.InitializeReactiveUI();
  
              var container = builder.Build();
              resolver.SetLifetimeScope(container);
  
              BuildAvaloniaApp().Start(AppMain, args);
          }
      }
  }
  </code></pre>
  <p>But this fails with another exception:</p>
  <blockquote>
  <p>System.Exception: 'Container has already been built and the lifetime scope set, so it is not possible to modify it anymore.'</p>
  </blockquote>
  <p>What can I do to get this to work?</p>