(READ!!!) Web Service Proxies in ASP.NET 2.0

Posted Friday, October 14, 2005 4:26 PM by C-Dog's .NET Tip of the Day
Rather than leave something that worked well alone that was quite simple, Microsoft has decided to change where it gets the URL for a dynamic web reference.  Web Applications and everything else (class libraries, windows forms apps, etc.) handle proxies totally differently than in 1.1.
 
Web References in ASP.NET applications sort of look the same.  The biggest difference in there is no reference.cs any more (that is dynamically generated at runtime).  Secondly, web references have been moved from the Web References folder to the App_WebReferences folder.  There is no setting for static/dynamic.  When you set the URL for a web reference, it automatically places a line in the web.config.  This line can easily be moved to the machine.config and it will behave just like a 1.1 web reference proxy.
 
Now for everything else.  Oh boy.  Well, for the most part it behaves a lot like 1.1.  It still has static / dynamic and a reference.cs.  However, they added support for the new Settings.settings file available to class libraries and windows forms applications.  So in order to account for this, they moved it into a section that complies more with the new settings file. 
 
To do this they have a new configSection called applicationSettings (not to be confused with appSettings).  Inside this section it creates a custom section for the assembly you are using called a ClientSettingsSection.  It looks kind of like this when registered in the configSections element.
 
    <sectionGroup name="applicationSettings" 
type="System.Configuration.ApplicationSettingsGroup, 
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="Thrifty.Components.Web.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
 
Now, you want to set those URLs for the web services.  Well the new schema for that looks like this.
 
   <applicationSettings>
        <Thrifty.Components.Web.Properties.Settings> <setting name="Thrifty_Components_Web_SupportWebService_Support"
                serializeAs="String">
                <value>http://172.16.24.150/cmt/private/Support.asmx</value>
            </setting>
        </Thrifty.Components.Web.Properties.Settings>
    </applicationSettings>
 
Notice the name attribute in the setting element.  This is the name of the web refenence including a namespace using underscores instead of periods.  This corresponds to the following code in reference.cs.  Notice it maps to the setting value set in the element.
 
this.Url = 
global::Thrifty.Components.Web.Properties.Settings.Default
.Thrifty_Components_Web_SupportWebService_Support;
 
The value element specifies the URL to the web service.  By default it autogenerates these elements in your app.config in the assembly.
 
This is currently the only way I know of specifying URLs to a web service without custom coding.  There may be some new best practice that we don't yet know of.

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=43