title: autofac and multithreading
body: <p>when doing parallel/multithreading, if there are dependencies that are not thread safe, what kind of instance method can be used with autofac to get an instance per thread? from what I know, autofac is the kind of DI container for certain framework like asp.net/mvc but for the rest of the app type like windows service, it does not have any support. in my scenario, i am doing multithreading for a windows service that also hosting a web api service. what kind of registration can be used so that it will work for web api instanceperhttprequest and instanceperlifetimescope. two separate container? </p>
  
  <p><strong>EDIt:</strong></p>
  
  <p>using this parallel extension method here:</p>
  
  <pre><code>public static Task ForEachAsync&lt;T&gt;(this IEnumerable&lt;T&gt; source, int dop, Func&lt;T, Task&gt; body)
      {
          return Task.WhenAll(
              from partition in Partitioner.Create(source).GetPartitions(dop)
              select Task.Run(async delegate
              {
                  using (partition)
                  {
                      while (partition.MoveNext())
                      {
                          await body(partition.Current);
                      }
                  }
              }));
      }
  </code></pre>
  
  <p>so the body will be use to do the work. DI will need to be inside of the body func. </p>