Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to retrieve web-references URL from config file?

Status
Not open for further replies.

huckfinn19

Programmer
May 16, 2003
90
CA
I am using C# 2.0

When creating a new web-reference in a Windows application, Visual Studio creates a bunch of references to the web service in the project. We have moved these references in the App.Config of the application as follows:

<applicationSettings>
<SomeNamespace.WebServices.Properties.Settings>
<setting name="SomeWebServices_Service" serializeAs="String">
<value> </setting>
...
</SomeNamespace.WebServices.Properties.Settings>
</applicationSettings>

I need to be able to retrieve this information at runtime to display the list of web services that are used to the users, without hard coding the access to the web services objects themselves. Basically, if a developper adds a reference to a web service, I want the list of web services displayed to the users at runtime to be updated automatically.

Does anyone knows how to retrieve these settings from the App.Config file at runtime?

Thanks.
 
[ol]
[li]create a new configuration section[/li]
[li]add your services to defined section[/li]
[li]use ConfigurationManager to read the the section[/li]
[/ol]
Code:
<?xml version="1.0"?>
<configuration>
   <configSections>
      <section name="ServiceConfiguration" type="[full object type name[, [namespace], Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/>
   </configSections>
   <ServiceConfiguration name="Available Sercives">
      <Service name="Get Weather" uri="[URL unfurl="true"]http://.../Weather.asmx"[/URL] serializeAs="string" />
      <Service name="Get Stock Quotes" uri="[URL unfurl="true"]http://.../StockMarket.asmx"[/URL] serializeAs="string" />
   </ServiceConfiguration>
<configuration>

I believe the ConfigurationManager has a Sections property which is a list of all sections. from there you can get a ConfigurationSection object to iterate through. I know how this works, just don't know the exact syntax. this should get you started though.
Code:
ConfigurationSection services = ConfigurationManager.Sections["ServiceConfiguration"];
foreach(...)
{
   //parse each service here.
   //add serice to list for display/selection.
}
here is the msdn page for the configurationsection object. this should point you in the right direction.


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi, thanks for your reply.

The thing is, when using the format I mentionned at the beginning of the post, I never need to explicitely configure the web services to point to the URLs specified in the config file. It is done automatically. Using your method would work, but I would have to set the URL of my WS programmatically.

Any other suggestions?

Thanks.
 
I think what your looking for is a Direct Injection (DI) and/or Inversion of Control (IoC) framework to automatically create an instance of a class when you need it. These objects can be added/removed at anytime, without compiling the code, correct?

If so, check out Castle Windsor, StructureMap or spring.Net. These are established DI frameworks all seem to be solid.

I did some preleminary research on this a few months ago. The concepts make sense, the syntax is ugly, for my project this was overkill, so I abandoned the research.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top