混淆。编写使用Web服务的VB应用程序。此Web服务的URL将根据应用程序的运行位置而更改,因此我需要在运行时更改它。我看到默认情况下,Web引用URL的行为设置为“Dynamic”,这意味着我应该能够在运行时更改它,但同时app.config文件将Scope设置为“Application”,这意味着它在运行时是只读的。我希望使用My.Settings.WebServiceURL = "new url"
,但为了做到这一点,范围必须设置为User ...我想。但如果是这样,那么“动态”行为对我有什么作用呢?有人能给我一个代码示例,告诉我如何在运行时更改它吗?提前谢谢。
答案 0 :(得分:2)
在这里,创建一个将成为您的Web服务客户端的类:
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
此实现允许您使用服务引用中的默认设置(构造函数的第一次重载)以及代码中指定的自定义设置(第二次重载)。
答案 1 :(得分:1)
为什么不编写一些代码来更改设置中的WebService Url属性?
oService.Url = sNewUrl
您可以使用正则表达式来仅允许指定网址根,例如http://ws.test.com/
并将其应用于需要移动的任何服务。