Print Page | Close Window

Bootstrapping WCF

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4254
Printed Date: 03-May-2024 at 3:52pm


Topic: Bootstrapping WCF
Posted By: amarpai
Subject: Bootstrapping WCF
Date Posted: 29-Jul-2013 at 3:51pm
We have an existing WCF service and would like to get the Cocktail/DevForce injection to work. The code is as follows:

[Export(typeof(IMyWcfService))]
public class MyWcfService : IMyWcfService
{
   [Import(typeof(IMyUnitOfWork))]
   private
IMyUnitOfWork MyUnitOfWork;

   public Customer ProcessRequest(int id)
   {
      Customer customer = await MyUnitOfWork.CustomerRepository.FindCustomer(id);
      return customer;
   }
}

The MyUnitOfWork field is not getting resolved. This is because Cocktail is not bootstrapped. I have seen examples of using Cocktail with WPF and I am able to get that to work. But the WCF part is not working.

Has anyone done this before?

Thanks!



Replies:
Posted By: mgood
Date Posted: 29-Jul-2013 at 4:28pm
First and foremost, don't use Cocktail on the server as-is. Many parts of Cocktail are not threadsafe and that includes the single shared MEF container. Cocktail is a client framework written with a single UI thread in mind. A server is a different animal all together starting with the fact that it is multi-threaded by design. 

The above won't work, because the WCF service is not created by MEF. You could call Composition.BuildUp(this) in the constructor to satisfy the imports, but again, don't do this on the server with Cocktial as-is, unless you want to create yourself a headache down the road.

If you only want to use the Compositon part of Cocktail on the server, the way to do that is to implement a threadsafe version of ICompositionProvider that sets up the MEF container per thread instead of a globally shared container. You can then call Compositon.SetProvider to configure Cocktail to use your threadsafe composition provider.



Print Page | Close Window