NinjectObjectBuilder.ConfigureProperty won't replace property binding when multiple configs are applied
So now this code
builder.ConfigureProperty(typeof(DuplicateClass), "SomeProperty", false);
builder.ConfigureProperty(typeof(DuplicateClass), "SomeProperty", true); // this should remove/override the previous property setting
Will result in SomeProperty being injected as true
I have created a profile for local development - in which (among other things) I want to disable first level retries.
I can get almost all the way there but not quite - because of the way the NinjectObjectBuilder configures properties by appending (rather than replacing) parameter bindings.
See below code for an example:
Logger.Info("Disabling First level retries");
// we can do this because the transactionsettings constructor defaults all property values to the relevant settings from SettingsHolder - so we're not overriding what might have been set elsewhere
var transactionSettings = new TransactionSettings {MaxRetries = 0};
// this should work but doesn't - because NServiceBus.Unicast.Transport.Config.Bootstrapper runs first and adds propertybindings. NinjectObjectBuilder.ConfigureProperty then appends instead of replaces propertybinding.
Configure.Instance.Configurer.ConfigureProperty<TransportReceiver>("TransactionSettings", transactionSettings);
As mentioned in the code comment the problem is that the NSB transport bootstrapper runs first - and this adds ninject propertybindings to the transport. When I come along within my custom profilehandler and want to configure the transactionsettings property for the transport, the impl of ConfigureProperty doesn't replace existing bindings but only appends my configuration. So there are now two parameter bindings for TransactionSettings. At runtime ninject resolves the TransportReceiver.TransactionSettings property value to the first configured binding - which is the one set by the bootstrapper and not my custom one.
from NServiceBus.ObjectBuilder.Ninject.NinjectObjectBuilder:
public void ConfigureProperty(Type component, string property, object value){
var bindings = kernel.GetBindings(component);
if (!bindings.Any()) {
throw new ArgumentException("Component not registered", "component");
}
foreach (var binding in bindings) {
binding.Parameters.Add(new PropertyValue(property, value));
}
}
I think the solution is going to be to remove any existing binding parameters for that propertyname before adding the newly configured one. Have added a new test to check and this issue affects only ninject and unity objectbuilders. The only potentially complicating factor I can think of would be where fixing this uncovers another bug where multiple property configurations are already being applied and only the first is actually being used. But this seems unlikely given the repro test fails only for two of the objectbuilders.
@andreasohlund I'm currently reviewing the pull request. As I see it this is the only reason why the behavior change is needed. But as I see it this sample code doesn't use any predefined why to set the TransactionsSettings. So how should we guarantee ordering? I'm using the ninject object builder for a long time now and have never come over the need to address these kind of changes. Shouldn't we improve the high level framework code so that such "hacks" aren't necessary and NSB infrastructure can rely upon a guaranteed order?
So how should we guarantee ordering?
Currently the implementation is "first in wins" - my PR just changes this to "last in wins" - which in my mind results in behaviour that is less unexpected. If I'm going to bother setting an ObjectBuilder.Property I expect it to override anything previously set by the framework.
Shouldn't we improve the high level framework code so that such "hacks" aren't necessary and NSB infrastructure can rely upon a guaranteed order?
yes, that would be great. :) All i actually want to do is disable FirstLevel error handling (this is for a "developer profile"). I'm having to jump through hoops (and trawling through nsb implementation code) just so i can control the transactionsettings... Good point, totally agree.
Currently the implementation is "first in wins" - my PR just changes this to "last in wins"
That makes it a breaking change right?
That makes it a breaking change right?
Yes, but. See comment in issue report:
The only potentially complicating factor I can think of would be where fixing this uncovers another bug where multiple property configurations are already being applied and only the first is actually being used. But this seems unlikely given the repro test fails only for two of the objectbuilders.
Given all bar two of the ObjectBuilder implementations actually passed the repro test before I applied the fix this indicates the implementations (across the different ObjectBuilders) were inconsistent. And so if the change did actually break something I'd expect an issue to have already been raised for one of the other ObjectBuilders which already implemented the fixed behaviour. Happy to concede I may be wrong but it seems the logic is correct.
I'd say we classify this as a bug an pull + highlight in the release notes?
Sent from my iPhone
On 24 okt 2013, at 23:44, danielmarbach [email protected] wrote:
@andreasohlund I'm currently reviewing the pull request. As I see it this is the only reason why the behavior change is needed. But as I see it this sample code doesn't use any predefined why to set the TransactionsSettings. So how should we guarantee ordering? I'm using the ninject object builder for a long time now and have never come over the need to address these kind of changes. Shouldn't we improve the high level framework code so that such "hacks" aren't necessary and NSB infrastructure can rely upon a guaranteed order?
— Reply to this email directly or view it on GitHub.
I'm against applying this change because of the following reason:
It is a breaking change and as long as the whole container story isn't properly addressed people are relying on the specific container adapters behavior to do all kind of nasty tricks to get their stuff injected into the core. We cannot possibly assume that this change will not break someones production code running on unity or ninject.
The primary reason for this pull van be addressed by properly designing configuration points into the core. This then allows us to address the container story for a next release
Am 25.10.2013 um 06:42 schrieb Andreas Öhlund [email protected]:
I'd say we classify this as a bug an pull + highlight in the release notes?
Sent from my iPhone
On 24 okt 2013, at 23:44, danielmarbach [email protected] wrote:
@andreasohlund I'm currently reviewing the pull request. As I see it this is the only reason why the behavior change is needed. But as I see it this sample code doesn't use any predefined why to set the TransactionsSettings. So how should we guarantee ordering? I'm using the ninject object builder for a long time now and have never come over the need to address these kind of changes. Shouldn't we improve the high level framework code so that such "hacks" aren't necessary and NSB infrastructure can rely upon a guaranteed order?
— Reply to this email directly or view it on GitHub. — Reply to this email directly or view it on GitHub.
Makes sense, lets pull this in 5.0.0
Sent from my iPhone
On 25 okt 2013, at 07:28, danielmarbach [email protected] wrote:
I'm against applying this change because of the following reason:
It is a breaking change and as long as the whole container story isn't properly addressed people are relying on the specific container adapters behavior to do all kind of nasty tricks to get their stuff injected into the core. We cannot possibly assume that this change will not break someones production code running on unity or ninject.
The primary reason for this pull van be addressed by properly designing configuration points into the core. This then allows us to address the container story for a next release
Am 25.10.2013 um 06:42 schrieb Andreas Öhlund [email protected]:
I'd say we classify this as a bug an pull + highlight in the release notes?
Sent from my iPhone
On 24 okt 2013, at 23:44, danielmarbach [email protected] wrote:
@andreasohlund I'm currently reviewing the pull request. As I see it this is the only reason why the behavior change is needed. But as I see it this sample code doesn't use any predefined why to set the TransactionsSettings. So how should we guarantee ordering? I'm using the ninject object builder for a long time now and have never come over the need to address these kind of changes. Shouldn't we improve the high level framework code so that such "hacks" aren't necessary and NSB infrastructure can rely upon a guaranteed order?
— Reply to this email directly or view it on GitHub. — Reply to this email directly or view it on GitHub. — Reply to this email directly or view it on GitHub.