Web Services .net framework 2

I have just ported an ASP.NET 1.1 application to framework version 2 and i have a problem.

My solution has a Web project and a class library project. The class library project has a web reference to a web service. The Web project makes a call to the web service.

I used to hold the url for the web service in the web.config and because the web reference was set to dynamic, this all used to work fine.

Now it looks a though the class library project has a config file with the url in. My question is how do I change the path of the url after the project has been deployed to the web server? I Can't find any config files.

Putting the value in web.config does no longer work.

Any help will be appreciated.

Dave Bates

[729 byte] By [Dave_Bates] at [2008-2-15]
# 1
Hi,

The Webservice object has a property called Url. You can retrieve the Url of the WebService from the web.config file and assign it manually in the class library - assigning it should surely work.

MyWebService obj = new MyWebService();
obj.Url = ConfigurationManager.AppSettings["MyUrl"];

Regards,
Vikram

Vikram at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 2
Thanks for your reply, that makes sense, but i have hundreds of web service calls. Is there a way to set the URL globally?

Dave

Dave_Bates at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 3
Hi,

In this situation you can go for a Wrapper Class and Wrap each method of the webservice with same methods on the wrapper. Within the methods u call the original methods. I have illustrated the concept with a code snippet below.

public class MyWebServiceWrapper
{
private MyWebService obj = new MyWebService();
public MyWebServiceWrapper()
{
obj.Url = ConfigurationManager.AppSettings["MyUrl"];
}

public void WebMethodA()
{
obj.WebMethodA();
}
}

Now, in your code, use just the wrapper class and call the methods.

Regards,
Vikram

Vikram at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 4
Excellent, thanks.

Dave

Dave_Bates at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 5
A couple of questions:
1). In your example is the web service local?
2). In order to access a web service in VS 2K5 you have to add a "web reference". Is there a way to update the references URL at runtime?
SideByEach at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 6
Hi,

1.) The web service need not be local. You need it be accessible when you add a Web Reference thats all.

2.) Thats what has been shown. You would save the URL in the web.config file and then assign it to the .Url property of the WebService object at runtime in code.

Regards,
Vikram

Vikram at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 7
Thanks for the quick response. I should have been more clear on one thing. I'm trying to consume a web service in a VB.Net 2k5 poject.
SideByEach at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 8
Hi,

You will need to add a reference to System.Configuration.dll and then add an Imports directive to System.Configuration.

Then you would use the ConfigurationManager class to retrieve the stored setting:


objWebService.Url = ConfigurationManager.AppSettings["WebServiceUrl"]

Regards,
Vikram

Vikram at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 9
I managed to get access to the configuration manager. I still can't seem to get access to any application settings.
Here is part of my "app.config" file:
<applicationSettings>
<WebServiceTest.Settings>
<setting name="WebServiceTest_GetPrinters_ServiceRequestHandler"
serializeAs="String">
<value>http://someBox/ServiceRequestHandler/handler.asmx</value>
</setting>
</WebServiceTest.Settings>
</applicationSettings>
When I use the following code:
Dim test As Integer = ConfigurationManager.AppSettings.Count
I get a return of 0 everytime.

SideByEach at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 10
A little help?
SideByEach at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 11
Hi,

You can access the property as follows:


My.Settings.WebServiceTest_GetPrinters_ServiceRequestHandler

Regards,
Vikram

Vikram at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 12
Worked like gold, thanks.
SideByEach at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...

.NET Development

Site Classified