tfsln
asked on
Changing web service url in code
I have an application which connects to a web service (if it helps, i have control over the web service code) and i need to change the Url that the service points to depending on certain conditions.
I am aware of the .Url property, but there is a problem with that.
Dim WSInst As New FareService.FareServices
In order to change the .Url property, i need an instance of the web service. Upon instantiation of the web service using the above code, it tries to connect to it, which throws an exception if the url is invalid (hence why i want to change it!).
I am aware of the .Url property, but there is a problem with that.
Dim WSInst As New FareService.FareServices
In order to change the .Url property, i need an instance of the web service. Upon instantiation of the web service using the above code, it tries to connect to it, which throws an exception if the url is invalid (hence why i want to change it!).
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Create a class in vb.Inside say Global and write the code to create an instance of the web service.
My code is in C#,You can convert into C# by any tool.
//SOService is the webservice reference you have added in the project
private static SOService _mWebService;
//Create an instance of that
public static SOService oWebService
{
get
{
if (_mWebService== null)
_mWebService= new SOService();
_mWebService.Url = <URL From Webconfig> or from a XML config file
return _mWebService;
}
}
In your code you can access the insatnce directly by Classname.property(Global. oWebServic e).
My code is in C#,You can convert into C# by any tool.
//SOService is the webservice reference you have added in the project
private static SOService _mWebService;
//Create an instance of that
public static SOService oWebService
{
get
{
if (_mWebService== null)
_mWebService= new SOService();
_mWebService.Url = <URL From Webconfig> or from a XML config file
return _mWebService;
}
}
In your code you can access the insatnce directly by Classname.property(Global.
What i was trying to say was , when ever you add a web service reference , it creates a file called Reference.cs ,which contain classed through which we can create reference to the webservice .
What i was proposing is, use the same code and create a class ( utility class i the name which i have given ), In which you need to modify the Constructor so that it the URL is fetched from Webconfig and updated .
Then Whenever you need to use the webservices , you will reference with this newly created class instead the webreference you have created ..
Thanks
P.Ramprathap
What i was proposing is, use the same code and create a class ( utility class i the name which i have given ), In which you need to modify the Constructor so that it the URL is fetched from Webconfig and updated .
Then Whenever you need to use the webservices , you will reference with this newly created class instead the webreference you have created ..
Thanks
P.Ramprathap
ASKER
rickson - that is what i do at the moment, and it doesnt work because if the default url for the web service is invalid, then creating a new instance of it will cause it to fail. If it fails then no instance is created which means i can not change the url.
P.Ramprathap - If i do what you are proposing, then each time i update the web service i will need to re-create this custom class to reflect the new properties/methods of the web service is that correct? If that is correct, then i might aswell just modify the constructor of the auto-generated Reference.vb each time, because it will just be one line of code;
Public Sub New()
MyBase.New
Me.Url = CUSTOM_URL_HERE <------------------------- ------
If (Me.IsLocalFileSystemWebSe rvice(Me.U rl) = true) Then
Me.UseDefaultCredentials = true
Me.useDefaultCredentialsSe tExplicitl y = false
Else
Me.useDefaultCredentialsSe tExplicitl y = true
End If
End Sub
If what im saying is correct, then this would be an acceptable solution for me because i dont often update the web service...
P.Ramprathap - If i do what you are proposing, then each time i update the web service i will need to re-create this custom class to reflect the new properties/methods of the web service is that correct? If that is correct, then i might aswell just modify the constructor of the auto-generated Reference.vb each time, because it will just be one line of code;
Public Sub New()
MyBase.New
Me.Url = CUSTOM_URL_HERE <-------------------------
If (Me.IsLocalFileSystemWebSe
Me.UseDefaultCredentials = true
Me.useDefaultCredentialsSe
Else
Me.useDefaultCredentialsSe
End If
End Sub
If what im saying is correct, then this would be an acceptable solution for me because i dont often update the web service...
ASKER
Was able to use your information to solve my problem, but did not get a response to my last question where i needed clarification
ASKER
Im not sure exactly what you mean by create a util class... and also im not familiar with C# im a VB developer although i can generally translate the code myself... Are you able to give some more detail on creating said util class? Maybe a small example?
Thanks alot