Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.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# 使用Autofac解析所有特定类型_C#_Asp.net Core_Autofac - Fatal编程技术网

C# 使用Autofac解析所有特定类型

C# 使用Autofac解析所有特定类型,c#,asp.net-core,autofac,C#,Asp.net Core,Autofac,在ASP.NET核心项目启动时,我有: services.AddScoped<SingleInstanceFactory>(x => y => x.GetRequiredService(y)); services.AddScoped<MultiInstanceFactory>(x => y => x.GetRequiredServices(y)); 不知道我错过了什么。。。有什么想法吗?要解析T的所有注册服务,您可以解析IEnumerable 顺

在ASP.NET核心项目启动时,我有:

services.AddScoped<SingleInstanceFactory>(x => y => x.GetRequiredService(y));
services.AddScoped<MultiInstanceFactory>(x => y => x.GetRequiredServices(y));

不知道我错过了什么。。。有什么想法吗?

要解析T的所有注册服务,您可以解析
IEnumerable

顺便说一下,您正在尝试注册Autofac不需要的委托(请参阅),并且由于Autofac自动使用合成(请参阅),因此您不需要注册任何内容


如果您想解析
多实例工厂
单实例工厂
您不需要在Autofac中注册任何东西

来解析t的所有注册服务,您可以解析
IEnumerable

顺便说一下,您正在尝试注册Autofac不需要的委托(请参阅),并且由于Autofac自动使用合成(请参阅),因此您不需要注册任何内容


如果要解析
MultiInstanceFactory
SingleInstanceFactory
您不需要在Autofac

中用代码注册任何内容,我会得到错误:无法将lambda表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型I还添加了一个扩展,作为对我的问题的更新,但我得到了与代码中相同的错误。知道为什么吗?什么是多实例工厂?尝试新的多实例工厂(y.Resolve…)MultiInstanceFactory如下所示:使用您的代码时,我遇到了错误:无法将lambda表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型。我还添加了一个扩展名,作为对我的问题的更新,但我得到的错误与代码中的错误相同。知道为什么吗?什么是多实例工厂?请尝试新的多实例工厂(y.Resolve…)MultiInstanceFactory如下所示:
builder.Register<SingleInstanceFactory>(y => z => y.Resolve(z)).InstancePerLifetimeScope();
builder.Register<MultiInstanceFactory>(y => z => y.ResolveAll(z)).InstancePerLifetimeScope();
public static T[] ResolveAll<T>(this IContainer container) {
  return container.Resolve<IEnumerable<T>>().ToArray();
}

public static Object[] ResolveAll(this IContainer container, Type type) {
  Type enumerable = typeof(IEnumerable<>).MakeGenericType(type);
  return (Object[])container.ResolveService(new TypedService(enumerable));
}
x.Register<MultiInstanceFactory>(y => z => y.ResolveAll(z)).InstancePerLifetimeScope();   
Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type