NServiceBus.ObjectBuilder.Ninject critical bug - ninject

I'm experiencing this error mentioned on the nsb group when running in the NServiceBus.Production profile.
Just wondering if it's a known issue (Remo ? :-) ) because it's currently going to prevent me using ninject with nsb.
NServiceBus.Ninject-CI.3.0.2044 nuget.
Many thanks!
2012-02-28 15:24:18,204 [Worker.8] WARN NServiceBus.Unicast.Transport.Transacti
onal.TransactionalTransport [(null)] <(null)> - Failed raising 'transport messag
e received' event for message with ID=02e57b0c-f345-46ae-a095-f52a4cfc0cfb\2071
System.NullReferenceException: Object reference not set to an instance of an obj
ect.
at Ninject.Injection.DynamicMethodInjectorFactory.EmitMethodCall(ILGenerator
il, MethodInfo method) in c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject
\Injection\DynamicMethodInjectorFactory.cs:line 130

I didn't write the NSB ObjectBuilder for Ninject. From the exception I can tell that Ninject fails to inject a property because there is no public setter. With the an unchanged Ninject this cannot happen because these properties are filtered before unless Ninject is explicitly told that it shall not filter none public members.
I had a short look at the NinjectObjectBuilder. It adds new heuristic and it seems that it does not filter public properties with private setters. This implementation requires some change.
But I think this won't fix your problem since you want to have that property injected anyway. The proper way to do so is to change the setter to public. Alternatively, you can change the settings to allow privat member injection. Settings.InjectNonPublic = true; But I don't recommend to do that.

Related

In ServiceMatrix, is it possible to 'ConvertToSaga' an endpoint which publishes/subscribes event messages rather than command messages?

I've got a solution with 4 NSB host endpoints which were created using ServiceMatrix and I want to use them within a saga.
I know how to Convert To Saga from a command and that works fine in a test project. What I don't understand is how I can do the same thing with published events, as the menu option is not available. Is it possible to do this using ServiceMatrix ?
I have tried to implement the changes by hand by following this tutorial and observing the changes that were made to my test project when I Converted To Saga.
It builds ok but throws an error within the framework when running:
System.InvalidOperationException was unhandled
Message: An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Additional information: No IBus instance available, please configure one and also verify that you're not defining your own Bus property in your saga since that hides the one in the base class
I'm using VS2013 and ServiceBus 5.
UPDATE:
I commented out the inherited IHandleMessages<> interface and public IBus Bus property from the generated code and it runs without this error. I'm not happy doing this, as the next design change using the tool will overwrite these fixes.
In your command handler you usually have a property of type IBus that is injected to the handler class either by property injection or constructor injection.
When you move from a command handler to a Saga, you need to remove your IBus property from the handler. Saga base class has a Bus property that you should use instead. This property is populated for you automatically, you should not care about this. This is exactly what this exception is telling you.

Injecting an object InRequestScope into a ValidationAttribute via Ninject throws error

Ninject now enables property injection into ValidationAttribute (https://github.com/ninject/ninject.web.mvc/wiki/Injection-of-validators). It works great, UNTIL you use bind the object InRequestScope().
So, here is what I think is happening. ValidationAttribute are implemented as singletons in ASP.NET.
The first time an attribute is instantiated, Ninject properly populates the injected property.
The object that was injected is disposed when the current request ends
Upon the next request, accessing the injected property throws an error because it has been disposed.
Ninject does not try to populate the injected property because the validation attribute is never re-instantiated.
Does this make sense? Does anyone have any idea for a work around?
That is correct.
If you really need a dependency in request scope then you have to inject a factory for the dependency instead of the dependency itself and use it to get a new instance during evaluation.

Single NServiceBus message handled by Saga two times

I have a NServiceBus version 3.3.8 Saga that I am working on migrating from NServiceBus 2.6. After I upgraded it, I have found that a single message in the queue is getting processed twice by the saga. What is most strange about it is that NServiceBus is treating this as two separate Transport Messages because my IMessageModule implementation is getting called twice as well.
The message it is receiving is a message bound to NServiceBus 2.6. Has anyone seen this before? I at first thought it was a dependency injection issue, as we are switching from Unity to Autofac, but that doesn't seem to explain the issue with the MessageModule getting called twice.
I appreciate any help you can provide.
UPDATE
It turns out that the issue was a dependency injection issue that I had inadvertently created. The saga has some additional dependencies that require IBus and the IBus is a property and set via property injection. Using an Autofac module, I tried to simplify the configuration by overriding the AttachToComponentRegistration and injecting properties on all registered items like this:
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) {
registration.Activating += (s, e) => e.Context.InjectProperties(e.Instance);
}
This, however, caused the issue above. To fix it, I removed this override and manually set up property injection via a call to:
builder.RegisterType<Implementation>().As<Interface>().PropertiesAutowired();
This resolved my duplicate processing issue. I am still not sure why it caused the issue, but removing that override did prevent the duplicate message handling.
It turns out that the issue was a dependency injection issue that I had inadvertently created. The saga has some additional dependencies that require IBus and the IBus is a property and set via property injection. Using an Autofac module, I tried to simplify the configuration by overriding the AttachToComponentRegistration and injecting properties on all registered items like this:
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) {
registration.Activating += (s, e) => e.Context.InjectProperties(e.Instance);
}
This, however, caused the issue above. To fix it, I removed this override and manually set up property injection via a call to:
builder.RegisterType<Implementation>().As<Interface>().PropertiesAutowired();
This resolved my duplicate processing issue. I am still not sure why it caused the issue, but removing that override did prevent the duplicate message handling.

