简体   繁体   English

C# IoC 配置:当它的参数之一不是已经注入的对象时,我如何注入一个对象?

[英]C# IoC configuration: how can I inject an object when one of its parameters it's not an already injected object?

I'm using the Microsoft.Extensions.DependencyInjection library and I have this concrete class which implements a generic interface called IInterface我正在使用Microsoft.Extensions.DependencyInjection库,并且我有这个具体的类,它实现了一个名为IInterface的通用接口

using System.Net.Http;

namespace my.namespace
{
    public class Concrete : IInterface
    {
        private readonly IHttpClientFactory _factory;
        private readonly string _s1;
        private readonly string _s2;

        public Concrete(IHttpClientFactory factory, string s1, string s2)
        {
            _factory = factory;
            _s1 = s1;
            _s2 = s2;
        }
    }
}

In my IocConfig.cs I'm injecting the IHttpClientFactory , like this:在我的 IocConfig.cs 中,我正在注入IHttpClientFactory ,如下所示:

services.AddHttpClient("clientName", client =>
            {
            });

Now I also want to inject the Concrete class.现在我还想注入Concrete类。 Since the s1 and s2 parameters are outside my already defined dependencies I think I should use the explicit constructor, like this:由于 s1 和 s2 参数超出了我已经定义的依赖项,我认为我应该使用显式构造函数,如下所示:

services.AddScoped<IInterface, Concrete>(sp => new Concrete(/* parameters */));

If all the parameters required by the constructor would have been already defined dependencies I should have done it this way:如果构造函数所需的所有参数都已经定义了依赖项,我应该这样做:

services.AddScoped<IOtherInterface, OtherConcrete>();

How can I inject this class correctly providing all the parameters needed?如何正确注入此类提供所需的所有参数? I think I need to explicitly pass IHttpClientFactory and also both s1 and s2 strings but I don't know how to do it.我想我需要明确传递 IHttpClientFactory 以及 s1 和 s2 字符串,但我不知道该怎么做。

The lambda function passed to AddScoped takes an IServiceProvider as input.传递给AddScoped的 lambda 函数将IServiceProvider作为输入。 You can use this to resolve an IHttpClientFactory by calling GetService<IHttpClientFactory>() .您可以通过调用GetService<IHttpClientFactory>()来使用它来解析IHttpClientFactory

services.AddScoped<IInterface, Concrete>(sp
    => new Concrete(sp.GetService<IHttpClientFactory>(), "s1", "s2"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IoC - 使用注入的对象参数解析 WPF 窗口 - IoC - resolve WPF windows with injected object parameters 使用Unity IoC容器时如何注入属性? - How can I inject a property when using Unity IoC container? 调用c#对象属性值“ get”时如何设置 - How can I set c# object property value when its “get” is called 当注入的对象是条件对象时的C#IoC实例化 - C# IoC Instantiation when the injected objects are conditional 当其中之一是必需的时,如何在c#中使用两个可选对象? - How i can use two optional object in c# when one of them is required? 当对象已设置为禁用时,如何将其设置为活动状态? - How can i set an object active when its already set to disabled? 在C#中,当括号表示不起作用时,如何访问对象的对象? - In C#, how can I access an object's objects when bracket notation doesn't work? c#如何检查一个文本框中是否已存在对象? 如果是,则不要将其添加到文本框中 - c# How do i check if an object is already in one of my textboxes? if it is then dont add it to a textbox 如何使用 C# 检查对象(我必须插入到列表中)是否已经在列表中? - How can I check if an object (that I have to insert in a list) is already in the list using C#? 在 C# 中创建对象时如何使用 if 语句? - How can i have an if statement when creating an object in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM