CodeSnip: Modifying Web.config file during installation
page 1 of 1
Published: 02 May 2006
Unedited - Community Contributed
Abstract
This application illustrates an installer that changes the values in a webservice's web.config file during setup, according to user entry. The application changes values of key values in the web.config file.
by Amal ElHosseiny
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 37757/ 42

In this article, I will demonstrate how to modify web.config file for web a service project during installation dynamically according to user entries.

Download Source

 

Figure 1

As web.config is a configuration file for ASP.NET applications or ASP.NET web service applications to store data, it can be modified by the user after application setup using Notepad or programmatically. Most programmers use web.config to store data that can be changed dynamically by the user.

Listing 1: Web.Config

<appSettings>
  <add key="Name" value="Keyvalue"/>
</appSettings>

A user may have to open and change this file every time the web application or service is moved from server to another. To avoid it, in the installer I've enabled a facility for user to change the xml attributes values.

Listing 2: Web.Config

<appSettings>
  <add key="Main.ConnectionString"
       value="server=(local);database=DatabaseName;
               User ID=sa;Password=;
              trusted_connection=false"/>
</appSettings>

In this article, I will change the value for <appsettings> attribute by implementing installer class added to web service project.

For that first from the project I will add a new item as shown in figure 2.

Figure 2

From Add New Item screen I will select Installer Class as shown in figure 3.

Figure 3

Then in the installer class, I will override the Install function as shown in listing 3.

Listing 3: Install () in Installer.cs

public override void Install(IDictionarystateSaver)
{
    //You type here your own code
    CreateConnectionString();
    base.Install (stateSaver);
}

To get data required to change the values in web.config from the user, I will add a custom action in the setup project as shown in figure 4.

Figure 4

I will then change the custom data property for it as shown in listing 5.

Listing 5:Custom Action Installer

/Server=[EDITA1]@/Username=[EDITA2]@/Password=[EDITA3]@/Folder=[EDITA4]

[EDITA1], [EDITA2], [EDITA3], [EDITA4] are names of textboxes in the dialog added in the setup project interface.

Figure 5

You will notice the value here:

/Server=[EDITA1]@/Username=[EDITA2]@/Password=[EDITA3]@/Folder=[EDITA4]

I've separated the parameters to the Installer class by a special letter (@).

Listing 6 :void FormateParamters()

{
string strServer,strUser , strPassword,strFolder;
EventLog.WriteEntry("TNT", Context.Parameters["Server"]);
strServer=Context.Parameters["Server"].Split('@')[0];
strUser=Context.Parameters["Server"].Split('@')[1];
strPassword=Context.Parameters["Server"].Split('@')[2];
strFolder=Context.Parameters["Server"].Split('@')[3];
Context.Parameters["Server"]=strServer;
Context.Parameters["Username"]=strUser.Split('=')[1];
EventLog.WriteEntry("user",Context.Parameters["Username"]);
Context.Parameters["Password"]=strPassword.Split('=')[1];
EventLog.WriteEntry("Password",Context.Parameters["Password"]);
Context.Parameters["Folder"]=strFolder.Split('=')[1];
EventLog.WriteEntry("folder",Context.Parameters["Folder"]);
EventLog.WriteEntry("Server",Context.Parameters["Server"]);
}

The previous function is to format the parameters by splitting them using the special character (@) so we can use them later for the connection string reconstruction function:

Listing 7: void CreateConnectionString()

{
    FormateParamters();
    Assembly ass=Assembly.GetExecutingAssembly();
    EventLog.WriteEntry ("Web.Config",ass.GetName().Name+".Web.config");
    Stream stmConfig=ass.GetManifestResourceStream(                 ass.GetName().Name+".Web.config");
   if(!Directory.Exists(Context.Parameters["Folder"]))
        Directory.CreateDirectory(Context.Parameters["Folder"]);
    FileStream stmPhysical=new FileStream(
       Context.Parameters["Folder"]+@"\Web.config",FileMode.Create);
    StreamReader srConfig=newStreamReader(stmConfig);
    StreamWriter swConfig=newStreamWriter(stmPhysical);
    string strConfig=srConfig.ReadToEnd();
    stmConfig.Close();
   strConfig=strConfig.Replace("server=(local);database" + 
              "=DatabaseName;UserID=sa;Password=;" + 
             "trusted_connection=false",NewConnection());
    swConfig.Write(strConfig);
    swConfig.Close ();
    stmPhysical.Close();
}

The above function copies the web.config from the physical path and begins to replace the old key value with the new value.

Note: the local folder parameter must be the same path where the web service is installed in. In this case, it is:

C:\intpub\wwwroot\webtest1\

Download Source

Conclusion

In this article, you have learned how to change any attributes values in web.config. From onwards IIS6 we can change web.config files dynamically using ASP.NET MMS snap-in. If you are curious to know more about it, you can check http://msdn2.microsoft.com/en-us/library/ms178683(VS.80).aspx link.

 



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 7/31/2014 5:37:57 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search