Castle Windsor: How to release instance with PerThread lifestyle
<p>i'm using Castle Windsor with PerThread lifestyle in a windows service.</p>

<p>When a new thread worker is started, a new instance is created by windsor, but never disposed and stays in memory for the entire life-cycle of our win-service.</p>

<p>I've seen in the Windsor documentation that:</p>

<blockquote>
  <p>Releasing the component explicitly <strong>does</strong> <strong>nothing</strong>. Instances will be
  released when the <strong>container</strong> they're registered with is <strong>disposed</strong></p>
</blockquote>

<p>But in our case - i need to release a PerThread instance at the end of the thread work. Therefore we cannot dispose the entire container - which will dispose all the needed instances of other threads or any resolved instances at all. 
<br>
A solution which will require disposing a child containers etc - seems to be too complex and not really effective for our per-thread need.
<br><br>For now we <strong>really concern</strong> about the amount of instances that will be hold in the IOC container till the windows service will be stopped.</p>

<p><strong>Questions:</strong>
<br>
1. Is there a way to achieve this action - to release the specific per-thread instance from the IOC container in order that it will be disposed by the GC?
<br>
<br>
2. Is there a preferred way to solve this issue by a custom lifestyle or something similar to this?
<br>
<br>
3. I've read about a way that may be right for our case - using "<strong>Scoped</strong>" lifestyle and calling "<code>Container.BeginScope()</code>" at the beginning of the thread worker, and dispose the scope at the end of the thread's work.
<br> But i'm not familiar with this lifestyle and if its thread-safe and won't cause any problems using this option in a thread context.</p>

<p>TNX.</p>

<p><strong>Update 1:</strong>
Here's a basic example which reflects our case flow.</p>

<pre><code> // This is a thread worker logic.
// We need that IConfig will be the same instance for the entire thread's context.
public void ThreadWorker(object manager)
{
    // Init config object.
    var config = Container.Resolve&lt;IConfig&gt;();
    // Updating states in config
    config.init(manager);

    // working.
    doSomeLogics(manager.services);
}

private void doSomeLogics(List&lt;IService&gt; services) 
{
    foreach (Iservice curService in services) 
    {
        curService.Execute();
    }
}
</code></pre>

<p><br></p>

<pre><code>// Each IService implementor comes from external dlls and cannot be changed since its decoupled from our main app.

public class Service1 : IService 
{
    public void Execute()
    {
        // Use the IConfig instance which need to be the same instance per thread.
        IConfig curConfig = Container.Resolve&lt;IConfig&gt;();

        // using the curConfig properties to do a specific work...
    }
}

public class Service2 : IService 
{
    public void Execute()
    {
        // Use the IConfig instance which need to be the same instance per thread.
        IConfig curConfig = Container.Resolve&lt;IConfig&gt;();

        // using the curConfig properties to do a specific work...
    }
}
</code></pre>