Use el registro con nombre en autofac con inyección de propiedad de atributo de filtro MVC

I'm using Autofac to fill in public properties of my filters, according to https://code.google.com/p/autofac/wiki/Mvc3Integration#Filter_Attribute_Property_Injection y funcionó muy bien.

Until I tried to use a named registration for one of the dependencies. I cannot find a way to do it. I tried to manually register my filters like so:

builder.RegisterType<MyCustomAttribute>()
       .WithProperty(ResolvedParameter.ForNamed<INamedDependency>("dependencyName"));

before calling the RegisterFilterProvider method, but that didn't work.

Any ideas? In case this has been fixed in a newer version, I'm using version 2.5.2.830.

Thanks, Kostas

preguntado el 05 de septiembre de 12 a las 10:09

1 Respuestas

May be you just forgot to register INamedDependency instance in your container:

public class MyCustomAttribute : FilterAttribute
{
    public IDependencyName DependencyName { get; set; }
}

public interface IDependencyName
{

}

public class DependencyName : IDependencyName
{

}

[Test]
public void ResolveCustomTest()
{
    // Arrange 
    var dependencyInstance = new DependencyName();
    var builder = new ContainerBuilder();
    builder.RegisterInstance(dependencyInstance).Named<IDependencyName>("dependencyName");
    builder.RegisterType<MyCustomAttribute>().WithProperty(ResolvedParameter.ForNamed<IDependencyName>("dependencyName"));
    builder.RegisterFilterProvider();
    var root = builder.Build();

    // Act
    var attr = root.BeginLifetimeScope("AutofacWebRequest").Resolve<MyCustomAttribute>();

    // Assert
    Assert.AreEqual(attr.DependencyName, dependencyInstance);
}

Respondido 16 ago 13, 01:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.