gragra33 / Blazing.Mvvm

MIT License
46 stars 10 forks source link

Blazor Server - Error on Service Initialization #4

Closed bbunderson closed 8 months ago

bbunderson commented 8 months ago

We are trying to use Blazing.Mvvm on Blazor Server. When registering the service with builder.Services.AddMvvmNavigation(); it throws an exception during build because the AspNetCore.Components.NavigationManager is registered as Scoped instead of a Singleton. Of course we could manually register the MvvmNavigation as Scoped, but what impact would this have? Not sure if your MvvmNavigationManager would operate properly if Scoped.

Getting the following error:

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Blazing.Mvvm.Components.IMvvmNavigationManager Lifetime: Singleton ImplementationType: Blazing.Mvvm.Components.MvvmNavigationManager': Cannot consume scoped service 'Microsoft.AspNetCore.Components.NavigationManager' from singleton 'Blazing.Mvvm.Components.IMvvmNavigationManager'.)'

Information on how the Default Services are registered: https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-7.0

gragra33 commented 8 months ago

@bbunderson I originally built the library to support WebAssembly (WASM) only for my own use and have shared it with others. With the release of .Net 8.0 imminent, I need to look at supporting the new interactive WebApp project.

You are right about Blazor Server. Scoped does not affect WASM projects. I have made the change and quickly put together a sample ServerSide project. I will release a new Nuget package once I have completed testing. However, for now, if you need early access for your own testing, check out the server branch.

gragra33 commented 8 months ago

I have found some time this afternoon to add multi-targeting to the Blazing.Mvvm for .Net 7.0 & 8.0RC2 + added a new Auto Blazor WebApp to test out the new Server/WebAssembly framework. You can try it out in the new DotNet8RC2 branch.

bbunderson commented 8 months ago

We have been prototyping various ways of using the Community Toolkit MVVM. Your library makes everything so elegant and addresses all the issues we have run in to. We are really just using the MvvmComponentBase and ViewModelBase at this time. We are using Blazor FluentUI and so we are not really using the MvvmNavigationManager. Two thumbs up on creating this! Thank you.

I do have a recommendation regarding the registration of service. You could put an option for HostingModel. (This is how Blazor FluentUI handles it.)

builder.Services.AddMvvmNavigation(options =>
{
    options.HostingModel = BlazorHostingModel.Server;
});
gragra33 commented 8 months ago

@bbunderson Glad that you find it useful.

As for your recommendation, here is a link to their implementation: fluentui-blazor / ServiceCollectionExtensions.cs. I m not sure how this fits our needs, however, can you create a branch off the server branch and create a pull request? I am more than happy to look at including it.