Azure Cloud Service - Configure Session from RoleEnvironment












0















Our application is hosted as a Cloud Service in Azure and we have all our connection strings and other connection-like settings defined in the ServiceConfiguration files. We are also using a Redis Cache as the session state store. We are trying to specify the Redis Cache host and access key in the ServiceConfig and then use those values for the deployment depending on where the bits land. The problem is session is defined in the web.config and we can't pull RoleEnvironment settings into the web.config.



We tried altering the web.config in the Application_Startup method but get errors that access is denied to the web.config on startup, which makes sense.



We don't really want to write deployment scripts to give the Network Service user access to the web.config.



Is there a way to setup session to use a different Redis Cache at runtime of the application?



enter image description here



The error that we are getting is "Access to the path 'E:sitesrootweb.config' is denied'. I read an article that gave some examples on how to give the Network Service user access to the web.config as part of the role starting process and did that and then now we have access to the file but now get the following error "Unable to save config to file 'E:sitesrootweb.config'."










share|improve this question

























  • Hello, Can you show the part of the Application_Startup that opens the web.config ? Along with the error being generated?

    – Nick Polyderopoulos
    Nov 15 '18 at 23:16











  • Added a screenshot of the code. This gets called in from the Application_Startup method in the Global.asax file

    – Nick Olsen
    Nov 16 '18 at 16:41











  • so you are getting the error because this piece of code will make the application to restart everytime. It does not make sense to do it that way you dont even check if you have the information there already. So let me ask you this. Is there any particular reason that you want to do this with every app startup ?

    – Nick Polyderopoulos
    Nov 16 '18 at 19:11











  • I'm not married to this way by any means. All we are trying to do is be able to define the session state provider information AFTER the project has been built. We are trying to be able to deploy the application bits to different environments and those environments require different end points for the session state provider. Since it appears the only place to define the session state provider is the web.config we are trying to alter the web.config after the cloud service package as been created.

    – Nick Olsen
    Nov 16 '18 at 20:29











  • I dont have access to my azure web site at the moment but if i remember correctly you are allowed to make changes to the web.config before deployment from the azure portal. If not that if you are using some type of CI/CD you could implement the changes through one of the steps of your deployment.

    – Nick Polyderopoulos
    Nov 16 '18 at 21:12
















0















Our application is hosted as a Cloud Service in Azure and we have all our connection strings and other connection-like settings defined in the ServiceConfiguration files. We are also using a Redis Cache as the session state store. We are trying to specify the Redis Cache host and access key in the ServiceConfig and then use those values for the deployment depending on where the bits land. The problem is session is defined in the web.config and we can't pull RoleEnvironment settings into the web.config.



We tried altering the web.config in the Application_Startup method but get errors that access is denied to the web.config on startup, which makes sense.



We don't really want to write deployment scripts to give the Network Service user access to the web.config.



Is there a way to setup session to use a different Redis Cache at runtime of the application?



enter image description here



The error that we are getting is "Access to the path 'E:sitesrootweb.config' is denied'. I read an article that gave some examples on how to give the Network Service user access to the web.config as part of the role starting process and did that and then now we have access to the file but now get the following error "Unable to save config to file 'E:sitesrootweb.config'."










share|improve this question

























  • Hello, Can you show the part of the Application_Startup that opens the web.config ? Along with the error being generated?

    – Nick Polyderopoulos
    Nov 15 '18 at 23:16











  • Added a screenshot of the code. This gets called in from the Application_Startup method in the Global.asax file

    – Nick Olsen
    Nov 16 '18 at 16:41











  • so you are getting the error because this piece of code will make the application to restart everytime. It does not make sense to do it that way you dont even check if you have the information there already. So let me ask you this. Is there any particular reason that you want to do this with every app startup ?

    – Nick Polyderopoulos
    Nov 16 '18 at 19:11











  • I'm not married to this way by any means. All we are trying to do is be able to define the session state provider information AFTER the project has been built. We are trying to be able to deploy the application bits to different environments and those environments require different end points for the session state provider. Since it appears the only place to define the session state provider is the web.config we are trying to alter the web.config after the cloud service package as been created.

    – Nick Olsen
    Nov 16 '18 at 20:29











  • I dont have access to my azure web site at the moment but if i remember correctly you are allowed to make changes to the web.config before deployment from the azure portal. If not that if you are using some type of CI/CD you could implement the changes through one of the steps of your deployment.

    – Nick Polyderopoulos
    Nov 16 '18 at 21:12














