Windows Azure creating virtual directory to local storage

自古美人都是妖i 提交于 2019-12-11 04:09:08

问题


I need a help with creating virtual directory pointing to local storage in Windows Azure (production environment). I can set up a virtual directory manually but it is being erased every time Azure is restarted. The clue is to create the virtual directory via config file when I upload a package of my project on Azure. The question is how to create such directory so that it is pointing to local storage.

Thx in advance for any suggestions.

Best Regards,

Darek


回答1:


I suggest you create the virtual directory by interacting with IIS in the WebRole's OnStart method:

public class WebRole : RoleEntryPoint
{
    public override bool OnStart()
    {
        // Connect to the IIS site.
        using (var manager = new Microsoft.Web.Administration.ServerManager())
        { 
                var localResourcePath = RoleEnvironment.GetLocalResource("MyResource").RootPath;

            // Add to the root application.
            var rootSite = manager.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];
            var rootApplication = rootSite.Applications["/"];
                rootApplication.VirtualDirectories.Add("/myVdir", localResourcePath);

                // Save
            manager.CommitChanges();
        }

        ...
    }
}

If I'm right you'll need to set the execution context to elevated for this to work. You can do this in the ServiceDefintion.csdef:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MyProject" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2012-05.1.7">
  <WebRole name="MyRole" vmsize="Small" enableNativeCodeExecution="true">
    <Runtime executionContext="elevated" />
    ...
  </WebRole>
</ServiceDefinition>

Note: You'll need to reference Microsoft.Web.Administration.dll (C:\Windows\System32\inetsrv)



来源:https://stackoverflow.com/questions/11187877/windows-azure-creating-virtual-directory-to-local-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!