none
changing web.config for azure app fabric settings

    Question

  • Hi,

     

    We are using app fabric cache and it works great with the values in web.config.

     

    I want to change the settting after reading from service config. I thought of putting some code in role start up which reads from service config and updates the values in web.config something like this:

     

    using (var server = new ServerManager()) 

       { 

           // get the site's web configuration 

              var siteNameFromServiceModel = "Web"; //this is from from service def

              var siteName = 

              string.Format("{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel); 

              var siteConfig = server.Sites[siteName].GetWebConfiguration();

            //var appSettings = siteConfig.GetSection("dataCacheClient").GetCollection(); 

             //   var section = siteConfig.GetSection("configuration/dataCacheClient");

    //update the values somehow

            server.CommitChanges(); 

       } 

     

    I get the following error when I try to read the datacacheclient section which is d

    Error: The configuration section 'dataCacheClient' cannot be read because it is missing schema.

     

    Has someone done anything similar? Or is there any other way to handle it. I want to change it because this setting would be different from dev/test/uat/prod…

     

    Any pointers?

     

    Manish

    Monday, May 16, 2011 7:11 PM

Answers

  • Hello Manish,

    You want to modify the web.config file in role start.

    Instead of using Configuration class to manipulate the configuration file, you can also use Ling to Xml to implement this. Please refer to the following code sample:

        public override bool OnStart()
        {
            using (var server = new ServerManager())
            {
                var siteNameFromServiceModel = "Web"; //this is from from service def
                var siteName = string.Format("{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
                string configFilePath = server.Sites[siteName].Applications[0].VirtualDirectories[0].PhysicalPath + "Web.config";

                XElement element = XElement.Load(configFilePath);
                element.Element("dataCacheClient").Element("hosts").Element("host").Attribute("name").Value = "my host";
                element.Element("dataCacheClient").Element("securityProperties").Element("messageSecurity").Attribute("authorizationInfo").Value = "my authorization information";
                element.Save(configFilePath);
            } 

            return base.OnStart();
        }

    If you need further assistance, please let me know.

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    • Marked as answer by Wenchao Zeng Monday, May 23, 2011 2:47 AM
    Wednesday, May 18, 2011 3:18 PM

All replies

  • Hi Manish,

    Andy Cross has written about how to Modify the web.config on WebRole Start.

    However, I think your scenario is rather different than what he has outlined.

    Your issue is rather similar to this thread.

    I hope this helps

     

    ~Cory


    Cory Fowler Windows Azure MVP http://blog.syntaxc4.net
    Monday, May 16, 2011 7:35 PM
    Moderator
  • is there anyone who has dnoe something similar. basically you aer right. My problem is similar to  the thread you are mentioning but i dont think there is a soln for it in that thread.
    Monday, May 16, 2011 9:50 PM
  • Hello Manish,

    You want to modify the web.config file in role start.

    Instead of using Configuration class to manipulate the configuration file, you can also use Ling to Xml to implement this. Please refer to the following code sample:

        public override bool OnStart()
        {
            using (var server = new ServerManager())
            {
                var siteNameFromServiceModel = "Web"; //this is from from service def
                var siteName = string.Format("{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
                string configFilePath = server.Sites[siteName].Applications[0].VirtualDirectories[0].PhysicalPath + "Web.config";

                XElement element = XElement.Load(configFilePath);
                element.Element("dataCacheClient").Element("hosts").Element("host").Attribute("name").Value = "my host";
                element.Element("dataCacheClient").Element("securityProperties").Element("messageSecurity").Attribute("authorizationInfo").Value = "my authorization information";
                element.Save(configFilePath);
            } 

            return base.OnStart();
        }

    If you need further assistance, please let me know.

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    • Marked as answer by Wenchao Zeng Monday, May 23, 2011 2:47 AM
    Wednesday, May 18, 2011 3:18 PM
  • The question was how to avoid exception and make the Web.Administration work.
    Thanks that you didn't suggest open a config as a text file and parse it.

    PS. There's a problem with you approach is that section may be in a different file and I was hopping that Web.Administration can help with that.

    Friday, November 09, 2012 3:15 AM