On Tuesday, May 28, 2013 7:16:04 PM UTC+3, cortex wrote:
Be carefull with thread-safeness.
Autofac is scoped container but RSB do not handle scope. I think you should consider modify autofac bootstrapper to call dispose on IDisposable object in Release method. Normally IConsumer should be register as transient (InstancePerDependency). The only thing missing is a deterministic call to Dispose().
Right - at least there should be if (o is IDiposable) then o.Dispose(). In my sample all consumers resolved withing a lifetime scope would be released automatically.
I think you're trying to do some sort of Unit Of Work scope (resolving to the same instance multiple times during one message processing), that RSB do not handle by default. It was historically done through the help of a IMessageModule.
With the help of a ThreadStatic context variable set and unset on ITransport events, a custom Castle ILifestyleManager could rely on it to keeps track of message scoped instances.
Wrong - I'm not trying to resolve the same instance of a consumer. Consumers are still registered per dependency. What I'm trying to is automatic disposal of consumers' dependencies in a normal Ioc fashion. Resolving the same instance is just a side effect here.
Do you think this may also be done for Autofac without modifing RSB core code and introducing autofac specific handling of lifetime in the DefaultServiceBus Implementation ?
That's not autofac-specific actually. I assume any Ioc supports scopes and any of existing IServiceLocator implementations can be adjusted accordingly. Actually I think that 'per-message-scope' is more logical and suitable, just the same as 'per-request-scope' is now de facto standard of web apps approach.
It seems that it's not possible to change only service locator side. DefaultHandler.Resolve returns a resolved consumer object, not a scope.
Other solution is clearly client-side: If all dependencies are registered properly (something like 'to be resolved in scope of XConsumer') - probably that would work, but need much additional work on proper registration creation and maintenance.
Well - I didn't know about modules, thank you - will check, probably that way it's going to work.