Jimmy D
Jimmy D

Reputation: 5376

VB with dynamic webservice URL

Confused. Writing a VB app that uses a webservice. The URL for this webservice will change based on where the app is running from so I need to change it at runtime. I see that by default the web reference URL has its behavior set to "Dynamic" which means I should be able to change it at runtime, but at the same time the app.config file has the Scope set to "Application," which means it's read-only at runtime. I'm looking to use My.Settings.WebServiceURL = "new url" but in order to do this the scope has to be set to User... I think. But if that's the case then what does the "Dynamic" behavior do for me? Can someone give me a code example of how I can change this at runtime? Thanks in advance.

Upvotes: 1

Views: 9656

Answers (2)

stevehipwell
stevehipwell

Reputation: 57498

Why don't you just write some code to change the WebService Url property from a setting?

oService.Url = sNewUrl

You could use a regular expression to allow you to only specify the url root e.g. http://ws.test.com/ and have that applied to any service that needed moving.

Upvotes: 1

Dmitry Selitskiy
Dmitry Selitskiy

Reputation: 633

Here you go, create a class which will become your web service client:

Public Class WS_ClientClass
Private MyService As MyWebServiceSoap

Public Sub New()
    MyService = New MyWebServiceSoapClient
End Sub

Public Sub New(ByVal strHost As String, ByVal strPort As String)
    'Set up the binding element to match the app.config settings '
    Dim binding = New BasicHttpBinding()
    binding.Name = "MyWebServiceSoap"
    binding.CloseTimeout = TimeSpan.FromMinutes(1)
    binding.OpenTimeout = TimeSpan.FromMinutes(1)
    binding.ReceiveTimeout = TimeSpan.FromMinutes(10)
    binding.SendTimeout = TimeSpan.FromMinutes(1)
    binding.AllowCookies = False
    binding.BypassProxyOnLocal = False
    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard
    binding.MaxBufferSize = 65536
    binding.MaxBufferPoolSize = 524288
    binding.MessageEncoding = WSMessageEncoding.Text
    binding.TextEncoding = System.Text.Encoding.UTF8
    binding.TransferMode = TransferMode.Buffered
    binding.UseDefaultWebProxy = True

    binding.ReaderQuotas.MaxDepth = 32
    binding.ReaderQuotas.MaxStringContentLength = 8192
    binding.ReaderQuotas.MaxArrayLength = 16384
    binding.ReaderQuotas.MaxBytesPerRead = 4096
    binding.ReaderQuotas.MaxNameTableCharCount = 16384

    binding.Security.Mode = BasicHttpSecurityMode.None
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None
    binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None
    binding.Security.Transport.Realm = ""
    binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName
    binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default

    'Define the endpoint address'
    Dim endpointStr = "http://" & strHost & ":" & strPort & "/MyWebApp/MyWebService.asmx"
    Dim endpoint = New EndpointAddress(endpointStr)

    MyService = New MyWebServiceSoapClient(binding, endpoint)
End Sub

This implementation allows you to both use default settings from the Service Reference (first overload of the constructor) as well as custom settings specified in the code (second overload).

Upvotes: 2

Related Questions