3
votes

We have hosted our ASP.Net website in Azure. I have written a continuous web job to process some of my files in background on queue trigger. Sometimes the process involves getting an image from an url and save a copy in predefined folder of our site.

We already have a function for getting an image and saving to folder in the codes of main site, so I was using that.

During development, I ran the .exe manually to check if the trigger works. Everything is fine until the image upload process. It can not map the path to given folder. Code is below, the filepath variable is always null, so I get an exception here and the job fails. Is there a better way to do this? Is this because I ran the job manually, or some functions I am using in wrong way?

public class MyHelper
{
    public const string UPLOAD_URL = "/Uploads/";

    // other functions
    // ....

    private static void GetFileLocation(string prefix, string imageName, out string fileUrl, out string saveLocation)
    {
        var filename = imageName;

        // filepath is always null
        var filePath = HostingEnvironment.MapPath(UPLOAD_URL);
        fileUrl = Path.Combine(UPLOAD_URL, filename);

        // this here throws the exception because filepath is null
        saveLocation = Path.Combine(filePath, filename);
    }
}

Appreciate any insight on this. Thanks!!

2

2 Answers

1
votes

You can only write to the root folder of your website. The webjob resides in the directory.

app_data/jobs/continuous/etc

So when you're mapping the path make sure that you're trying to write to the root folder of the website in which the webjob resides. There's more information here.

The problem you're experiencing seems to be that HostingEnvironment.MapPath() is returning a null value. Can you modify the UPLOAD_URL to make sure you're returning something other than null? To debug, you can always Console.Writeline() of your different HostingEnvironment.MapPath(UPLOAD_URL) when you're running this in the cloud and check the output in the Webjobs Dashboard.

Hope this helps,

EDIT:

Try UPLOAD_URL = "~/Uploads/" since it looks like you're searching for a virtual path. Source.

0
votes

Apparently you can write to %HOME%\site\wwwroot, but that didn't work for me, so I used the absolute path to write to anywhere within the Azure Web App: D:\home\site\wwwroot.