Search code examples
c#wcfdependency-injectionmef

Trying to inject dependency into IIS hosted WCF service using MEF


I'm trying to inject specific class into my WCF service but it doesn't work and I can't understand why. I'm VERY NEW to MEF and patterns, just trying to make it work. Watched series of videos to understand what it is about but bootstraping won't work in my case since it is not Silverlight http://channel9.msdn.com/blogs/mtaulty/mef--silverlight-4-beta-part-1-introduction

Here is my Web application's Global.asax code. This is non MVC, just regular ASP.NET app:

private void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();

            var catalog = new WebScopedCatalog(new DirectoryCatalog(Server.MapPath("~\\bin"))); 
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }

FIRST, I'm not sure I even bootrstrap it properly.. SECOND, I'm using http://www.timjroberts.com/2011/02/web-scoped-mef-parts/ as a guidance for web-scoped parts. I need that because some injected objects supposed to live during request only.

Now, I have following class:

    [Export(typeof(ITest))]
    [WebPartCreationPolicy(WebCreationPolicy.Session)]
    public class Test : ITest
    {
        public string TestMe()
        {
            return "Hello!";
        }
    }

And my service looks like:

[ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MobileService
    {
        [Import]
        public ITest MyTestClass { get; set; }

        public MobileService()
        {
            int i = 10;
        }

When breakpoint hits at i=10 - I have NULL inside MyTestClass. Clearly, MEF does not initialize this class for me. Am I missing something?

EDIT:

When I examine catalog - I see my class Exported but I don't see it imported anywhere... enter image description here

EDIT 2: Daniel, Thank you. It makes sense. It still feels litle like a magic for me at this point. Yes, WCF creates this service. Than I have MembershipProvider and various Utility classes that ALL need the same import. And I'm not creating neither of those classes so I can't have Mef creating them. (Service created by WCF, MembershipProvider created by who-knows-what, Utility classes have static methods..) I wanted to use Mef instead of storing my stuff in HttpContext.Current. My container object doesn't have SatisfyImports. It has SatisfyImportsOnce. What if I do it in many different constructors? Will Mef share same singleton or it will be creating new instance every time?

At this point I wonder if I should even use Mef in this particular scenario..


Solution

  • MEF won't satisfy imports for objects it doesn't create. What is it that creates MobileService? If it's not MEF, then the import won't be satisfied by default. Even if it is MEF, the import wouldn't be satisfied in the constructor- you can't set properties on an object you create until it is done being created (ie the constructor has finished).

    You can call container.SatisfyImportsOnce(mobileService) to satisfy the imports of a part. You should try to avoid doing this everywhere you need a MEF import. If you can, you should export a part and import it from another part so that MEF handles the creation for you. However, it looks like this part may be created by WCF so you can't have MEF create it, and in that case it would be OK to use SatisfyImportsOnce.

    Response to EDIT2: If you use the same container each time, MEF will by default only create one instance of the part with the export. You can set the CreationPolicy to NonShared (in either the export or import attribute) to change this.

    As for whether it makes sense to use MEF the way you are trying to, I don't know enough about writing web services to give you any advice on that.