Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将IServiceCollection扩展插件与DryIoc一起使用_C#_Asp.net Core_Dependency Injection_Dryioc - Fatal编程技术网

C# 将IServiceCollection扩展插件与DryIoc一起使用

C# 将IServiceCollection扩展插件与DryIoc一起使用,c#,asp.net-core,dependency-injection,dryioc,C#,Asp.net Core,Dependency Injection,Dryioc,IServiceCollection有许多扩展——在我的例子中,我使用了AddHttpClient 我的场景是在Startup.cs中的ConfigureServices方法中注册常规内容,其中IServiceCollection用于注册服务。仅在特定项目中需要的所有内容都在相应项目的扩展方法中注册,但由于DryIoc容器必须集成到ASP.NET核心项目中,因此使用了DryIocIContainer 现在我有了一个HttpClient,我只需要在特定的项目中使用它。因此,我想把它的注册放在相应的

IServiceCollection有许多扩展——在我的例子中,我使用了
AddHttpClient

我的场景是在
Startup.cs
中的
ConfigureServices
方法中注册常规内容,其中
IServiceCollection
用于注册服务。仅在特定项目中需要的所有内容都在相应项目的扩展方法中注册,但由于DryIoc容器必须集成到ASP.NET核心项目中,因此使用了DryIoc
IContainer

现在我有了一个HttpClient,我只需要在特定的项目中使用它。因此,我想把它的注册放在相应的项目中。问题是我想为它使用AddHttpClient,我通常只能与IServiceCollection一起使用

我的问题是:有没有办法在我的其他项目中使用它。可能是从DryIoc容器或其他什么地方得到的

这是所述文件的一般结构:

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.RegisterSomeService();
        // register other stuff
    }
}
public class Startup
{
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseServiceProviderFactory(new DryIocServiceProviderFactory())
            .ConfigureContainer<Container>(SomeProject.ContainerSetup.Initialize);
}

Program.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.RegisterSomeService();
        // register other stuff
    }
}
public class Startup
{
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseServiceProviderFactory(new DryIocServiceProviderFactory())
            .ConfigureContainer<Container>(SomeProject.ContainerSetup.Initialize);
}

公共类启动
{
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.UseStartup();
})
.UseServiceProviderFactory(新的DryIocServiceProviderFactory())
.ConfigureContainer(SomeProject.ContainerSetup.Initialize);
}
某个项目中的ContainerSetup.cs

public static class ContainerSetup
{
    public static void Initialize(HostBuilderContext hostContext, IContainer container)
    {
        container.Register<SomeService>();
        // register other stuff
        // here I want to use AddHttpClient
    }
}
公共静态类容器设置
{
公共静态void初始化(HostBuilderContext hostContext,IContainer容器)
{
container.Register();
//登记其他资料
//这里我想使用AddHttpClient
}
}

我建议查看AddHttpClient内部

并且可以使用
IContainer
而不是服务集合进行相同的注册

更新:
另一个想法是将IServiceCollection注册到自身中(或者它已经自动注册了?),然后从IContainer和AddHttpClient解析它。

我建议查看AddHttpClient内部

并且可以使用
IContainer
而不是服务集合进行相同的注册

更新:
另一个想法是将IServiceCollection注册到自身中(或者它可能已经自动注册了?),然后从IContainer和AddHttpClient解决它。

我能够通过使用IContainer扩展
填充
来解决这个问题,它是

我用它编辑了ContainerSetup.cs,如下所示

public static class ContainerSetup
{
    public static void Initialize(HostBuilderContext hostContext, IContainer container)
    {        
        var services = new ServiceCollection();
        
        services.AddHttpClient<MyTypedClient>()
            .Configure[...];
        
        container.Populate(services); // with this call all services registered above will be registered in the container
        
        // register other stuff if necessary
        container.Register<SomeService>();
    }
}
公共静态类容器设置
{
公共静态void初始化(HostBuilderContext hostContext,IContainer容器)
{        
var services=newservicecolection();
services.AddHttpClient()
.配置[…];
container.Populate(services);//通过此调用,上面注册的所有服务都将在容器中注册
//如有必要,登记其他资料
container.Register();
}
}

我能够通过使用
IContainer
扩展
填充
解决问题,该扩展是的一部分

我用它编辑了ContainerSetup.cs,如下所示

public static class ContainerSetup
{
    public static void Initialize(HostBuilderContext hostContext, IContainer container)
    {        
        var services = new ServiceCollection();
        
        services.AddHttpClient<MyTypedClient>()
            .Configure[...];
        
        container.Populate(services); // with this call all services registered above will be registered in the container
        
        // register other stuff if necessary
        container.Register<SomeService>();
    }
}
公共静态类容器设置
{
公共静态void初始化(HostBuilderContext hostContext,IContainer容器)
{        
var services=newservicecolection();
services.AddHttpClient()
.配置[…];
container.Populate(services);//通过此调用,上面注册的所有服务都将在容器中注册
//如有必要,登记其他资料
container.Register();
}
}

是的,这正是我的两个想法。第一个是可能的,但非常难看,因为在
AddHttpClient
中有太多的注册。我还检查了第二种方法。默认情况下,它没有注册,但我认为这是可能的。唯一一件事我不知道它是否会有任何不良副作用。我明天会试试这个。不,好像不行。注册仅在已解析的
IServiceCollection
实例中完成。在此之后,它们不再是DryIoC容器的一部分。或者我做错了。是的,这正是我的两个想法。第一个是可能的,但非常难看,因为在
AddHttpClient
中有太多的注册。我还检查了第二种方法。默认情况下,它没有注册,但我认为这是可能的。唯一一件事我不知道它是否会有任何不良副作用。我明天会试试这个。不,好像不行。注册仅在已解析的
IServiceCollection
实例中完成。在此之后,它们不再是DryIoC容器的一部分。也许我做错了。