© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
7 replies
Yawnder

DI: Registration of services through a builder.

I'm making a library to be "registered" like
services.AddMyFeature(builder => ...);
services.AddMyFeature(builder => ...);

The
builder
builder
can be used a bit like ASP's registration with the
AddAuthorization
AddAuthorization
,
AddPolicy
AddPolicy
, etc.
For example:
services.AddAuthorization(c =>
{
    var tenantAuthRequirement = new TenantAuthorizationRequirement();
    c.AddPolicy(Policies.TenantAuthenticatedUser, p =>
    {
        p.AddAuthenticationSchemes(PreSharedKeyAuthenticator.Scheme, PartitionConstants.AuthenticationSchemaBearer)
          .RequireAuthenticatedUser()
          .AddRequirements(tenantAuthRequirement);
    });
}
services.AddAuthorization(c =>
{
    var tenantAuthRequirement = new TenantAuthorizationRequirement();
    c.AddPolicy(Policies.TenantAuthenticatedUser, p =>
    {
        p.AddAuthenticationSchemes(PreSharedKeyAuthenticator.Scheme, PartitionConstants.AuthenticationSchemaBearer)
          .RequireAuthenticatedUser()
          .AddRequirements(tenantAuthRequirement);
    });
}


I want one of these
builder.AddSubFeature()
builder.AddSubFeature()
(the equivalent of the
c.AddPolicy(...)
c.AddPolicy(...)
above) to add some more registrations in
services
services
.

The way I was thinking about it was to have an internal
List<Action<IServiceCollection>>
List<Action<IServiceCollection>>
on the
builder
builder
in which
builder.AddSubFeature()
builder.AddSubFeature()
would add custom registrations.
Any better suggestion?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ builder.Services.AddDbContext<>
C#CC# / help
3y ago
✅ read-only builder.Services
C#CC# / help
2y ago
✅ Identity Services Registration not working
C#CC# / help
2y ago
DI Service Usage in Quartz.NET registration
C#CC# / help
13mo ago