C # dynamic WebService call

Source: Internet
Author: User

C # There are many ways to call WebService:

1. Add a WebService reference directly to the project. This method is simple, but not dynamic. That is, you must add a reference again after each service address or content change.

2. To use SoapHttpClientProtocol, you must Reference the Reference generated by adding a WebService. cs-class service interfaces are integrated into the Custom Service call class. The service call class inherits from SoapHttpClientProtocol. if the service interface changes, you need to modify the service call class. The sample code is as follows:

 public class MySoapHttpClientProtocol : SoapHttpClientProtocol    {        public MySoapHttpClientProtocol(string url)        {            Url = url;        }        [SoapHeader("ClientContext")]        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace = MyNamespace,        ResponseNamespace = MyNamespace, Use = System.Web.Services.Description.SoapBindingUse.Literal,        ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]        [return: System.Xml.Serialization.XmlElementAttribute("out", IsNullable = true)]        public string myMethod([System.Xml.Serialization.XmlElementAttribute(IsNullable = true)] string in0)        {            try            {                object[] results = this.Invoke("getConnection", new object[]                                                                    {                                                                        in0                                                                    });                return ((string)(results[0]));            }            catch (Exception ex)            {            }        }    }


Here, myMethod is copied from Reference. cs referenced by the Service. Of course, you can also write it by yourself, which is easier to copy.

3. Dynamic call. The sample code is as follows:

////// WebService proxy class ///Public class WebServiceAgent {private object agent; private Type agentType; private const string CODE_NAMESPACE = "Beyondbit. WebServiceAgent. Dynamic ";///
 
  
/// Call the specified method //////
  Method Name, case sensitive///
  Parameters. Values are assigned in the order of parameters.///
  
   
Web Service Return Value
  Public object Invoke (string methodName, params object [] args) {MethodInfo mi = agentType. GetMethod (methodName); return this. Invoke (mi, args );}///
  /// Call the specified method //////
  Method Information///
  Parameters. Values are assigned in the order of parameters.///
  
   
Web Service Return Value
  Public object Invoke (MethodInfo method, params object [] args) {return method. invoke (agent, args);} public MethodInfo [] Methods {get {return agentType. getMethods ();}}}
 

You only need to specify the service URL, interface name, and parameters for dynamic calling.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.