WebService.asmx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <%@ WebService Language="C#" Class="Samples.AspNet.WebService" %>
  2. using System;
  3. using System.Web;
  4. using System.Web.Services;
  5. using System.Xml;
  6. using System.Web.Services.Protocols;
  7. using System.Web.Script.Services;
  8. namespace Samples.AspNet
  9. {
  10. [WebService(Namespace = "http://tempuri.org/")]
  11. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  12. [ScriptService]
  13. public class WebService : System.Web.Services.WebService
  14. {
  15. private string _xmlString =
  16. @"<?xml version=""1.0"" encoding=""utf-8"" ?>
  17. <message>
  18. <content>
  19. Welcome to the asynchronous communication layer world!
  20. </content>
  21. </message>";
  22. // This method returns an XmlDocument type.
  23. [WebMethod]
  24. [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
  25. public XmlDocument GetXmlDocument()
  26. {
  27. XmlDocument xmlDoc = new XmlDocument();
  28. xmlDoc.LoadXml(_xmlString);
  29. return xmlDoc;
  30. }
  31. // This method uses GET instead of POST.
  32. // For this reason its input parameters
  33. // are sent by the client in the
  34. // URL query string.
  35. [WebMethod]
  36. [ScriptMethod(UseHttpGet = true)]
  37. public string EchoStringAndDate(DateTime dt, string s)
  38. {
  39. return s + ":" + dt.ToString();
  40. }
  41. [WebMethod]
  42. public string GetServerTime()
  43. {
  44. string serverTime =
  45. String.Format("The current time is {0}.", DateTime.Now);
  46. return serverTime;
  47. }
  48. [WebMethod]
  49. public string Add(int a, int b)
  50. {
  51. int addition = a + b;
  52. string result =
  53. String.Format("The addition result is {0}.",
  54. addition.ToString());
  55. return result;
  56. }
  57. [WebMethod]
  58. [ScriptMethod(ResponseFormat = ResponseFormat.Xml,
  59. XmlSerializeString = true)]
  60. public string GetString()
  61. {
  62. return "Hello World";
  63. }
  64. }
  65. }