You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

provider.cs 1014 B

1234567891011121314151617181920212223242526
  1. public class UtilizingProvider
  2. {
  3. private readonly IServiceProvider _provider;
  4. private readonly AnyService _service;
  5. // This service is allowed to be null because it is only populated if the service is actually available in the provider.
  6. private readonly AnyOtherService? _otherService;
  7. // This constructor injects only the service provider,
  8. // and uses it to populate the other dependencies.
  9. public UtilizingProvider(IServiceProvider provider)
  10. {
  11. _provider = provider;
  12. _service = provider.GetRequiredService<AnyService>();
  13. _otherService = provider.GetService<AnyOtherService>();
  14. }
  15. // This constructor injects the service provider, and AnyService,
  16. // making sure that AnyService is not null without having to call GetRequiredService
  17. public UtilizingProvider(IServiceProvider provider, AnyService service)
  18. {
  19. _provider = provider;
  20. _service = service;
  21. _otherService = provider.GetService<AnyOtherService>();
  22. }
  23. }