public IEnumerable <WeatherForecast> Get( [FromServices] ITestTransient testTransient1, // 在参数中通过增加[FromService]对参数进行注入 [FromServices] ITestTransient testTransient2, [FromServices] ITestScoped testScoped1, [FromServices] ITestScoped testScoped2, [FromServices] ITestSignleton testSignleton1, [FromServices] ITestSignleton testSignleton2 ) { Console.WriteLine($"--------------请求开始--------------"); Console.WriteLine($"Transient1 HashCode:{testTransient1.GetHashCode()}"); Console.WriteLine($"Transient2 HashCode:{testTransient2.GetHashCode()}"); Console.WriteLine($"ITestScoped1 HashCode:{testScoped1.GetHashCode()}"); Console.WriteLine($"ITestScoped2 HashCode:{testScoped2.GetHashCode()}"); Console.WriteLine($"ITestSignleton1 HashCode:{testSignleton1.GetHashCode()}"); Console.WriteLine($"ITestSignleton2 HashCode:{testSignleton2.GetHashCode()}"); Console.WriteLine($"--------------请求结束--------------"); var rng = new Random(); return(Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray()); }
public DemoController(ITestOne testOne, ITestTwo testTwo, ITestThree testThree , ITestTransient testTransient, ITestScoped testScoped, ITestSingleton testSingleton, TestService testService , IOptions <ConfigOptions> options ) { this._cfgContent = options.Value; _testOne = testOne; _testTwo = testTwo; _testThree = testThree; _testTransient = testTransient; _testScoped = testScoped; _testSingleton = testSingleton; _testService = testService; }
public TestService(ITestTransient testTransient, ITestScoped testScoped, ITestSingleton testSingleton) { TestTransient = testTransient; TestScoped = testScoped; TestSingleton = testSingleton; }
public ValuesController(ITestTransient testTransient, ITestScoped testScoped, ITestSingleton testSingleton) { _testTransient = testTransient; _testScoped = testScoped; _testSingleton = testSingleton; }