reactiveui/splat

[BUG] Types registered directly in Autofac container are not resolved

artlaskow opened this issue · 6 comments

Describe the bug

Calling Autofac's container.RegisterType<MyService>().As<IMyService>() as in official example does not work - type can't be later resolved with a Locator.Current.GetService<T>. Registering with Locator.CurrentMutable.Register when using Autofac works fine.

Steps To Reproduce

  1. Create minimal application ReactiveUI using WPF and .Net 5.0.
  2. Follow the official guide for custom DI, using Splat.Autofac
  3. Add container.RegisterType<MyService>().As<IMyService>() right under the registration of MainWindow, as in example
  4. Use Locator.Current.GetService<IMyService>() in a ViewModel constructor.
  5. Observe that returned value is null

Expected behavior

MyService is exported correctly, both when using Locator.CurrentMutable.Register and container.RegisterType<T> when using Splat.Autofac.

Screenshots

Environment

  • OS: Windows 10
  • Version 1909
  • Device: Desktop

Additional context

There's probability I am doing something incorrectly. I tried to follow the guide as best as I could. In that case the official example should be corrected.
Full example:

public partial class App : Application
{
    public App()
    {
	var container = new ContainerBuilder();
	container.RegisterType<MainWindow>().As<IViewFor<MainViewModel>>();
	container.RegisterType<ImageSelection>().As<IImageSelection>();

	container.UseAutofacDependencyResolver();

	Locator.CurrentMutable.InitializeSplat();
	Locator.CurrentMutable.InitializeReactiveUI();

	//Locator.CurrentMutable.Register(() => new ImageSelection(), typeof(IImageSelection));
    }
}

Uncommenting the last line makes it work.

https://github.com/reactiveui/splat/tree/main/src/Splat.Autofac#register-the-adapter-to-splat

The implementation changed recently to handle the immutability of Autofac.

GitHub
Makes things cross-platform. Contribute to reactiveui/splat development by creating an account on GitHub.

Version of Splat?
Version of Autofac?

Curious if this is new, or an upgrade of existing dependencies?

    <PackageVersion Include="Splat.Autofac" Version="9.8.1" />
    <PackageVersion Include="Autofac" Version="6.1.0" />

Completely a new project

Hey @artlaskow,
try this code

            var builder = new ContainerBuilder();
            builder.RegisterAssemblyModules(assembly);
            var resolver = builder.UseAutofacDependencyResolver();
            resolver.InitializeSplat();
            resolver.InitializeReactiveUI();
            builder.RegisterInstance(resolver);

            _scope = builder.Build();

            resolver.SetLifetimeScope(_scope);

I believe this is resolved in the latest releases.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.