将DependencyResolver与Ninject一起使用后,HttpContext为NULL

时间:2015-07-03 20:46:15

标签: asp.net asp.net-mvc ninject asp.net-identity

我实现了自定义IIdentityMessageService类。我的实现只是将消息放入数据库。我使用Dependency解析器来创建存储库类的实例。

public class QueueSmsService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        var smsRepository = DependencyResolver.Current.GetService<ISmsRepository>();

        Sms smsMessage = new Sms()
        {
            Priority = (int)MessagePriority.High,
            NumberTo = message.Destination,
            Body = message.Body
        };

        return smsRepository.InsertAndSubmitAsync(smsMessage);
    }
}

不幸的是,HttpContext存在空问题。也许如果我在contructor中注入存储库会解决问题?我可以以某种方式在构造函数中注入存储库吗?也许有另一种解决方案来消除HttpContect == null问题?现在,QueueSmsService的创建如下所示:

var manager = new AppUserManager(new AppUserStore(context.Get<DataContext>()));

manager.RegisterTwoFactorProvider("SMS", new PhoneNumberTokenProvider<User, int>
{
    MessageFormat = "Kod autentykujący: {0}"
});

manager.EmailService = new QueueEmailService();
manager.SmsService = new QueueSmsService();

HttpContext用于存储库类

public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
{
    private IDataContext dataContext;
    private readonly IHttpContextFactory httpContextFactory;
    protected static Logger logger = LogManager.GetCurrentClassLogger();

    public RepositoryBase(IDataContext dataContext, IHttpContextFactory httpContext)
    {
        if (httpContext == null) throw new ArgumentNullException("httpContextFactory");
        this.httpContextFactory = httpContext;

        this.dataContext = dataContext;
    }
(...)

DataContxtFactory只是

public class HttpContextFactory : IHttpContextFactory
{
    public HttpContextBase Create()
    {
        return new HttpContextWrapper(HttpContext.Current);
    }
}

错误报告完全在上面的代码中。

0 个答案:

没有答案