带有ninject示例的WCF

Sha*_*ean 5 wcf ninject

首先,我从未见过使用ninject和wcf的例子.

这是我的.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="MyService.Services.NotifyService" %>
Run Code Online (Sandbox Code Playgroud)

我的服务:

[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
    }
}
Run Code Online (Sandbox Code Playgroud)

利用这些信息,我怎么依赖注入MyEmailRepositoryNotifyService

如果我没有默认构造函数,wcf会抛出一个错误请求一个.如果有帮助,我也有使用ninject和asp.net mvc3的经验.

Rem*_*oor 8

请参阅https://github.com/ninject/ninject.extensions.wcf/tree/master/src/Examples/WcfTimeService

  • 你能把最相关的代码带到这个答案吗?当链接失效时,答案变得毫无用处,当我按照该链接时,我必须深入了解您的存储库以了解我需要做什么.这个Q/A对列在[这里](http://meta.stackoverflow.com/a/334916/578411).这个问题并不值得,所以我宁愿挽救答案. (3认同)

Jef*_*eff 5

使用自定义IInstanceProvider来解析您的服务实例.这是一个例子:

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

  • 实际上,看起来NInject已经提供了上述类的预构建版本:http://stackoverflow.com/questions/3466886/using-ninject-wcf-extension-with-wcf-web-service (2认同)