最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How do I ensure that certain services are not overridden by user registrations - Stack Overflow

programmeradmin2浏览0评论

I know that lots of library code relies on TryAdd* and TryAddEnumerable methods to NOT overwrite user-supplied registrations for certain services and only provide default implementations if none have been registered.

However, what if I need to make sure that user code does not/cannot override my library's default service registrations? I've played around with some form of AsStrict and asked a question here, but this among other shortcomings relies on the user to implement it in his own code-base.

Is there a pattern or general way of structuring AddMyLibrary so that I can ensure that I have fixed dependencies that the user cannot provide their implementation for.

I know that lots of library code relies on TryAdd* and TryAddEnumerable methods to NOT overwrite user-supplied registrations for certain services and only provide default implementations if none have been registered.

However, what if I need to make sure that user code does not/cannot override my library's default service registrations? I've played around with some form of AsStrict and asked a question here, but this among other shortcomings relies on the user to implement it in his own code-base.

Is there a pattern or general way of structuring AddMyLibrary so that I can ensure that I have fixed dependencies that the user cannot provide their implementation for.

Share Improve this question edited Jan 21 at 12:49 Ivan Petrov asked Jan 18 at 15:41 Ivan PetrovIvan Petrov 5,1402 gold badges11 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

To ensure that your library's service registrations cannot be overridden by the user, you can use IHostedService. This approach runs validation at application startup and prevents the user from overriding your critical service registrations.

your service :

public interface IMyService
{
    void Execute();
}

public class MyService : IMyService
{
    public void Execute()
    {
        Console.WriteLine("Executing MyService");
    }
}

Hosted service for validation:

internal class ValidationHostedService(IServiceProvider serviceProvider) : IHostedService
{
    public Task StartAsync(CancellationToken cancellationToken)
    {
        // Validate required services at application startup
        var myService = serviceProvider.GetRequiredService<IMyService>();
        if (myService.GetType() != typeof(MyService))
        {
            throw new InvalidOperationException("IMyService cannot be overridden.");
        }

        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        // No specific stop logic required
        return Task.CompletedTask;
    }
}

Service Collection Extension :

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddExampleLibrary(this IServiceCollection services)
    {
        // Register your library's default service
        services.AddSingleton<IMyService, MyService>();

        // Register a hosted service for automatic validation
        services.AddHostedService<ValidationHostedService>();

        return services;
    }
}
发布评论

评论列表(0)

  1. 暂无评论