5

First of all, I have never seen an example of using ninject with wcf.

This is my .svc:

<%@ ServiceHost Language="C#" Debug="true" Service="MyService.Services.NotifyService" %>

My Service:

[ServiceContract]
public interface INotifyService
{
    [OperationContract]
    void SendEmail(string to, string from, string message);
}

class NotifyService : INotifyService
{
    private IEmailRepository emailRepo;

    public NotifyService(IEmailRepository emailRepo)
    {
        if (emailRepo== null) throw new ArgumentNullException("emailRepo");
        this.emailRepo= emailRepo;
    }
    public void SendEmail(string to, string from, string message)
    {
        //do stuff here
    }
}

Using this information, how do I dependency inject MyEmailRepository in NotifyService?

If I do not have a default constructor, wcf throws an error asking for one. I also have experience using ninject with asp.net mvc3 if that helps.

Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406

3 Answers3

8

See https://github.com/ninject/ninject.extensions.wcf/tree/master/src/Examples/WcfTimeService

M Afifi
  • 4,645
  • 2
  • 28
  • 48
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • 3
    Can you bring the most relevant code to this answer. When the link goes dead the answer becomes useless and when I follow that link I have to dig through your repository to understand what I need to do. This Q/A pair is listed [here](http://meta.stackoverflow.com/a/334916/578411). The question is not close-worthy so I rather salvage the answer. – rene Oct 12 '16 at 09:20
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Mistalis Mar 07 '17 at 10:09
5

Use a custom IInstanceProvider to resolve your service instance. Here is an example:

http://orand.blogspot.com/2006/10/wcf-service-dependency-injection.html

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Jeff
  • 35,755
  • 15
  • 108
  • 220
  • Theres no easier way to do this like in asp.net mvc? – Shawn Mclean Jul 12 '11 at 19:43
  • Nope :(...ASP .NET MVC does make this very easy which is kind of nice. It's not that much work though. 3 classes: InstanceProvider (to create instance using your IoC container), InstanceProviderServiceBehavior (to apply the InstanceProvider), and ServiceHostFactory (to apply the service behavior). Then you change your .svc file to reference your custom ServiceHostFactory. – Jeff Jul 12 '11 at 19:47
  • 2
    In fact, it looks like NInject already offers a prebuilt version of the above classes: http://stackoverflow.com/questions/3466886/using-ninject-wcf-extension-with-wcf-web-service – Jeff Jul 12 '11 at 19:48
1

This answer at SO provides a full implementation to add NInject to a WCF project.

I won't copy and paste it here, but basically, after installing the Ninject, Ninject.Extensions.Wcf and Ninject.Web.Common extensions through Nuget, you'll have to create three classes:

public class NInjectInstanceProvider : IInstanceProvider, IContractBehavior
public class NInjectServiceHostFactory : ServiceHostFactory
public class NInjectServiceHost : ServiceHost

Then point the Factory attribute in your .svc (right click the file on Solution Explorer, then choose "View Markup") to the NInjectServiceHost class:

<%@ ServiceHost ... Factory="SomeNamespace.NInjectServiceHostFactory" %>
Community
  • 1
  • 1
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62