How to share dynamic URLs across multiple Web Application Projects

I was recently asked how to configure a Dynamic URL so that it could be updated in machine.config and affect all Web Application Projects calling that Web Reference on that machine.  This is fairly straight forward in VS03 and in VS05 Web Site projects.

In both VS03 and in VS05 Web Site projects the Web Reference proxies are generated to lookup the dynamic url form the <appSettings> section of web.config

Web.config

  <appSettings>
     <add key="localhost.WebService" value="http://localhost/test/WebService.asmx"/>
</appSettings>

 

Proxy Generated:

 

        /// <remarks/>
        public WebService() 
           string urlSetting = System.Configuration.ConfigurationManager.AppSettings["localhost.WebService"];
                  if ((urlSetting != null)) {
                this.Url = urlSetting;
            }
            else {
                this.Url = "http://localhost/test/WebService.asmx";
            }
        }

 

 

Since the <appSettings> section rolls up from machine.config it's easy to put this setting in machine.config remove the entry from  web.config and you're all set.

However, in VS05 WinForm projects and Web Applicaiton Project the proxy is generated to use the  <applicationSettings> section.

 

Web.config

 

    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MainWeb.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>

    <
applicationSettings>
        <MainWeb.Properties.Settings>
            <setting name="MainWeb_localhost_WebService" serializeAs="String">
                <value>http://localhost/test/WebService.asmx</value>
            </setting>
        </MainWeb.Properties.Settings>
    </applicationSettings>

 

 

Proxy Generated:

 

        /// <remarks/>
        public WebService() {
            this.Url = global::MainWeb.Properties.Settings.Default.MainWeb_localhost_WebService;
            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                this.UseDefaultCredentials = true;
                this.useDefaultCredentialsSetExplicitly = false;
            }
            else {
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }

As you can see the proxy generated is very different depending on the type of project you’re using.  If you want to specify a dynamic URL in machine.config that can be overridden in web.config.  It turns out that was very easy to do in VS03 and in Web Site projects because they simply used <appSettings>.  However with WinForm projects and WAP we now getting the settings from the System.Configuration.ClientSettingsSection. See the definition under <configSections> above. 

this.Url = global::MainWeb.Properties.Settings.Default.MainWeb_localhost_WebService; 

Because the generated proxy is assuming that the URL is coming from a configuration section named <MainWeb.Properties.Settings>, you’ll have to make sure you have one defined at the machine.config level. 

You’ll need to add these both to machine.config: 

    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MainWeb.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections> 

    <applicationSettings>
        <MainWeb.Properties.Settings>
            <setting name="MainWeb_localhost_WebService" serializeAs="String">
                <value>http://localhost/test/WebService.asmx</value>
            </setting>
        </MainWeb.Properties.Settings>
    </applicationSettings>

Once this is done your proxy will always find the value from machine.config unless overridden in web.config. 

This get’s more complicated if you’re trying to share the Dynamic URL across multiple applications.  To share a Dynamic URL across applications you’ll want to make sure that the proxy in each WAP project is using the same <applicationSettings> entry.  

The best way to do this is to create a class library project will all the Web References that you want to share.  For example I created one named WebRefLibrary.  The config entries for this library project are as follows. 

    <configSections>
        <sectionGroup name="applicationSettings">
            <section name="WebRefLibrary.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>

    <applicationSettings>
        <WebRefLibrary.Properties.Settings>
            <setting name="WebRefLibrary_localhost_WebService" serializeAs="String">
                <value>http://localhost/test/WebService.asmx</value>
            </setting>
        </WebRefLibrary.Properties.Settings>
    </applicationSettings> 

The next step is to add these entries to machine.config.  Once that is done I can add a reference to WebRefLibrary to each of my WAP Projects.  Even with out adding anything to the web.config of those WAP they will get the dynamic URL from machine.config. 

However I can now update the web.config in any of my WAP projects to override the dynamic URL. 

    <applicationSettings>
        <WebRefLibrary.Properties.Settings>
            <setting name="WebRefLibrary_localhost_WebService" serializeAs="String">
                <value>http://localhost/Production/NewWebService.asmx</value>
            </setting>
        </WebRefLibrary.Properties.Settings>
    </applicationSettings> 

By using a Class Library project containing my Web References I’m able to share a dynamic URL across Web Application Projects and update with the URL by a single change to machine.config.

Hope this helps,
Brad.

8 Comments

Comments have been disabled for this content.