-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong constructor called #22
Comments
This library does nothing about the constructor selection. So, I could not understand why there is a difference here. However, I believe the constructor design of this sample is very tricky and badly designed in general. You should remove the first constructor. In addition, if you use dependency injection, it will be a problem to pass string[] to the second ctor. So, I think this is not a good and realistic sample to spend time to find the problem :) |
How constructor is selected by Windsor:
If you want the default constructor, you can do this: using Castle.Core;
public class MyService : IMyService
{
public MyService()
{
Console.WriteLine("Default constructor called!");
}
[DoNotSelect]
public MyService(params string[] foo)
{
Console.WriteLine("Custom constructor called!");
}
} |
I provided a simplified example. The service in my real world scenario is unfortunately from a separate library where I am not able to add [DoNotSelect] or even modify the library design. It does not matter how much you quote from the documentation; I provided a use case where it would not follow these rules across applications. I don't see any reason why Castle.Windsor in combination with I will likely have to find a different way to solve it, for example registering the component using a factory method, or not using dependency injection for this dependency. But I wanted to report this issue since I felt it was important for others to know that your application might break or provide non-deterministic behaviour when e.g. moving from ASP.NET to ASP.NET Core. Obviously I am now already a bit worried what other types of things it might resolve differently during the runtime of my application. |
Thank you @mbp |
Consider the following code
I register it as follows:
When I call
container.Resolve<IMyService>()
from a normal console application not involving castle-windsor-ms-adapter, the default constructor is called.When I call
container.Resolve<IMyService>()
from an ASP. NET Core project using this library (return WindsorRegistrationHelper.CreateServiceProvider(container, services);
inStartup.cs
, the second constructor is called.I would have expected the default constructor always be called, as by out-of-the-box Castle Windsor behaviour. Am I missing anything?
The text was updated successfully, but these errors were encountered: