I'm running hangfire background process. Now part of the function being called is using MediaService which also needs HttpContext because when I try to execute mediaService.Save(media)
it throws an error of
System.ArgumentNullException
Value cannot be null. Parameter name: httpContext
System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext
From what I was reading it needs to be a normal http request and not a background process.
How can we fake or fix the httpcontext issue inside my background service?
public void saveMedia()
{
var mediaService = ApplicationContext.Current.Services.MediaService;
string[] filePaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "uploads");
foreach (string filePath in filePaths)
{
using (Stream stream = System.IO.File.OpenRead(filePath))
{
string filename = Path.GetFileName(filePath);
string mediaType = Constants.Conventions.MediaTypes.File;
string ext = Path.GetExtension(filename);
IMedia media = mediaService.CreateMedia(filename, Constants.System.Root, mediaType);
media.SetValue("umbracoFile", filename, stream);
mediaService.Save(media); // HTTPCONTEXT error here
media = null;
}
System.IO.File.Delete(filePath);
}
}