0

Possible Duplicate:
Injecting data to a WCF service

I have a WCF service that looks like this: (Service-contract attributes omitted for clarity)

public interface IMyService
{
    void LoadBulkData(LoadRequest request);
}

public class MyService : IMyService
{
    IUnityContainer unity = new UnityContainer();
    IDataProvider provider;

    public MyService()
    {
        unity.LoadConfiguration();
        provider = unity.Resolve<IDataProvider>();
    }

    public void LoadBulkData(LoadRequest request)
    {
        // ...
    }
}

As per company policy, Microsoft Unity must be used for DI/IoC. Because this is WCF, I can't use constructor injection because the service implementation must use the default, parameterless constructor.

In particular, I don't like using Unity to resolve the dependency in the constructor. Does anyone know of a way around this? Also, is there a pattern out there that might better suit my needs here?

Community
  • 1
  • 1
Didaxis
  • 8,486
  • 7
  • 52
  • 89

4 Answers4

3

Why not use Property Injection:

Annotating Objects for Property (Setter) Injection

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • This is probably what I'll go with for the time being. Thank you – Didaxis Dec 20 '11 at 15:14
  • One question after reading that article: Where would the "run-time" code be? The constructor of the service? – Didaxis Dec 20 '11 at 15:30
  • @ErOx - I would take a look at this article about WCF Extensibility and figure out the best place to place it: http://msdn.microsoft.com/en-us/magazine/cc163302.aspx – Justin Niessner Dec 20 '11 at 15:40
  • 2
    Property Injection implies something completely different about the class' invariants than Constructor Injection, so I don't find this a good solution - particular when (contrary to what the OP thinks) it's perfectly possible to support Constructor Injection in WCF. – Mark Seemann Dec 20 '11 at 16:38
  • @Mark -I meant that vanilla WCF doesn't support constructors w/parameters out-of-the-box. For these types of constructors, chris.house.00's answer or one of the many others available online would be the way to go. – Didaxis Dec 20 '11 at 20:08
2

Shameless plug for my own blog :)

http://thirteendaysaweek.com/2010/12/01/dependency-injection-and-wcf-services/

It's eactually pretty easy to do constructor injection with WCF and Unity. You'll end up needing a custom ServiceHost, ServiceHostFactory, an IInstanceProvider implementation that interacts with your container and an IServiceBehavior implementation. All in all, it's not a lot of code and fairly easy to understand.

chris.house.00
  • 3,273
  • 1
  • 27
  • 36
  • Actually, your blog post is really nice, thank you for that. Probably not what I'll go with this time around, time constraints and all, but I plan on coming back to this in the future. Again, thank you – Didaxis Dec 20 '11 at 15:13
0

You can use Ninject and the WCF Ninject Extension. http://ninject.org/

Massimo Zerbini
  • 3,125
  • 22
  • 22
  • Sorry, I can't use Ninject as per company policy. I've used Ninject.Extensions.Wcf elsewhere, and it works brilliantly. In a more perfect world maybe... – Didaxis Dec 20 '11 at 15:03
0

This blogpost describes how you can implement a custom WCF IInstanceProvider

Tim Cools
  • 1,162
  • 10
  • 19