Being the author of LightInject I will try to answer the best I can.
First of all, LightInject should never throw a StackOverflowException, so that is something that needs to be looked into based on what you describe here.
And I clearly understand that property injection was not your intention here as it would make little sense to inject the service twice (Constructor and Property).
There are actually several things we can do to avoid the dependency to be injected into the property.
- Make the property read-only by removing the public setter.
Register the service by using what the documentation refers to as "explicit" service registration.
container.Register<ISomeService>(f => new SomeService(f.GetInstance<IAnotherService>()));
This will cause the public properties to be ignored because we have now been explicit about how to resolve the dependencies of the SomeService class.
Make use of LightInject.Annotation
This will make sure that only properties marked with the InjectAttribute will have its dependencies injected. In your specific case this would mean to just leave the property as is without the attribute. At the composition root, we can enable this by making this simple configuration.
container.EnableAnnotatedPropertyInjection();
I will as said before investigate further into the StackOverflowException and make proper adjustments for this case. My first thought would be to throw an exception if the container finds a dependency that is eligible for injection both as a constructor dependency and a property dependency.
Hope this helps.
Best regards
Bernhard Richter
EDIT
Although the problem seem to be solved for your case, I would still like to be able to reproduce this in a test. The reason that you get an exception is probably not as simple as the service having both a constructor dependency and a property dependency of the same type.
Consider this service
public class FooWithConstructorAndPropertyDependency : IFoo
{
public FooWithConstructorAndPropertyDependency(IBar bar)
{
Bar = bar;
}
public IBar Bar { get; set; }
}
LightInject will happily inject the dependency.
container.Register<IBar, Bar>();
container.Register<IFoo, FooWithConstructorAndPropertyDependency>();
container.GetInstance<IFoo>();
So there must be something else that is causing the exception. I would appreciate if you could come up with the smallest example that reproduces the exception so that this can be dealt with. It is a little hard for me do further investigations without something that fails:) If you choose to make this effort you can post it here or mail me at bernhard.richter@gmail.com.