-
-
Notifications
You must be signed in to change notification settings - Fork 2
Description
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.
Line 52 in 78f5dc4
| var serviceProvider = serviceCollection.BuildServiceProvider(); |
Running .RegisterReactiveUIViewsFromEntryAssembly() afterwards tries to add the View services to the finalized resolver.
| resolver.Register( |
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
- Create a new Avalonia MVVM project with ReactiveUI; one can run
dotnet new avalonia.mvvm -o TestApp -m ReactiveUIafter installing Avalonia dotnet templates (dotnet new install Avalonia.Templates) - (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
- Add the Nuget package
ReactiveUI.Avalonia.Microsoft.Extensions.DependencyInjection - Chain onto
BuildAvaloniaAppthe 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();
}- 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