Inject Service Provider To Generic Host #89
-
I would like to use Pure.DI in generic host and replace the MS service provider to my own composition
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 20 replies
-
To register composition roots in MS DI, you must use the Pure.DI setup that are defined in the // See https://aka.ms/new-console-template for more information
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Pure.DI;
using Pure.DI.MS;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder();
builder.ConfigureContainer(new Composition());
var host = builder.Build();
_ = Task.Run(host.Run);
var service = host.Services.GetRequiredService<IMyService>();
service.Bar();
public interface IMyDependency : IDisposable
{
void Bar() => Console.WriteLine(this);
void IDisposable.Dispose()
{
}
}
public class MyDependency : IMyDependency;
public interface IMyService : IDisposable
{
void Bar() => Console.WriteLine(this);
void IDisposable.Dispose()
{
}
}
public class MyService(IMyDependency _) : IMyService;
public partial class Composition :
ServiceProviderFactory<Composition>,
IKeyedServiceProvider,
IServiceScopeFactory,
IServiceScope
{
[Conditional(nameof(DI))]
static void Setup() => DI.Setup()
.DependsOn(Base)
.Hint(Hint.ObjectResolveMethodName, nameof(GetService))
.Hint(Hint.ObjectResolveByTagMethodName, nameof(GetRequiredKeyedService))
.RootBind<IMyDependency>().As(Lifetime.Singleton).To<MyDependency>()
.RootBind<IMyService>().As(Lifetime.Singleton).To<MyService>();
public IServiceProvider ServiceProvider => this;
public IServiceScope CreateScope() => new Composition(this);
public object GetKeyedService(Type serviceType, object? serviceKey) =>
GetRequiredKeyedService(serviceType, serviceKey);
} Your composition setup code can be simplified: public partial class Composition : ServiceProviderFactory<Composition>
{
private static void Setup() => DI.Setup()
.DependsOn(Base)
.DefaultLifetime(Lifetime.Singleton)
.Bind().To<MyDependency>()
.RootBind<IMyService>().To<MyService>();
} |
Beta Was this translation helpful? Give feedback.
-
@NikolayPianikov I'm working on a MAUI app with ReactorUI framework and am having an issue. I have defined my The problem is that I don't know how to get the Here's my Composition class: public partial class Composition :
ServiceProviderFactory<Composition>,
IServiceProvider
{
private static void Setup() => DI.Setup()
.DependsOn(Base)
.Hint(Hint.OnCannotResolveContractTypeNameRegularExpression, "^Microsoft\\.(Extensions|Maui)\\..+$")
.Root<SplashScreenPage>(nameof(SplashScreenPage))
.Bind<IAuthenticate>().As(Lifetime.Singleton).To<AndroidAuthenticator>();
public IServiceProvider ServiceProvider => this; // this throws a stack overflow exception when calling new Composition().ServiceProvider
public object GetService(Type serviceType)
{
// TODO: how to implement this without access to the serviceprovider?
}
} Is there something I'm missing? |
Beta Was this translation helpful? Give feedback.
To register composition roots in MS DI, you must use the Pure.DI setup that are defined in the
ServiceProviderFactory<Composition>
class. To do this, you can add a dependency on it by callingDependsOn(Base)
. Here is the working code: