WCF with ninject example
Asked Answered
T

3

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.

Tilney answered 12/7, 2011 at 19:32 Comment(0)
B
8

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

Bothnia answered 12/7, 2011 at 20:7 Comment(2)
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. The question is not close-worthy so I rather salvage the answer.Limoli
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.Bellbella
L
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

Leal answered 12/7, 2011 at 19:35 Comment(3)
Theres no easier way to do this like in asp.net mvc?Tilney
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.Leal
In fact, it looks like NInject already offers a prebuilt version of the above classes: #3467386Leal
L
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" %>
Literate answered 10/11, 2015 at 3:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.