Dynamic call Web Service

zhaozj2021-02-12  167

In the development environment of Visual Stdio .NET, the usual way to use Web Service is to add web references.

A problem in the actual programming process: The added web reference is normal in the form, but cannot be used in the class.

So how do I solve it?

We can generate the Agent class of the web service through the WSDL tool, and the generated proxy class can be used by class.

At the command prompt, use WSDL.exe Creating a proxy class, (at least) specifying the URL of the XML Web Services or service description or the path to the saved service description.

WSDL / LANGUAGE: LANGUAGE / PROTOCOL: Protocol / Namespace: MyNamespace / Out: filename / username: username / password: Password / Domain: Domain

Generated proxy class detail

When WSDL.exe is used to generate a proxy class, a source file will be generated in the specified language. This file contains an agent class for the synchronization and asynchronous methods of public XML Web Services. For example, if an XML Web Services contains an XML Web Services method called Add, the proxy class has the following method to call Add XML Web Services methods: add, beginadd, and endadd. The ADD method of the proxy class is used to synchronize communication with the Add XML Web Services method, but BeginAdd and Endadd methods are used for asynchronous communication with the XML Web Services method. For more information on asynchronously communicating with the XML Web Services method, see Asynchronous communication with XML Web Services.

Each method of the generated proxy class contains the appropriate code to communicate with the XML Web Services method. If an error occurs during XML Web Services and proxy communication, an exception will be triggered. For more information on processing exceptions, see Processing and Turning Exceptions in XML Web Services.

Between the XML Web Services method and the definition order in the associated proxy class, the parameter order may vary. In most cases, the parameter sequence is the same. However, if XML Web Services requires the SOAP message in the Document format, a case where a parameter order is not the same. If the XML Web Services method defines the output parameter before defining the input parameters, the output parameter in the proxy class is placed after all input parameters. For example, in the following code example, the XML Web Services Method MYWeBMethod declares the OutStr output parameter before declaring the INSTR input parameters. However, in the proxy class, the INSTR parameter is declared before declaring Outstr.

In some cases, a proxy class generated by WSDL.exe uses an uncommon naming method to enforce objects to convert the objects to the type specified in the service description. Therefore, the generated type in the proxy class may not be the type of development of the developer wants or expected. For example, when WSDL.exe encounters an ArrayList type in the service description, it will create an "object array" in the generated proxy class. To ensure that the object type conversion is correct, open the file containing the generated proxy class, change all incorrect object types into the type of object you need.

转载请注明原文地址:https://www.9cbs.com/read-6430.html

New Post(0)