How do I make Castle Windsor call the aimless constructor?

I currently have a class that looks like this:

public class MyClass : IMyClass
{
   public MyClass()
   {
      //...
   }
   public MyClass(IMyRepository repository)
   {
      //...
   }
}

      

In my config file, I have the IMyClass registered, but not the IMyRepository. I want Windsor to use a constructor that takes no parameters, but I get this message:

The component "MyClass" cannot be created because it has dependencies that must be satisfied. MyClass waits for the following Dependency:

Services: - Namespace.IMyRepository, which was not registered.

I found another post that says the container will call the constructor with the largest argument it can satisfy. So why is he trying to call the constructor with an argument that he doesn't know how to satisfy?

0


source to share


1 answer


You may be using an older version of Windsor ... this works fine for me:



[TestFixture]
public class WindsorTests {

    public interface ISomeInterface {}

    public class AService {
        public int Id { get; private set; }

        public AService() {
            Id = 1;
        }

        public AService(ISomeInterface s) {
            Id = 2;
        }
    }

    [Test]
    public void Parameters() {
        var container = new WindsorContainer();
        container.AddComponent<AService>();
        var service = container.Resolve<AService>();
        Assert.AreEqual(1, service.Id);
    }
}

      

+1


source







All Articles