Dynamic WebService and static WebService

Source: Internet
Author: User

1. Common static call method, which can be changed by the URL address

 

Localhost. WebService Ws = new localhost. WebService ();
WS. url = "new URL ";
WS. helloworld ();

 

2. Dynamic calling with low performance

 

 

  1. /// <Summary>
  2. /// Dynamic Web Service proxy method
  3. /// </Summary>
  4. /// <Param name = "@ namespace"> name of the input space </param>
  5. /// <Param name = "url"> input URL parameters </param>
  6. /// <Param name = "classname"> input class name </param>
  7. /// <Param name = "methodname"> input method name </param>
  8. /// <Param name = "ARGs"> input parameter. this parameter is an ordered parameter. </param>
  9. Public ObjectInvokewebservice (StringURL,String@Namespace,StringClassname,StringMethodname,Object[] ARGs)
  10. {
  11. Try
  12. {
  13. System. net. WebClient WC =NewSystem. net. WebClient ();
  14. System. Io. Stream stream = WC. openread (URL +"? WSDL");
  15. System. Web. Services. description. servicedescription SD = system. Web. Services. description. servicedescription. Read (Stream );
  16. System. Web. Services. description. servicedescriptionimporter SDI =NewSystem. Web. Services. description. servicedescriptionimporter ();
  17. SDI. addservicedescription (SD,"","");
  18. System. codedom. codenamespace Cn =NewSystem. codedom. codenamespace (@Namespace);
  19. System. codedom. codecompileunit CCU =NewSystem. codedom. codecompileunit ();
  20. CCU. namespaces. Add (CN );
  21. SDI. Import (CN, CCU );
  22. Microsoft. CSHARP. csharpcodeprovider CSC =NewMicrosoft. CSHARP. csharpcodeprovider ();
  23. System. codedom. compiler. icodecompiler ICC = CSC. createcompiler ();
  24. System. codedom. compiler. compilerparameters cplist =NewSystem. codedom. compiler. compilerparameters ();
  25. Cplist. generateexecutable =False;
  26. Cplist. generateinmemory =True;
  27. Cplist. referencedassemblies. Add ("System. dll");
  28. Cplist. referencedassemblies. Add ("System. xml. dll");
  29. Cplist. referencedassemblies. Add ("System. Web. Services. dll");
  30. Cplist. referencedassemblies. Add ("System. Data. dll");
  31. System. codedom. compiler. compilerresults Cr = ICC. compileassemblyfromdom (cplist, CCU );
  32. If(True= Cr. errors. haserrors)
  33. {
  34. System. Text. stringbuilder sb =NewSystem. Text. stringbuilder ();
  35. Foreach(System. codedom. compiler. compilererror CEInCr. Errors)
  36. {
  37. SB. append (Ce. tostring ());
  38. SB. append (system. environment. newline );
  39. }
  40. Throw NewException (sb. tostring ());
  41. }
  42. System. reflection. Assembly = Cr. compiledassembly;
  43. Type T = assembly. GetType (@Namespace+"."+ Classname,True,True);
  44. ObjectOBJ = activator. createinstance (t );
  45. System. reflection. methodinfo MI = T. getmethod (methodname );
  46. ReturnMi. Invoke (OBJ, argS );
  47. }
  48. Catch(Exception ex)
  49. {
  50. Throw NewException (ex. innerexception. Message,NewException (ex. innerexception. stacktrace ));
  51. }
  52. }

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.