Windows Develop Bookmark and Share   
 index > Windows Forms General > web service overriding the url property
 

web service overriding the url property

Ok, here's my problem.

I have a web service referenced in my windows app and depending on what options the user selects when they log in, i need to change the url that the web service points to.

Before you can change the URL, you obviouslyneed to create an instance of the web service. The problem with this is that if the default url fails, then an exception is raised and the web service is not instantiated so i dont even get the chance to change the url.

My solution to this, was to go into the auto generated code file for the web service, and change its "New" function to require a URL parameter (which i pass it when i instantiate the web service in my code)

The problem with this, is that each time i update the web service i need to go in and make this change and its a hassle because i have a number of different web services which get updated regularly.

My next idea involved creating a custom service class, inheriting the web service, and overridding the Url property. The problem with that is that the Url property can not be overridden. So instead of using the'overrides' keyword, i used'shadows' instead (admittedly, not really knowing exactly whatthe difference was).

It looked ok... when i requested the URL property of my new class it returned the right value... but when it actually performed a request,it was performing it against the default URL. What can i do here? I am really sick of having to make these changes every time i update the web reference.

This is whatsaid custom class looked like;
    Public Class Override_TakeFliteService
        Inherits TakeFliteService.TakeFliteServices
        Dim CustomHost As String

        Public Sub New(ByVal CustomHostName As String)
            MyBase.New()
            Me.CustomHost = CustomHostName
        End Sub

        Public Shadows Property Url() As String
            Get
                If CustomHost = "" Then
                    Select Case ServerType
                        Case ServerTypes.Production
                            Return SetHostname(MyBase.Url, ProductionAddress)
                        Case Else
                            Return SetHostname(MyBase.Url, UATAddress)
                    End Select
                Else
                    Return SetHostname(MyBase.Url, CustomHost)
                End If
            End Get
            Set(ByVal value As String)

            End Set
        End Property
    End Class
nizmo  Tuesday, September 15, 2009 9:34 PM
Just to follow up... assuming there isn't any other way around your problem you could try putting your custom constructor in a seperate .vb file. The class generate by vs is marked as partial, so for example, if it's generated a class for you called CustomerOrderWS you could create a secondvb file with the additional constructor in it.

I don't know the VB syntax, but in C# it would go something like this;

CustomerOrderWSCustomCode.cs

public partial class CustomerOrderWS
{
  public CustomerOrderWS(string url) 
  {

public CustomerOrderWS() {

this.Url = url;
if ((this.IsLocalFileSystemWebService(this.Url) == true)) {

this.UseDefaultCredentials = true;

this.useDefaultCredentialsSetExplicitly = false;

}

else {

this.useDefaultCredentialsSetExplicitly = true;

}

}


   }
}


That code is pretty much a straight copy and paste of the generated constructor except for the line declaring the constructor and the line that sets the url property based on the constructor argument.

Since the custom constructor is now in a seperate file, when you regenerate the web sevice reference (update it) you shouldn't lose your code.

----

Follow up... I just walked the code for the constructor in a web service proxy object that VS generated for me, and nowhere does it appear to make any connection to the remote service so you should be able to create an instance of the object and set the URL fine without any problems.
  • Marked As Answer bynizmo Tuesday, September 22, 2009 4:07 AM
  •  
Yort  Friday, September 18, 2009 4:07 AM
I posted the same question on experts exchange, and for the first time ever, i didnt get a single response. Now im getting the same thing here!

Whats up with that?!? I didn't think that question was that difficult!
nizmo  Friday, September 18, 2009 3:47 AM
Hi,

I'm not sure why you're having this problem. I work primarily in C# and not VB .Net, but when I create a new instance of a web service proxy object (i.e the class generated by VS for me) it doesn't make any attempt to connect to the remote service, so it doesn't matter what the URL is or if that service exists/is reachable. I can just create an instance of the class, set the URL property and then call the service. At least, I'm pretty sure that's the case.

Can you show the code that instantiates and sets up the web service ? Perhaps there is somethign there that is causing it to contact the service unnecessarily.

If you set the URL to an empty string in the properties on the service reference, and then create the service object, does that work ? It would be annoying because you'd have the put the URL back before you did an update on the service, but it might be less annoying than your current problems.
Yort  Friday, September 18, 2009 4:01 AM
Just to follow up... assuming there isn't any other way around your problem you could try putting your custom constructor in a seperate .vb file. The class generate by vs is marked as partial, so for example, if it's generated a class for you called CustomerOrderWS you could create a secondvb file with the additional constructor in it.

I don't know the VB syntax, but in C# it would go something like this;

CustomerOrderWSCustomCode.cs

public partial class CustomerOrderWS
{
  public CustomerOrderWS(string url) 
  {

public CustomerOrderWS() {

this.Url = url;
if ((this.IsLocalFileSystemWebService(this.Url) == true)) {

this.UseDefaultCredentials = true;

this.useDefaultCredentialsSetExplicitly = false;

}

else {

this.useDefaultCredentialsSetExplicitly = true;

}

}


   }
}


That code is pretty much a straight copy and paste of the generated constructor except for the line declaring the constructor and the line that sets the url property based on the constructor argument.

Since the custom constructor is now in a seperate file, when you regenerate the web sevice reference (update it) you shouldn't lose your code.

----

Follow up... I just walked the code for the constructor in a web service proxy object that VS generated for me, and nowhere does it appear to make any connection to the remote service so you should be able to create an instance of the object and set the URL fine without any problems.
  • Marked As Answer bynizmo Tuesday, September 22, 2009 4:07 AM
  •  
Yort  Friday, September 18, 2009 4:07 AM
The partial class thing is a great idea, that should do it. Cheers!
nizmo  Tuesday, September 22, 2009 4:06 AM

You can use google to search for other answers

Custom Search

More Threads

• Passing (or accessing) arrays on one win form to another.
• Check node level in treeview
• Class with UserControl and multiple buttons - please help
• ToolStripStatusLabel cutoff
• ON buton click reset combobox to blank value.. HELP
• Displaying non-child windows at the start of the mdi app
• Events Handling Determination
• Set Image background form Resource File's Image.
• Screen Resolution
• Inverse of GraphicPath.Warp() ?