首先,我从未见过使用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)
利用这些信息,我怎么依赖注入MyEmailRepository的NotifyService?
如果我没有默认构造函数,wcf会抛出一个错误请求一个.如果有帮助,我也有使用ninject和asp.net mvc3的经验.
请参阅https://github.com/ninject/ninject.extensions.wcf/tree/master/src/Examples/WcfTimeService
使用自定义IInstanceProvider来解析您的服务实例.这是一个例子:
http://orand.blogspot.com/2006/10/wcf-service-dependency-injection.html
| 归档时间: |
|
| 查看次数: |
11144 次 |
| 最近记录: |