Skip to content

Inject Service Provider To Generic Host #89

Answered by NikolayPianikov
c0nstexpr asked this question in Q&A
Discussion options

You must be logged in to vote

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 calling DependsOn(Base). Here is the working code:

// 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 IMyDepen…

Replies: 2 comments 20 replies

Comment options

You must be logged in to vote
1 reply
@c0nstexpr
Comment options

Answer selected by c0nstexpr
Comment options

You must be logged in to vote
19 replies
@NikolayPianikov
Comment options

@ninety7
Comment options

@ninety7
Comment options

@NikolayPianikov
Comment options

@ninety7
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants