Nop 4.2 _httpContextAccessor.HttpContext Null Referance Exception when using PictureService in a Task

5 months ago
We have a background task that depends on _pictureService.GetPictureUrl method. In the method GetImagesPathUrl (which is called by GetThumbUrl) the _httpContextAccessor.HttpContext is NULL and throws an error. In 4.10 this method didn't exist and the shop path was handled by webHelper.GetStoreLocation(); I guess the Task doesn't have a  context, hence the exception.
5 months ago
Hello,

In 4.20 you can find here,

https://github.com/nopSolutions/nopCommerce/blob/develop/src/Libraries/Nop.Core/IWebHelper.cs
1 month ago
Hi.
I have same problem in version 4.20.
How to create new instance of IHttpContextAccessor in my plugin's static method.
I use this code in my static method,but httpAccessor.HttpContext is null.

var httpAccessor = EngineContext.Current.Resolve<IHttpContextAccessor>();
var domainName = httpAccessor.HttpContext.Request.Host.ToString();


Please advice me.
Thanks
[Best Regard]
1 month ago
I'm going inside a static class, I use this method:


        public static string GetDomainName()
        {
            var httpAccessor = EngineContext.Current.Resolve<IHttpContextAccessor>();
            var domainName = httpAccessor.HttpContext.Request.Host.ToString();
            //HttpContext.Current.Request.Url.Host.ToLower();
            domainName = domainName.Replace("www.", "");
            domainName = domainName.Replace("http://", "");
            domainName = domainName.Replace("https://", "");

            domainName = domainName.Replace(".com", "");

            return domainName;
        }


But httpAccessor.HttpContext is null.

Please advice me.
Thanks
1 month ago
Hello,

If you want to use in static class than httpconext pass as method para.

May be it works, try please.