J.D. Meier's Blog

Software Engineering, Project Management, and Effectiveness

How To Reference Web Services and Databases During Development

How To Reference Web Services and Databases During Development

  • Comments 2

One technique for pointing your Web services and database references to alternate locations during development is to use a user.config file.  Although you could change your app.config references directly, using a level of indirection keeps your production settings intact while carving out just the references to your Web services and database connections.

To use this approach, you point your app.config file to a user.config file.  You then store your user.config file with production settings in source control.  Each user changes their user.config file to point to the dev or test locations, but they don't check this in.

In .NET 1.1, you can use the approach outlined in "Managing Dependencies" from Team Development with Visual Studio .NET and Visual SourceSafe.

In .NET 2.0, you can use configSource to redirect from your app.config to user.config.

Referencing Web Services from WinForms
In a WinForms application, you would do the following:
1.  Add a Web service reference to your WinForm.  This will add an app.config file with settings for the Web service:
        <WindowsApplication1.Properties.Settings>
            <setting name="WindowsApplication1_localhost_Service" serializeAs="String">
                <value>http://localhost:8085/WebServiceTest/Service.asmx</value>
            </setting>
        </WindowsApplication1.Properties.Settings>
2.  Add an application configuration file and name it user.config
3.  Delete all the text in user.config
4.  Copy the relevent settings from your app.config to your user.config
        <WindowsApplication1.Properties.Settings>
            <setting name="WindowsApplication1_localhost_Service" serializeAs="String">
                <value>http://localhost:8085/WebServiceTest/Service.asmx</value>
            </setting>
        </WindowsApplication1.Properties.Settings>
Important  - Your user.config file should only the settings above.
5.  In app.config, use configSource to redirect from app.config to user.config
        <WindowsApplication1.Properties.Settings configSource="user.config">
  <!--
            <setting name="WindowsApplication1_localhost_Service" serializeAs="String">
                <value>http://localhost:8085/WebServiceTest/Service.asmx</value>
            </setting>
  -->
        </WindowsApplication1.Properties.Settings
IImportant - comment out the settings, since you will now be using the settings in user.config
6.  change the "Copy to Output Directory" property of the user.config file from "Do not copy" to "Copy if newer"

Referencing Web Services from WebForms
In an ASP.NET application, you would do the following:
1.  add a Web services reference.  This would create settings in Web.config
 <appSettings>
 <add key="localhost.Service" value="http://localhost:8085/WebServiceTest/Service.asmx"/>
 </appSettings>
2.  Add a new web.config file and rename it to user.config
3.  Delete all the text in the user.config file.
4.  copy appSettings from web.config to user.config
 <appSettings>
 <add key="localhost.Service" value="http://localhost:8085/WebServiceTest/Service.asmx"/>
 </appSettings>
Important  - Your user.config file should strictly have only the settings above.
5.  In web.config, use configSource to redirect from app.config to user.config
 <appSettings configSource="user.config">
  <!--
  <add key="localhost.Service" value="http://localhost:8085/WebServiceTest/Service.asmx"/>
  -->
 </appSettings>
Important - comment out the settings, since you will now be using the settings in user.config

Referencing Database Connections from WinForms
To reference a database from a Winform application, you would do the following:
1.  Add an application configuration file and name it app.config
2.  Add a reference to the configuration dll (System.Configuration)
3.  Add an application configuration and rename it to user.config file
4.  Add your connection string in app.config
 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
  <connectionStrings>
  <add name="test" connectionString="Server=MyServer;Database=MyDatabase;Trusted_Connection=Yes" providerName="System.Data.SqlClient" />
  </connectionStrings>
 </configuration>
5.  Test your connection string
           string connectionString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
           using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
           }
6.  Copy your connectionStrings from app.config to user.config
  <connectionStrings>
  <add name="test" connectionString="Server=MyServer;Database=MyDatabase;Trusted_Connection=Yes" providerName="System.Data.SqlClient" />
  </connectionStrings>
Important - this should be the only text in your user.config
7.  In app.config, redirect to user.config using configSource
 <connectionStrings configSource="user.config">
  <!--
  <add name="test" connectionString="Server=MyServer;Database=MyDatabase;Trusted_Connection=Yes" providerName="System.Data.SqlClient" />
   -->
  </connectionStrings>
Important - comment out the settings, since you will now be using the settings in user.config 
8.  On your user.config file, change the "Copy to Output Directory" property from "Do not copy" to "Copy if newer"

For more information, take a look at Explained: Managing Source Control Dependencies in Visual Studio Team.

Page 1 of 1 (2 items)