0












0








0








Our application is hosted as a Cloud Service in Azure and we have all our connection strings and other connection-like settings defined in the ServiceConfiguration files. We are also using a Redis Cache as the session state store. We are trying to specify the Redis Cache host and access key in the ServiceConfig and then use those values for the deployment depending on where the bits land. The problem is session is defined in the web.config and we can't pull RoleEnvironment settings into the web.config.



We tried altering the web.config in the Application_Startup method but get errors that access is denied to the web.config on startup, which makes sense.



We don't really want to write deployment scripts to give the Network Service user access to the web.config.



Is there a way to setup session to use a different Redis Cache at runtime of the application?



enter image description here



The error that we are getting is "Access to the path 'E:sitesrootweb.config' is denied'. I read an article that gave some examples on how to give the Network Service user access to the web.config as part of the role starting process and did that and then now we have access to the file but now get the following error "Unable to save config to file 'E:sitesrootweb.config'."










share|improve this question
















Our application is hosted as a Cloud Service in Azure and we have all our connection strings and other connection-like settings defined in the ServiceConfiguration files. We are also using a Redis Cache as the session state store. We are trying to specify the Redis Cache host and access key in the ServiceConfig and then use those values for the deployment depending on where the bits land. The problem is session is defined in the web.config and we can't pull RoleEnvironment settings into the web.config.



We tried altering the web.config in the Application_Startup method but get errors that access is denied to the web.config on startup, which makes sense.



We don't really want to write deployment scripts to give the Network Service user access to the web.config.



Is there a way to setup session to use a different Redis Cache at runtime of the application?



enter image description here



The error that we are getting is "Access to the path 'E:sitesrootweb.config' is denied'. I read an article that gave some examples on how to give the Network Service user access to the web.config as part of the role starting process and did that and then now we have access to the file but now get the following error "Unable to save config to file 'E:sitesrootweb.config'."







asp.net-mvc azure session web-config azure-cloud-services






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 16:40







Nick Olsen

















asked Nov 15 '18 at 21:55









Nick OlsenNick Olsen

3,83874470




3,83874470













  • Hello, Can you show the part of the Application_Startup that opens the web.config ? Along with the error being generated?

    – Nick Polyderopoulos
    Nov 15 '18 at 23:16











  • Added a screenshot of the code. This gets called in from the Application_Startup method in the Global.asax file

    – Nick Olsen
    Nov 16 '18 at 16:41











  • so you are getting the error because this piece of code will make the application to restart everytime. It does not make sense to do it that way you dont even check if you have the information there already. So let me ask you this. Is there any particular reason that you want to do this with every app startup ?

    – Nick Polyderopoulos
    Nov 16 '18 at 19:11











  • I'm not married to this way by any means. All we are trying to do is be able to define the session state provider information AFTER the project has been built. We are trying to be able to deploy the application bits to different environments and those environments require different end points for the session state provider. Since it appears the only place to define the session state provider is the web.config we are trying to alter the web.config after the cloud service package as been created.

    – Nick Olsen
    Nov 16 '18 at 20:29











  • I dont have access to my azure web site at the moment but if i remember correctly you are allowed to make changes to the web.config before deployment from the azure portal. If not that if you are using some type of CI/CD you could implement the changes through one of the steps of your deployment.

    – Nick Polyderopoulos
    Nov 16 '18 at 21:12



















  • Hello, Can you show the part of the Application_Startup that opens the web.config ? Along with the error being generated?

    – Nick Polyderopoulos
    Nov 15 '18 at 23:16











  • Added a screenshot of the code. This gets called in from the Application_Startup method in the Global.asax file

    – Nick Olsen
    Nov 16 '18 at 16:41











  • so you are getting the error because this piece of code will make the application to restart everytime. It does not make sense to do it that way you dont even check if you have the information there already. So let me ask you this. Is there any particular reason that you want to do this with every app startup ?

    – Nick Polyderopoulos
    Nov 16 '18 at 19:11











  • I'm not married to this way by any means. All we are trying to do is be able to define the session state provider information AFTER the project has been built. We are trying to be able to deploy the application bits to different environments and those environments require different end points for the session state provider. Since it appears the only place to define the session state provider is the web.config we are trying to alter the web.config after the cloud service package as been created.

    – Nick Olsen
    Nov 16 '18 at 20:29











  • I dont have access to my azure web site at the moment but if i remember correctly you are allowed to make changes to the web.config before deployment from the azure portal. If not that if you are using some type of CI/CD you could implement the changes through one of the steps of your deployment.

    – Nick Polyderopoulos
    Nov 16 '18 at 21:12

















