我遇到的问题是,当使用不同的环境时,我无法让我的应用程序动态访问单独的应用程序webservice。我知道我需要使url动态化并改变配置文件中的信息。我不确定当webservice是一个Web引用的服务引用时需要采取的步骤(有很多教程显示你可以右键单击>属性>并选择动态URL行为。这不适用于服务引用)如何在运行时配置它?
谢谢!
答案 0 :(得分:1)
为什么不在代码中动态调用Web服务而不是使用静态引用?
我已经使用这段代码已经很久了,不记得我在哪里得到它了:
Friend Class WsProxy
<SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted:=True)> _
Friend Shared Function CallWebService(ByVal webServiceAsmxUrl As String, ByVal serviceName As String, ByVal methodName As String, ByVal args As Object()) As Object
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf bypassAllCertificateStuff)
Dim client As New System.Net.WebClient()
' Connect To the web service
Dim stream As System.IO.Stream = client.OpenRead(webServiceAsmxUrl & "?wsdl")
' Now read the WSDL file describing a service.
Dim description As ServiceDescription = ServiceDescription.Read(stream)
'''// LOAD THE DOM /////////
' Initialize a service description importer.
Dim importer As New ServiceDescriptionImporter()
importer.ProtocolName = "Soap12"
' Use SOAP 1.2.
importer.AddServiceDescription(description, Nothing, Nothing)
' Generate a proxy client.
importer.Style = ServiceDescriptionImportStyle.Client
' Generate properties to represent primitive values.
importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties
' Initialize a Code-DOM tree into which we will import the service.
Dim nmspace As New CodeNamespace()
Dim unit1 As New CodeCompileUnit()
unit1.Namespaces.Add(nmspace)
' Import the service into the Code-DOM tree. This creates proxy code that uses the service.
Dim warning As ServiceDescriptionImportWarnings = importer.Import(nmspace, unit1)
If warning = 0 Then
' If zero then we are good to go
' Generate the proxy code
Dim provider1 As CodeDomProvider = CodeDomProvider.CreateProvider("CSharp")
' Compile the assembly proxy with the appropriate references
Dim assemblyReferences As String() = New String(4) {"System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll"}
Dim parms As New CompilerParameters(assemblyReferences)
Dim results As CompilerResults = provider1.CompileAssemblyFromDom(parms, unit1)
' Check For Errors
If results.Errors.Count > 0 Then
For Each oops As CompilerError In results.Errors
System.Diagnostics.Debug.WriteLine("========Compiler error============")
System.Diagnostics.Debug.WriteLine(oops.ErrorText)
Next
Throw New System.Exception("Compile Error Occured calling webservice. Check Debug ouput window.")
End If
' Finally, Invoke the web service method
Dim wsvcClass As Object = results.CompiledAssembly.CreateInstance(serviceName)
Dim mi As MethodInfo = wsvcClass.[GetType]().GetMethod(methodName)
Return mi.Invoke(wsvcClass, args)
Else
Return Nothing
End If
End Function
Private Shared Function bypassAllCertificateStuff(ByVal sender As Object, ByVal cert As X509Certificate, ByVal chain As X509Chain, ByVal [error] As System.Net.Security.SslPolicyErrors) As Boolean
Return True
End Function
End Class
要使用它,只需使用如下参数创建一个Object类型的数组:
Dim vParams(1) As Object
vParams(0) = "Some param" ' Some string parameter
vParams(1) = 12345 ' Some integer parameter
然后使用对类的静态调用来调用:
WsProxy.CallWebService("http://yourserviceurlhere/", "WsNamespace", "MethodName", vParams)
这将产生Object类型的响应,无论Web服务响应是什么。希望它有所帮助。