Dynamic WebService call-Recommendation

Source: Internet
Author: User

Generally, when WebService needs to be called in a program, we use "add web reference" to let the Vs. NET environment generate a Service proxy for us and then call the corresponding web service. This makes the work simple, but it is bound with the URL, method name, and parameter of the web service. This is the limitation that vs. Net automatically generates Web Service proxy for us. If the URL of the web service has changed on which day, we need to re-Let vs. Net generate the proxy and re-compile it. In some cases, this may be intolerable. We need the ability to dynamically call WebService. For example, you can save the Web Service URL in the configuration file. When the service URL changes, you only need to modify the configuration file.

 

Webservicehelper class:

  1. Using system;
  2. Using system. Web. Services. description;
  3. Using system. IO;
  4. Using system. net;
  5. Using system. text;
  6. Using system. codedom;
  7. Using system. codedom. compiler;
  8. Using Microsoft. CSHARP;
  9. /// <Summary>
  10. /// Summary of webservicehelper
  11. /// </Summary>
  12. Public class webservicehelper
  13. {
  14. Private webservicehelper ()
  15. {
  16. }
  17. // Dynamically call the Web Service
  18. Public static object invokewebservice (string URL, string methodname, object [] ARGs)
  19. {
  20. Return webservicehelper. invokewebservice (URL, null, methodname, argS );
  21. }
  22. Public static object invokewebservice (string URL, string classname, string methodname, object [] ARGs)
  23. {
  24. String @ namespace = "enterpriseserverbase. WebService. dynamicwebcalling ";
  25. If (classname = NULL) | (classname = ""))
  26. {
  27. Classname = webservicehelper. getwsclassname (URL );
  28. }
  29. Try
  30. {
  31. // Obtain the WSDL
  32. WebClient WC = new WebClient ();
  33. Stream stream = WC. openread (URL + "? WSDL ");
  34. Servicedescription SD = servicedescription. Read (Stream );
  35. Servicedescriptionimporter SDI = new servicedescriptionimporter ();
  36. SDI. addservicedescription (SD ,"","");
  37. Codenamespace Cn = new codenamespace (@ namespace );
  38. // Generate client proxy code
  39. Codecompileunit CCU = new codecompileunit ();
  40. CCU. namespaces. Add (CN );
  41. SDI. Import (CN, CCU );
  42. Csharpcodeprovider CSC = new csharpcodeprovider ();
  43. Icodecompiler ICC = CSC. createcompiler ();
  44. // Set compilation Parameters
  45. Compilerparameters cplist = new compilerparameters ();
  46. Cplist. generateexecutable = false;
  47. Cplist. generateinmemory = true;
  48. Cplist. referencedassemblies. Add ("system. dll ");
  49. Cplist. referencedassemblies. Add ("system. xml. dll ");
  50. Cplist. referencedassemblies. Add ("system. Web. Services. dll ");
  51. Cplist. referencedassemblies. Add ("system. Data. dll ");
  52. // Compile the proxy class
  53. Compilerresults Cr = ICC. compileassemblyfromdom (cplist, CCU );
  54. If (true = Cr. errors. haserrors)
  55. {
  56. System. Text. stringbuilder sb = new system. Text. stringbuilder ();
  57. Foreach (system. codedom. compiler. compilererror Ce IN Cr. Errors)
  58. {
  59. SB. append (Ce. tostring ());
  60. SB. append (system. environment. newline );
  61. }
  62. Throw new exception (sb. tostring ());
  63. }
  64. // Generate a proxy instance and call the Method
  65. System. reflection. Assembly = Cr. compiledassembly;
  66. Type T = assembly. GetType (@ namespace + "." + classname, true, true );
  67. Object OBJ = activator. createinstance (t );
  68. System. reflection. methodinfo MI = T. getmethod (methodname );
  69. Return mi. Invoke (OBJ, argS );
  70. }
  71. Catch (exception ex)
  72. {
  73. Throw new exception (ex. innerexception. Message, new exception (ex. innerexception. stacktrace ));
  74. }
  75. }
  76. Private Static string getwsclassname (string wsurl)
  77. {
  78. String [] parts = wsurl. Split ('/');
  79. String [] PPS = parts [parts. Length-1]. Split ('.');
  80. Return PPS [0];
  81. }
  82. }

Procedure:

  1. Using system;
  2. Using system. Web;
  3. Using system. Web. UI;
  4. Using system. Web. UI. htmlcontrols;
  5. Using system. Web. UI. webcontrols;
  6. Using system. Web. UI. webcontrols. webparts;
  7. Public partial class test3: system. Web. UI. Page
  8. {
  9. Protected void page_load (Object sender, eventargs E)
  10. {
  11. String [] ARGs = new string [1];
  12. ARGs [0] = "202.102.224.68 ";
  13. Object OBJ = webservicehelper. invokewebservice ("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx", "getcountrycitybyip", argS );
  14. Response. Write (string []) (OBJ) [1]);
  15. }
  16. }

The program has passed the test. If there are other cases, you can modify it to suit your needs.

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.