WCF, Unity: Teardown disposable instances

we've set up a WCF service which uses a Unity Container to resolve instances for managing Exchange 2010 Powershell commands. We defined an IExchangePowershell interface which has a concrete implementation that implements IDisposable. After some time we encountered the problem that we couldnt execute powershell commands anymore since the server said there are already too many powershell sessions open. It seems that we never really disposed of our powershell instances. The Dispose() method of the concrete Powershell would take care of closing the runspace and the session. Once I call this in the repository methods, we don't get the errors anymore.
((IDisposable)this.powershell).Dispose();
Now of course I dont want to explicitly call dispose in every repository method. I thought unity could take care of this. Our WCF Instance Provider does this:
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
container.Teardown(instance);
}
But that doesnt really dispose of the IExchangePowershell instances. Do you have any idea how I could automatically dispose of those instances?
That is actually well known problem in Unity. TearDown method does nothing. If you want to use TearDown you must create custom container extension.
I wrote an article about using object lifetimes managers in Unity and their impact on disposing. If you use default TransientLifetimeManager or PerResolveLifetimeManager the Unity will even don't track existence of your objects so it can't call Dispose. The only lifetime managers which calls Dispose on resolved instances are ContainerControlledLifetimeManager (aka singleton) and HierarchicalLifetimeManager. The Dispose is called when the lifetime manager is disposed.
The solution for you is either use casting and handle Dispose manually as you already did or switch to HiearchicalLifetimeManager and create new subcontainer for each incoming WCF request. Each subcontainer will only handle single request and it will dispose resolved entities with hiearchical lifetime.
There are other ways, for example this article builds a very complex code around Unity to support disposing and TearDown for all resolved objects.
The answer depends on how you register your type / instance with unity. Standard implementation of Teardown does nothing at all.
If you register type then Unity does not store reference to instance it creates - it's up to you to manage it's lifetime and dispose it. If you register instance, then the instance lifetime is managed by unity and is kept until you dispose of the container.
The link below helps to understand a bit better about lifetime management:
http://msdn.microsoft.com/en-us/library/ff648098.aspx
You need to ask yourself when you would like your objects to be disposed. If you know when to call ReleaseInstance, you might as well call IDispose instead of it.
(I'm sorry, I'm not familiar with WCF, so I'm not sure what instance provide is in this context)

Problem with spring + nhibernate in .net environment

I am working with Spring.net 1.3 and nHibernate for .net 1.3.
While fetching the application context using XML ( _appContext = new XmlApplicationContext("abc.xml")) I am getting the exception as
"Error creating object with name 'NHibernateSessionFactory' defined in abc.xml"
Initialization of object failed : The following types may not be used as proxies"
I have declared all the properties of class as public virtual.still why am i getting this exception
Thanks in advance
You must have missed marking a property or method virtual. The exception (or an InnerException, I'm not familiar with Spring) message should tell you exactly which type is the problem. For example, I just reproduced this:
{"The following types may not be used as proxies:\nModel.Project: method get_ProjectId should be 'public/protected virtual' or 'protected internal virtual'"}