0

我遇到的问题是,在使用不同的环境时,我无法让我的应用程序动态访问单独的应用程序 web 服务。我知道我需要使 url 动态化并更改配置文件中的信息。我不确定当 Web 服务是 Web 引用的服务引用时需要采取的步骤(有很多教程显示您可以右键单击 > 属性 > 并选择动态 URL 行为。这不适用于服务引用)如何我要在运行时配置它吗?

谢谢!

4

1 回答 1

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 服务响应是什么。希望能帮助到你。

于 2012-10-29T18:46:52.547 回答