tryphotino / photino.Blazor

https://tryphotino.io
Apache License 2.0
338 stars 63 forks source link

Need to call UseServiceProviderFactory #83

Open sam-wheat opened 1 year ago

sam-wheat commented 1 year ago

PhotinoBlazorAppBuilder does not implement IHostBuilder so I can not use this. Is there a workaround?

I need to use Autofac service provider:

builder.UseServiceProviderFactory(new AutofacServiceProviderFactory());

MikeYeager commented 1 year ago

@sam-wheat Photino.Blazor is built on the Microsoft.NETCore.App framework. In order to do what you're asking, we'd have to switch to the Microsoft.AspNETCore.App framework which would bloat the project a bit. While this may be a reasonable step to take, it's not a minor change. You can use the default dependency injection, but using Autofac won't be supported until this change is made, tested and published, which likely won't happen soon unless you or someone in the community picks this up.

sam-wheat commented 1 year ago

I actually did look at implementing IHostBuilder - it is something I'd like to do but unfortunately it is yet another distraction from my main project which is long overdue.

We'd have to switch to the Microsoft.AspNETCore.App framework

You should be able to just add a reference to Microsoft.Extensions.Hosting, correct?

The workaround is pretty simple but not something I would want to do in a large app:

// Program.cs

var builder = PhotinoBlazorAppBuilder.CreateDefault(args);
ContainerBuilder containerBuilder = new();
// add services to containerBuilder and dotnet container here
containerBuilder.Populate(builder.Services);
IContainer container = containerBuilder.Build();
builder.Services.AddSingleton(typeof(IContainer), container);
app = builder.Build();

// Your page

@code {

    [Inject] IContainer container { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        ILifetimeScope scope = container.BeginLifetimeScope();
        myService = scope.Resolve<IMyService>();
    }
philippjbauer commented 2 weeks ago

This change has been proposed in the following PR. We still need to test it ...

https://github.com/tryphotino/photino.Blazor/pull/122