简体   繁体   中英

Azure Worker Role: Get path of App_Data of Web Role

How can I get the path to my web role's App_Data -folder in a worker role?

Locally, called from the worker role, AppDomain.CurrentDomain.GetData("DataDirectory") returns the path to the web role's App_data just fine. On the Azure server, it returns null , though.

I also tried (System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data") , which returns null both locally as well as on the server.

I believe it is not possible to get this information about a web role from a worker role simply because they are two different applications running on different machines.

Even if it were possible to do so, I would advise you against it. Cloud Service (Web/Worker roles) are stateless so there should not be any dependency between the two. Furthermore think about a scenario where you would have multiple instances of both web and worker roles running. How would a solution like this work in that scenario.

What you need is storage that is accessible to both web and worker role. One possible solution would be to use Azure File Service . What you could do is create a file share in an Azure Storage account and then map that file share as a network drive in both web/worker role. Since the file share is mapped as a network drive, you will be able to perform standard IO operations (System.IO) on this remote storage as if it is something available to you locally. Worker role will receive a file. It will process it and then save it in that file share. Web role would probably implement some kind of File System Watcher which would monitor these files and pick them up when any new/changed file is dropped there.

One thing you would need to be cautious about is how you would handle multi-instance scenario where multiple instances of web role would pick up the same file. You would want to implement some pattern where only one instance of web role will be able to pick up the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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