0

I implemented custom IIdentityMessageService class. My implementation simply put messages into database. I use Dependency resolver to create instance of repository class.

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);
    }
}

Unfortunately there is a problem with HttpContext being null. Maybe if I inject repository in contructor would solve the problem? Can I somehow inject repository in contructor? Maybe there is another solution to eliminate the HttpContect == null problem? Right now QueueSmsService is created like below in:

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 is used in repository class

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;
    }
(...)

Where the DataContxtFactory is simply

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

And the error is reported exactly in the code above.

jmazur
  • 125
  • 1
  • 4
  • 12
  • You should have a look at this and follow the answer: http://stackoverflow.com/questions/18383923/why-is-httpcontext-current-null-after-await – BatteryBackupUnit Jul 04 '15 at 06:32
  • I found this question before. Unfortunately it does not solve the problem. I've updated question and added some more code. – jmazur Jul 04 '15 at 10:14
  • what are you using on HttpContext? we typically discourage the use of HttpContext directly, and instead create an interface & implementation to perform the work on HttpContext. also, yes, you want to inject `ISmsRepository` into the constructor instead of using DependencyResolver. – Dave Thieben Jul 09 '15 at 14:37

0 Answers0