Dotnet核心依赖注入参数

时间:2017-08-11 20:46:39

标签: c# dependency-injection .net-core

假设我有班级

public class Entity : IEntity {
    public Entity(IDependency dep, string url)
    {
        //...
    }
}

public class Dependency : IDependency {
    //...
}

现在,当我想使用依赖注入时,我可以执行以下操作:

IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<IDependency, Dependency>();
serviceCollection.AddScoped<IEntity, Entity>(); // how to inject the url

正如您所看到的,问题在于我不知道如何注入网址值(例如&#34; https://google.com/&#34;)。我知道结构图提供了命名参数及其值。

在使用DI作为依赖项时,有没有办法将字符串注入Entity的构造函数?

0 个答案:

没有答案