Skip to content

[Bug]: README example Throws for ReactiveUI.Avalonia.Microsoft.Extensions.DependencyInjection #42

@MidgeOnGithub

Description

@MidgeOnGithub

Describe the bug 🐞

In the README's example for the MS DI extension package, the order of the methods shown is:

  • .UseReactiveUIWithMicrosoftDependencyResolver(...)
  • .RegisterReactiveUIViewsFromEntryAssembly()

This results in a runtime System.InvalidOperationException. I believe this is related to the fact that .UseReactiveUIWithMicrosoftDependencyResolver(...) methods call serviceCollection.BuildServiceProvider() and use Splat MS DI extension methods to resolve with the built MS DI container.

var serviceProvider = serviceCollection.BuildServiceProvider();

Running .RegisterReactiveUIViewsFromEntryAssembly() afterwards tries to add the View services to the finalized resolver.

Reversing the order of the methods results in the application not throwing (though for a new Avalonia MVVM template project as shown in the steps to recreate, .RegisterReactiveUIViewsFromEntryAssembly() isn't necessary and can be removed).

I believe we get the same behaviors from any of the view registration methods, so you can also run the example with .RegisterReactiveUIViews(Assembly.GetExecutingAssembly()) after adding using System.Reflection and see the same runtime Exception.

Truncated stack trace:

Unhandled exception. System.InvalidOperationException: This container has already been built and cannot be modified.
   at Splat.Microsoft.Extensions.DependencyInjection.MicrosoftDependencyResolver.Register(Func`1 factory, Type serviceType, String contract) in c:\temp\releaser\splat\src\Splat.Microsoft.Extensions.DependencyInjection\MicrosoftDependencyResolver.cs:line 161
   at ReactiveUI.Avalonia.AppBuilderExtensions.RegisterViewsInternal(IMutableDependencyResolver resolver, Assembly[] assemblies) in c:\temp\releaser\ReactiveUI.Avalonia\src\ReactiveUI.Avalonia\AppBuilderExtensions.cs:line 234
   at ReactiveUI.Avalonia.AppBuilderExtensions.<>c__DisplayClass2_0.<RegisterReactiveUIViews>b__0(AppBuilder _) in c:\temp\releaser\ReactiveUI.Avalonia\src\ReactiveUI.Avalonia\AppBuilderExtensions.cs:line 124

Step to reproduce

  1. Create a new Avalonia MVVM project with ReactiveUI; one can run dotnet new avalonia.mvvm -o TestApp -m ReactiveUI after installing Avalonia dotnet templates (dotnet new install Avalonia.Templates)
  2. (Optionally) Update the project's target framework to .NET 10 and language version to C# 14; the error happens just as well if you stick with the template's .NET 9 and C# 13 output
  3. Add the Nuget package ReactiveUI.Avalonia.Microsoft.Extensions.DependencyInjection
  4. Chain onto BuildAvaloniaApp the methods shown in the documentation example for MS DI, like so:
using Avalonia;
using System;
using ReactiveUI.Avalonia.Splat;

namespace TestApp;

sealed class Program
{
    [STAThread]
    public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);

    public static AppBuilder BuildAvaloniaApp()
        => AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .WithInterFont()
            .LogToTrace()
            .UseReactiveUIWithMicrosoftDependencyResolver(_ => { }, null)
            .RegisterReactiveUIViewsFromEntryAssembly();
}
  1. Run the project

Reproduction repository

https://github.com/reactiveui/ReactiveUI

Expected behavior

Using README examples would not throw.

Screenshots 🖼️

No response

IDE

Rider Windows

Operating system

Windows

Version

No response

Device

No response

ReactiveUI.Avalonia Version

11.3.8

Additional information ℹ️

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions