views:

154

answers:

2

I've created a multi-threaded service that uses Castle Windsor to create components to run on separate threads. I Resolve an component by name with parameters for each thread.

I'm running into concurrency problems with a 3rd party library used by the components. I suspect that isolating those components in separate AppDomains will resolve the problem.

Is there a way to have Resolve create the component using a different AppDomain?

private ActivityThread NewActivityThread(ActivityInstance activityInstance)
{
 // Set up the creation arguments.
 System.Collections.IDictionary arguments = new Dictionary<string, string>();
 activityInstance.Parameters.ForEach(p => arguments.Add(p.Name, p.Value));

 // Get the activity handler from the container.
 IActivity activity = Program.Container.Resolve<IActivity>(activityInstance.Name, arguments);

 // Create a thread for the activity.
 ActivityThread thread = new ActivityThread(activity, activityInstance, _nextActivityID++);
 return thread;
}

public ActivityThread(IActivity activity, ActivityInstance instance, int id)
{
    _activity = activity;
 _instance = instance;
 _id = id;
}

public void Start()
{
 if (_thread == null)
 {
  // Create a new thread to run this activity.
  _thread = new Thread(delegate() { _activity.Run(); });
  _thread.Name = _activity.ToString();
  _thread.SetApartmentState(ApartmentState.STA);
  _thread.Start();
 }
}
A: 

If you've already created a separate AppDomain, can't you just create a new instance of a Windsor container within the private AppDomain?

David Montgomery

related questions