Hello, Can you show the part of the Application_Startup that opens the web.config ? Along with the error being generated?

– Nick Polyderopoulos
Nov 15 '18 at 23:16





Hello, Can you show the part of the Application_Startup that opens the web.config ? Along with the error being generated?

– Nick Polyderopoulos
Nov 15 '18 at 23:16













Added a screenshot of the code. This gets called in from the Application_Startup method in the Global.asax file

– Nick Olsen
Nov 16 '18 at 16:41





Added a screenshot of the code. This gets called in from the Application_Startup method in the Global.asax file

– Nick Olsen
Nov 16 '18 at 16:41













so you are getting the error because this piece of code will make the application to restart everytime. It does not make sense to do it that way you dont even check if you have the information there already. So let me ask you this. Is there any particular reason that you want to do this with every app startup ?

– Nick Polyderopoulos
Nov 16 '18 at 19:11





so you are getting the error because this piece of code will make the application to restart everytime. It does not make sense to do it that way you dont even check if you have the information there already. So let me ask you this. Is there any particular reason that you want to do this with every app startup ?

– Nick Polyderopoulos
Nov 16 '18 at 19:11













I'm not married to this way by any means. All we are trying to do is be able to define the session state provider information AFTER the project has been built. We are trying to be able to deploy the application bits to different environments and those environments require different end points for the session state provider. Since it appears the only place to define the session state provider is the web.config we are trying to alter the web.config after the cloud service package as been created.

– Nick Olsen
Nov 16 '18 at 20:29





I'm not married to this way by any means. All we are trying to do is be able to define the session state provider information AFTER the project has been built. We are trying to be able to deploy the application bits to different environments and those environments require different end points for the session state provider. Since it appears the only place to define the session state provider is the web.config we are trying to alter the web.config after the cloud service package as been created.

– Nick Olsen
Nov 16 '18 at 20:29













I dont have access to my azure web site at the moment but if i remember correctly you are allowed to make changes to the web.config before deployment from the azure portal. If not that if you are using some type of CI/CD you could implement the changes through one of the steps of your deployment.

– Nick Polyderopoulos
Nov 16 '18 at 21:12





I dont have access to my azure web site at the moment but if i remember correctly you are allowed to make changes to the web.config before deployment from the azure portal. If not that if you are using some type of CI/CD you could implement the changes through one of the steps of your deployment.

– Nick Polyderopoulos
Nov 16 '18 at 21:12












1 Answer
1






active

oldest

votes


















0














We ended up being able to solve this using the ServerManager API in the WebRole.OnStart method. We did something like this:



