Autofac中的装饰器和属性注入 - c#

我正在尝试为这些使用属性注入的服务注册装饰器。
当我添加containerBuilder.RegisterDecorator<ServiceDecorator, IService>()时,不再注入属性。
我想Autofac正在尝试将其注入到装饰器中,而不是原始服务中。

我已经写了一些测试来说明这个问题。有服务和装饰器:

public interface IService
{
    bool NestedServiceIsNotNull();
}

public interface INestedService { }

public class Service : IService
{
    public INestedService NestedService { get; set; }

    public bool NestedServiceIsNotNull()
    {
        return NestedService != null;
    }
}

public class NestedService : INestedService { }

public class ServiceDecorator : IService
{
    private readonly IService _original;

    public ServiceDecorator(IService original)
    {
        _original = original;
    }

    public bool NestedServiceIsNotNull()
    {
        return _original.NestedServiceIsNotNull();
    }
}

以及测试方法:

[TestMethod]
public void PropertyInjectedServiceShouldNotBeNull()
{
    var builder = new ContainerBuilder();
    builder.RegisterType<NestedService>().As<INestedService>();
    builder.RegisterType<Service>().As<IService>().PropertiesAutowired();
    var container = builder.Build();
    var service = container.Resolve<IService>();

    Assert.IsTrue(service.NestedServiceIsNotNull());
}

[TestMethod]
public void PropertyInjectedServiceShouldNotBeNullEvenIfDecoratorRegistered()
{
    var builder = new ContainerBuilder();
    builder.RegisterType<NestedService>().As<INestedService>();
    builder.RegisterType<Service>().As<IService>().PropertiesAutowired();
    // Here's the difference - decorating the service
    // causes the assertion to fail.
    builder.RegisterDecorator<ServiceDecorator, IService>();
    var container = builder.Build();
    var service = container.Resolve<IService>();

    Assert.IsTrue(service.NestedServiceIsNotNull());
}

第一个测试通过,但是第二个通过断言失败。

这是正确的行为吗?
我正在处理一个旧项目,所以我不应该通过将依赖项移至构造函数来更改现有代码。
有什么办法解决这个问题?

参考方案

似乎...您发现了一个错误! ow! I've filed an issue on your behalf here.

所有这些都不会丢失,但是-您仍然可以按需要使用装饰器,只需使用较旧的不太漂亮的Autofac装饰器语法即可完成。

var builder = new ContainerBuilder();
builder.RegisterType<NestedService>().As<INestedService>();

// Decorating the service with the old syntax works.
builder.RegisterType<Service>().Named<IService>("service").PropertiesAutowired();
builder.RegisterDecorator<IService>((c, inner) => new ServiceDecorator(inner), fromKey: "service");

var container = builder.Build();
var service = container.Resolve<IService>();

Assert.True(service.NestedServiceIsNotNull());

There is more documentation on how to work with this older syntax here.

将谓词<T>转换为Func <T,bool> - c#

我有一个包含成员Predicate的类,希望在Linq表达式中使用该类:using System.Linq; class MyClass { public bool DoAllHaveSomeProperty() { return m_instrumentList.All(m_filterExpression); } private IEnumerable&…

根据激活的Maven配置文件更新战争名称 - java

在pom中,我有两个配置文件。测试1测试2现在,我希望根据激活的配置文件更改战争名称。预期结果激活test1配置文件后,战争名称应为prefix-test1.war。激活test1和test2时,战争名称应为prefix-test1-test2.war。如果没有激活任何配置文件,则战争名称应为prefix.war。我的POM文件....<?xml ve…

将对象转换为List <object> - c#

我看过类似的问题,但没有什么合适的。我有一个碰巧包含列表的对象。我想把它变成我可以列举的东西。例如:object listObject; // contains a List<Something> List<object> list; list = listObject as List<object>; // list c…

合并List <T>和List <Optional <T >> - java

鉴于: List<Integer> integers = new ArrayList<>(Arrays.asList( 10, 12 )); List<Optional<Integer>> optionalIntegers = Arrays.asList( Optional.of(5), Optional.em…

无法从ArrayList <String>转换为List <Comparable> - java

当我写下面的代码时,编译器说 无法从ArrayList<String>转换为List<Comparable>private List<Comparable> get(){ return new ArrayList<String>(); } 但是当我用通配符编写返回类型时,代码会编译。private List&l…