How to Manage Web Service Dependencies

- J.D. Meier, Jason Taylor, Alex Mackman, Prashant Bansode

Generally, the Web service Uniform Resource Locator (URL) reference in production is different from that for the development and test environments. To facilitate management of each of these Web service URL references, the URL value should be specified in a user configuration file that can be changed by individual developers and testers without impacting the main App.config file. To do this, you set the URL Behavior property of your Web service reference to Dynamic.* *Store and reference the Web service URL from a user configuration file.

By default, Visual Studio sets the value of this property to Dynamic when you add a Web reference.

To verify that this value is still set to Dynamic
  1. In Solution Explorer, expand the list of Web references.
  2. Select each Web reference in the list.
  3. For each Web reference, check that the value of the URL Behavior property is set to Dynamic.

Specify a Web Service URL in a user configuration file

When you first add a Web reference, the App.config file looks like the following:

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name=" SomeService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <YourProject.Properties.Settings>
            <setting name="SomeService_ localhost _Service" serializeAs="String">
                <value>http://localhost/someservice/Service.asmx</value>
            </setting>
        </ YourProject.Properties.Settings>
    </applicationSettings>
</configuration>


This file has a new configuration section that contains the address of the Web service that Visual Studio found when generating this proxy.

To create a User.config file
  1. In Solution Explorer, right-click the project that contains the Web service reference, point to Add, and then click New Item.
  2. Select Application Configuration File, change the name to User.config, and then click Add.
  3. Copy the <YourProject.Properties.Settings> element setting from your application configuration file (App.config) to the User.config file. This file should contain only the element for which the runtime is redirected. Delete the <?xml> directive and the <configuration> element if present, as shown in the following example:

<YourProject.Properties.Settings>
    <setting name="SomeService_localhost_Service" serializeAs="String">
       <value>http://localhost/someservice/Service.asmx</value>
    </setting>
</YourProject.Properties.Settings>

  1. In Solution Explorer, right-click the User.config file, click Properties, and then set the Copy To Output Directory property to Copy if newer.

Individual developers should set their User.config file to reference the appropriate Web service URL reference.

To update the App.config file to reference User.config file when accessing the Web service URL
  1. Add a configSource="user.config" attribute to the <YourProject.Properties.Settings> element of your main application configuration file.
This silently redirects the runtime to the named user configuration file when it accesses information from this section.
  1. Delete the content from the <YourProject.Properties.Settings> element.

Your App.config should now look like the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="SomeService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
  <applicationSettings>
    <YourProject.Properties.Settings  configSource="user.config">
    </YourProject.Properties.Settings>
   </applicationSettings>
</configuration>


In the preceding example, YourProject is the name of the project that contains the Web service reference. Make sure that the <SomeService.Properties.Service> element is empty in the App.config file.

Important:
  • Do not add your User.config file to source control. By not doing so, each developer (and the test team) can explicitly bind to specific URLs by using his or her own User.config files. To prevent this, clear the User.config file check box when you first check in the file. You can then right-click the file in Solution Explorer and select the Undo Pending Changes option to ensure that the file is not subject to source control.
  • Source control may contain other User.config files; for example, for testing and production. These files should be managed by the users responsible for managing the testing and production environments. These test and production User.config files should not be stored as part of the Web service projects but should be in different areas of the source control system.
  • Store a global User.config file in source control. This could either contain only the root element (no <setting> element), or it could specify the default location of the Web service. The User.config file must be present for the configuration system to work.

It is important to understand that the User.config file must be present if you are using this mechanism. Some team member must be responsible for ensuring that the environment is correct when creating builds for production releases and for any test environments. As part of this build step, the appropriate User.confg file must be retrieved from the source control system and copied into the correct location in order for MSBuild to be able to find it.

Additional Resources

Last edited Aug 16, 2007 at 6:32 PM by prashantbansode, version 5

Comments

No comments yet.