The short answer: it's expected, and I don't think it's a problem.
A bit longer dive into it:
Autofac holds onto disposable objects until the lifetime scope is disposed, at which point anything disposable is disposed. (You can manually dispose of objects if you register them as Owned
, but in the case of WCF stuff I'd leave it.)
WCF channels are a bit of a notorious challenge because they're disposable, but the IDisposable
implementation will throw an exception if the channel is faulted (e.g., if you got an error response), which is actually very bad disposal design. Anyway, that's what UseWcfSafeRelease
handles - it makes sure your channels get properly disposed by correctly closing and/or aborting the channel based on its state. CloseChannel
is just the method that handles the proper cleanup.
If you're resolving a bunch of channels, those are all going to have to get cleaned up somehow or another. If you see that CloseChannel
getting called 22,000 times, it means you resolved 22,000 instances that all needed to be safely cleaned up.
I don't know that I'd change the lifetime scope. Another WCF problem is that if you fault the channel then you can't reuse (or reopen) the client proxy. You're done, and you need a new channel.
A better option to optimize your solution would be to resolve Func<IBookingManager>
in your constructor. Whenever you need a proxy, call the function to get a new one.
public class MyConsumer
{
private Func<IBookingManager> _factory;
public MyConsumer(Func<IBookingManager> factory)
{
this._factory = factory;
}
public void DoWork()
{
this._factory().CallOperation(input);
}
}
That provides two benefits:
- If you have methods on the class that don't need a channel resolved, you can save the creation of the channel (and the disposal).
- If you handle errors (e.g., timeout/retry), you can create new proxies when old ones are faulted and your class can be more fault tolerant.
Outside of that, I don't think I'd worry about trying to avoid that CloseChannel
call. If you need to optimize, optimize the number of channels you create.