using (var server = new ServerManager())
{
try
{
Site site = server.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];
string physicalPath = site.Applications["/"].VirtualDirectories["/"].PhysicalPath;
string webConfigPath = Path.Combine(physicalPath, "web.config");

var doc = System.Xml.Linq.XDocument.Load(webConfigPath);

var redisCacheProviderSettings = doc.Descendants("sessionState").Single().Descendants("providers").Single().Descendants("add").Single();

redisCacheProviderSettings.SetAttributeValue("host", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheHost"));
redisCacheProviderSettings.SetAttributeValue("accessKey", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheAccessKey"));
redisCacheProviderSettings.SetAttributeValue("ssl", "true");
redisCacheProviderSettings.SetAttributeValue("throwOnError", "false");

doc.Save(webConfigPath);
}
catch (Exception ex)
{
// Log error
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328445%2fazure-cloud-service-configure-session-from-roleenvironment%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    We ended up being able to solve this using the ServerManager API in the WebRole.OnStart method. We did something like this:



    using (var server = new ServerManager())
    {
    try
    {
    Site site = server.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];
    string physicalPath = site.Applications["/"].VirtualDirectories["/"].PhysicalPath;
    string webConfigPath = Path.Combine(physicalPath, "web.config");

    var doc = System.Xml.Linq.XDocument.Load(webConfigPath);

    var redisCacheProviderSettings = doc.Descendants("sessionState").Single().Descendants("providers").Single().Descendants("add").Single();

    redisCacheProviderSettings.SetAttributeValue("host", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheHost"));
    redisCacheProviderSettings.SetAttributeValue("accessKey", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheAccessKey"));
    redisCacheProviderSettings.SetAttributeValue("ssl", "true");
    redisCacheProviderSettings.SetAttributeValue("throwOnError", "false");

    doc.Save(webConfigPath);
    }
    catch (Exception ex)
    {
    // Log error
    }
    }





    share|improve this answer




























      0














      We ended up being able to solve this using the ServerManager API in the WebRole.OnStart method. We did something like this:



      using (var server = new ServerManager())
      {
      try
      {
      Site site = server.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];
      string physicalPath = site.Applications["/"].VirtualDirectories["/"].PhysicalPath;
      string webConfigPath = Path.Combine(physicalPath, "web.config");

      var doc = System.Xml.Linq.XDocument.Load(webConfigPath);

      var redisCacheProviderSettings = doc.Descendants("sessionState").Single().Descendants("providers").Single().Descendants("add").Single();

      redisCacheProviderSettings.SetAttributeValue("host", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheHost"));
      redisCacheProviderSettings.SetAttributeValue("accessKey", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheAccessKey"));
      redisCacheProviderSettings.SetAttributeValue("ssl", "true");
      redisCacheProviderSettings.SetAttributeValue("throwOnError", "false");

      doc.Save(webConfigPath);
      }
      catch (Exception ex)
      {
      // Log error
      }
      }





      share|improve this answer


























        0












        0








        0







        We ended up being able to solve this using the ServerManager API in the WebRole.OnStart method. We did something like this:



        using (var server = new ServerManager())
        {
        try
        {
        Site site = server.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];
        string physicalPath = site.Applications["/"].VirtualDirectories["/"].PhysicalPath;
        string webConfigPath = Path.Combine(physicalPath, "web.config");

        var doc = System.Xml.Linq.XDocument.Load(webConfigPath);

        var redisCacheProviderSettings = doc.Descendants("sessionState").Single().Descendants("providers").Single().Descendants("add").Single();

        redisCacheProviderSettings.SetAttributeValue("host", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheHost"));
        redisCacheProviderSettings.SetAttributeValue("accessKey", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheAccessKey"));
        redisCacheProviderSettings.SetAttributeValue("ssl", "true");
        redisCacheProviderSettings.SetAttributeValue("throwOnError", "false");

        doc.Save(webConfigPath);
        }
        catch (Exception ex)
        {
        // Log error
        }
        }





        share|improve this answer













        We ended up being able to solve this using the ServerManager API in the WebRole.OnStart method. We did something like this:



        using (var server = new ServerManager())
        {
        try
        {
        Site site = server.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];
        string physicalPath = site.Applications["/"].VirtualDirectories["/"].PhysicalPath;
        string webConfigPath = Path.Combine(physicalPath, "web.config");

        var doc = System.Xml.Linq.XDocument.Load(webConfigPath);

        var redisCacheProviderSettings = doc.Descendants("sessionState").Single().Descendants("providers").Single().Descendants("add").Single();

        redisCacheProviderSettings.SetAttributeValue("host", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheHost"));
        redisCacheProviderSettings.SetAttributeValue("accessKey", RoleEnvironment.GetConfigurationSettingValue("SessionRedisCacheAccessKey"));
        redisCacheProviderSettings.SetAttributeValue("ssl", "true");
        redisCacheProviderSettings.SetAttributeValue("throwOnError", "false");

        doc.Save(webConfigPath);
        }
        catch (Exception ex)
        {
        // Log error
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 13 '18 at 23:39









        Nick OlsenNick Olsen

        3,83874470




        3,83874470






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328445%2fazure-cloud-service-configure-session-from-roleenvironment%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)