ASP.NET Core option dependency in constructor

前端 未结 2 541
感动是毒
感动是毒 2021-01-15 04:19

I\'m using ASP.NET Core and I\'m attempting to create a resolvable class which has an optional parameter:

public class Foo
{
     public Foo() : this(null)
          


        
2条回答
  •  走了就别回头了
    2021-01-15 04:46

    You need to register the IValidator first:

      var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
      services.AddTransient, RealValidator>();
      services.AddTransient();
    
      var serviceProvider = services.BuildServiceProvider();
      var validator = serviceProvider.GetService>();
      var foo = serviceProvider.GetService();
    
      Assert.NotNull(validator);
      Assert.NotNull(foo);
    

提交回复
热议问题