为什么无法从生命周期范围解析键入的注册?

huangapple go评论57阅读模式
英文:

Why cannot I resolve keyed registrations from lifetime scope?

问题

我正在尝试从生命周期范围解析带键的服务。以下代码呈现了问题:

public class Service1 : IService1 { }

public interface IService1 { }

public class Service2 : IService2 
{
    public Service2([KeyFilter("Test")] IService1 service1) { }
}

public class IService2 { }

public static class Program
{
    public static void Main(string[] args)
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<Service1>().Named<IService1>("Test");
        builder.RegisterType<Service2>().As<IService2>();
        var container = builder.Build();

        using (var scope = container.BeginLifetimeScope())
        {
            var service2 = scope.Resolve<IService2>();
        }
    }
}

我的意图是为特定的其他服务创建多个Service1注册。我需要使用范围,因为我已将DbContext注册为范围,服务需要它才能正常工作。

在启动演示代码后,我遇到了异常:

Autofac.Core.DependencyResolutionException: '在激活 ManagedConsoleSketchbook.Service2 时抛出了异常。'

内部异常
DependencyResolutionException: 无法使用可用的服务和参数调用类型 'ManagedConsoleSketchbook.Service2' 上找到的任何构造函数:
无法解析参数 'ManagedConsoleSketchbook.IService1 service1' 的构造函数 'Void .ctor(ManagedConsoleSketchbook.IService1)'。

我做错了什么吗?为什么无法从生命周期范围中解析具有名称/键的服务?

英文:

I'm trying to resolve keyed service from a lifetime scope. The following code presents the issue:

public class Service1 : IService1 { }

public interface IService1 { }

public class Service2 : IService2 
{
    public Service2([KeyFilter(&quot;Test&quot;)] IService1 service1) { }
}

public class IService2 { }

public static class Program
{
    public static void Main(string[] args)
    {
        var builder = new ContainerBuilder();
        builder.RegisterType&lt;Service1&gt;().Named&lt;IService1&gt;(&quot;Test&quot;);
        builder.RegisterType&lt;Service2&gt;().As&lt;IService2&gt;();
        var container = builder.Build();

        using (var scope = container.BeginLifetimeScope())
        {
            var service2 = scope.Resolve&lt;IService2&gt;();
        }
    }
}

My intention is to create multiple Service1s registrations per specific other services. I need scopes, because I have registered DbContext as scoped and services needs it to work properly.

After starting the demo code, I'm getting the exception:

Autofac.Core.DepencencyResolutionException: &#39;An exception was thrown while activating ManagedConsoleSketchbook.Service2.&#39;

Inner Exception
DependencyResolutionException: None of the constructors found on type &#39;ManagedConsoleSketchbook.Service2&#39; can be invoked with the available services and parameters:
Cannot resolve parameter &#39;ManagedConsoleSketchbook.IService1 service1&#39; of constructor &#39;Void .ctor(ManagedConsoleSketchbook.IService1)&#39;.

Am I doing something wrong? Why cannot I resolve named/keyed service from a lifetime scope?

答案1

得分: 1

如果您阅读了 KeyFilterAttribute 上的文档,您会注意到您忘记将 .WithAttributeFiltering() 添加到 Service2 的注册中。属性过滤会影响性能,因此它是选择性的。

英文:

If you read the docs on the KeyFilterAttribute you'll notice you forgot to add .WithAttributeFiltering() to the Service2 registration. Attribute filtering is a perf hit so it's opt-in.

huangapple
  • 本文由 发表于 2023年6月12日 17:14:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定