WCF, Unity: One-Time Teardown Instances

we created a WCF service that uses the Unity container to resolve instances to manage Exchange Server Powershell commands. We have defined the IExchangePowershell interface, which has a specific implementation that implements IDisposable. After some time, we encountered a problem that we could not run powershell commands anymore, as the server said that too many powershell sessions were already open. Looks like we never got rid of our powershell instances. The Powershell-specific Dispose () method will take care of closing the workspace and session. As soon as I call this in the repository methods, we no longer get errors.

((IDisposable)this.powershell).Dispose(); 

Now, of course, I don’t want to explicitly call dispose on every repository method. I thought unity could take care of that. Our WCF instance provider does this:

 public void ReleaseInstance(InstanceContext instanceContext, object instance) { container.Teardown(instance); } 

But it really doesn’t get rid of IExchangePowershell instances. Do you know how I can automatically dispose of these instances?

+6
source share
2 answers

This is a really well-known issue in Unity. TearDown method does nothing . If you want to use TearDown , you must create your own container extension.

I wrote an article about the use of object lifecycle managers in Unity and their effect on deletion. If you use TransientLifetimeManager or PerResolveLifetimeManager by default, Unity does not even track the existence of your objects, so it cannot call Dispose . The only lifelong managers that Dispose calls on authorized instances are ContainerControlledLifetimeManager (aka singleton) and HierarchicalLifetimeManager . Dispose is called when the lifetime manager is located.

The solution for you either uses casting and the Dispose handle manually, as you have already done, or switch to the HiearchicalLifetimeManager and create a new subcontainer for each incoming WCF request. Each subcontainer will process only one request, and it will have the allowed objects with the highest lifetime.

There are other ways, for example , this article builds very complex code around Unity to support recycling and TearDown for all allowed objects.

+10
source

The answer depends on how you register your type / instance with unity. The standard implementation of Teardown does nothing.

If you register a type, Unity does not store a reference to the instance that it creates - you decide how to manage and manage its lifetime. If you register an instance, then the lifetime of the instance is controlled by a single one and remains until you destroy the container.

The following link helps you understand a little better about lifelong management: http://msdn.microsoft.com/en-us/library/ff648098.aspx

You need to ask yourself when you want your objects to be deleted. If you know when to call ReleaseInstance, you can also call IDispose instead.

(Sorry, I'm not familiar with WCF, so I'm not sure which instance is provided in this context)

0
source

Source: https://habr.com/ru/post/886298/


All Articles