Services and autofac disposing IDisposable

We’re using autofac along with service stack and are having issues where our “repositories” are not being disposed of at the end of a request. Services are created everytime a request comes in (from what I understand), and our repository is injected in the constructor of the service and is marked as IDisposable. Given it’s registered as a instanceperdependency in autofac I was under the assumption that it would be disposed at the end of the request, but it is not. Any ideas on how to fix this, as I would assume any dependency resolved when the service is created would be disposed?

If AutoFac is creating the dependency it’s up to AutoFac or its IContainerAdapter adapter to Dispose/Release it.

If your AutoFac IContainerAdapter implements IRelease it will get called back with the Service instance and every dependency registered in RequestContext.Instance.Items which is where ServiceStack registers its transient/request scoped dependencies which it disposes at the end of each request.

I set a break point in Resolve and Release, and the adapter is called for the Resolve, but never gets called for Release. By request dependencies I assume you mean both the Request argurment types and the types used to construct the service itself?

What I mean by this is say you have

public FooService : Service {
     public FooService(IRepository repository){

    public SomeResponse DoSomething(SomeRequest someRequest){

When FooService is constructed, is service stack keeping track of the constructor dependencies passed to the service in order to call DoSomething. I should note that core using autofac is working fine in this regard and so is our mvc 5 app, all are using the same autofac config.

No ServiceStack doesn’t track dependencies of other IOC’s, just dependencies resolved from it’s own IOC.

What do you mean it doesn’t get hit? it should at least be called with the Service instance if no items are registered in RequestContext.Instance.Items. If you’re overriding OnEndRequest() in your AppHost you will need to call the base method.

k so http request comes in, service stack needs a dependency for the service and we’ve set the container adapter to our own, so it calls that and says resolve IFooRepository. When the request ends is it not supposed to call release on the container adapter on everything it resolved from the other Container?. What i’m saying is you call resolve on the container adapter for the dependency required for the service, but never call release? How would the autofac container have any idea on the lifetime of the instance given it’s tied to the servicestack request?

Only the IOC knows the lifetime of dependencies that’s registered with it and needs to handle disposing of its own dependencies that should be disposed. Release gets called back with the Service instance, if you need a callback for the end of the request you can register a handler in OnEndRequestCallbacks or override OnEndRequest() or if you want to register dependencies to be disposed of at the end of the request you can call RequestContext.Instance.TrackDisposable() or you can be explicit and dispose dependencies in Service.Dispose() in your Service or a custom Service base class.

That’s the ticket, interesting case really sure autofac should dispose but it can’t it has no idea when the request ends. So in the autofac container adapter i just check if its IDisposable and call TrackDisposable.

But you don’t want to dispose Singleton dependencies.

Yep, i’ll have to deal with that somehow.