. NET Dynamic Call WebService

Source: Internet
Author: User

This is not an article that teaches you to understand WebService, nor is it an in-depth understanding of webservice, which is a way to teach you how to call WebService in the development process if dynamic.

in a more common webservice invocation, we typically choose to reference the WebService service in the same way that new additions are added to the project. For example, the following way:1: Create new Add Service reference in Project 2: Enter the address of the referral server and then our Assembly will have the file reference associated with the newly created service reference. Then we can call ServiceReference1 directly in the code, Here is not going to continue the details of the explanation, we still go back to the topic, how to subtract these steps, because such a way to add a reference, 1: Bad extension, 2: more cumbersome Here's a way to invoke WebService References dynamically:A: Right-click in the project to select New and then we modify the component class headquarters. After we add it, the component class internally defaults to:
1    Public Partial classcomponent1:component2     {3          PublicComponent1 ()4         {5 InitializeComponent ();6         }7 8          PublicComponent1 (IContainer container)9         {TenContainer. ADD ( This ); One  A InitializeComponent (); -         } -}
here we need to make some changes.First add the WebService reference to the project.The component class is then inherited from SoapHttpClientProtocol to the component class to:
1[WebServiceBinding (Namespace ="http://tempuri.org/" )]2      Public classComponent1:soaphttpclientprotocol3     {4          PublicComponent1 ()5         {6 InitializeComponent ();7         }8 9          PublicComponent1 (IContainer container)Ten         { OneContainer. ADD ( This ); A  - InitializeComponent (); -         } the}
The
soaphttpclientprotocol SoapHttpClientProtocol class can directly access the specified method of the specified webservice to communicate with XML Web services, for the XML WEB Services creates a proxy class that is derived indirectly or directly from WebClientProtocol. Instead of creating a proxy class manually, use the Web Service Description Language Tool (Wsdl.exe) to create a proxy class for the service description for a given XML Web services. When a proxy class is generated for the SOAP protocol, the synchronous invocation of the XML Web services method is made through the Invoke method, and the asynchronous invocation is done through the BeginInvoke method and the EndInvoke method.
then write the address of the server link that we want to refer to in the configuration file:
1 <add key="serviceaddress" value="http://localhost:7340/ Coursemakerservice.asmx" />

Call the server link address in the Conponent Class construct we added

1   PublicComponent1 (stringserviceurl)2  {3        if(Serviceurl. Equals ("updateserviceaddress" ))4           Base. URL = ConfigurationManager. appsettings["updateserviceaddress" ];5         Else6             Base. URL = ConfigurationManager. appsettings["serviceaddress" ];7}

and then when we want to invoke the method in webservice, we just need to Write in the Component1 class. If I call the way

1[WebServiceBinding (Namespace ="http://tempuri.org/" )]2      Public classOfflinelearingclient:soaphttpclientprotocol3     {4          PublicOfflinelearingclient (stringserviceurl)5         {6             if(Serviceurl. Equals ("updateserviceaddress" ))7                 Base. URL = ConfigurationManager. appsettings["updateserviceaddress" ];8             Else9                 Base. URL = ConfigurationManager. appsettings["serviceaddress" ];Ten         } One          Publicofflinelearingclient () A         { -             Base. URL = ConfigurationManager. appsettings["serviceaddress" ]; -         } the [SoapDocumentMethod] -          Publicyhbjuser GetUser (yhbjuser user) -         { -             return Base. Invoke ("GetUser",New Object[] {user}) [0] asYhbjuser; +         } -  + [SoapDocumentMethod] A          PublicList < yhbjclass> GETTRAININGS11 (stringuserId) at         { -             return Base. Invoke ("GETTRAININGS11",New Object[] {userId}) [0] asList <yhbjclass > ; -}

This allows us to dynamically implement if WebService is called.

. NET Dynamic Call WebService

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.