简体   繁体   English

Azure webjob 如何访问托管站点文件夹

[英]Azure webjob how to access hosted site folder

We have hosted our ASP.Net website in Azure.我们在 Azure 中托管了我们的 ASP.Net 网站。 I have written a continuous web job to process some of my files in background on queue trigger.我已经编写了一个连续的 Web 作业来在队列触发器的后台处理我的一些文件。 Sometimes the process involves getting an image from an url and save a copy in predefined folder of our site.有时该过程涉及从 url 获取图像并将副本保存在我们网站的预定义文件夹中。

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.在开发过程中,我手动运行 .exe 以检查触发器是否有效。 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.代码如下,文件filepath变量始终为空,所以我在这里遇到异常并且作业失败。 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!!谢谢!!

You can only write to the root folder of your website.您只能写入网站的根文件夹。 The webjob resides in the directory. webjob 驻留在目录中。

app_data/jobs/continuous/etc 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.因此,当您映射路径时,请确保您尝试写入 webjob 所在网站的根文件夹。 There's more information here.这里有更多信息

The problem you're experiencing seems to be that HostingEnvironment.MapPath() is returning a null value.您遇到的问题似乎是 HostingEnvironment.MapPath() 返回空值。 Can you modify the UPLOAD_URL to make sure you're returning something other than null?您能否修改 UPLOAD_URL 以确保您返回的不是空值? 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.要进行调试,您始终可以在云中运行时使用不同 HostingEnvironment.MapPath(UPLOAD_URL) 的 Console.Writeline() 并检查 Webjobs 仪表板中的输出。

Hope this helps,希望这可以帮助,

EDIT:编辑:

Try UPLOAD_URL = "~/Uploads/" since it looks like you're searching for a virtual path.尝试 UPLOAD_URL = "~/Uploads/" 因为它看起来像是在搜索虚拟路径。 Source.来源。

显然您可以写入%HOME%\\site\\wwwroot ,但这对我不起作用,所以我使用绝对路径写入 Azure Web 应用程序中的任何位置: D:\\home\\site\\wwwroot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM