WebService.asmx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <%@ WebService Language="C#" Class="Samples.AspNet.TestService" %>
  2. namespace Samples.AspNet
  3. {
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Web.Services;
  8. using System.Web.Script.Services;
  9. // Custom type used by WebService.
  10. public class SimpleClass
  11. {
  12. private string _s = "SimpleClass1";
  13. public String s {
  14. get { return _s; }
  15. set { _s = value; }
  16. }
  17. }
  18. // Custom type used by WebService.
  19. public class SimpleClass2
  20. {
  21. private string _s = "SimpleClass2";
  22. public String s
  23. {
  24. get { return _s; }
  25. set { _s = value; }
  26. }
  27. }
  28. // The following service allows the generation
  29. // of the SimpleClass2 type proxy by applying the
  30. // attribute: GenerateScriptType. This assures
  31. // that the a SimpleClass2 object can be
  32. // passed by the client script to the
  33. // PassGenericDictionary method.
  34. // Note if you comment out the GenerateScriptType
  35. // you get an error because the SimpleClass2 type proxy
  36. // is not generated.
  37. [WebService(Namespace = "http://tempuri.org/")]
  38. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  39. [GenerateScriptType(typeof(SimpleClass2))]
  40. [ScriptService]
  41. public class TestService : System.Web.Services.WebService
  42. {
  43. // The following method return a list of SimpleClass
  44. // objects.
  45. // Note the SimpleClass type proxy is automatically
  46. // generated because used in the return parameter.
  47. [WebMethod]
  48. [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
  49. public List<SimpleClass> GetGenericList()
  50. {
  51. List<SimpleClass> GetGenericList = new List<SimpleClass>();
  52. SimpleClass simple1 = new SimpleClass();
  53. SimpleClass simple2 = new SimpleClass();
  54. simple1.s = "Generics first instance";
  55. simple2.s = "Generics second instance";
  56. GetGenericList.Add(simple1);
  57. GetGenericList.Add(simple2);
  58. return GetGenericList;
  59. }
  60. // The following method return a Dictionary of strings.
  61. [WebMethod]
  62. public Dictionary<String, String> GetGenericDictionary()
  63. {
  64. //Instantiate the dictionary object.
  65. Dictionary<String, String> result =
  66. new Dictionary<string, string>();
  67. //Add items.
  68. result.Add("0000FF", "Blue");
  69. result.Add("FF0000", "Red");
  70. result.Add("00FF00", "Green");
  71. result.Add("000000", "Black");
  72. return result;
  73. }
  74. // The following method return a Dictionary of
  75. // SimpleClass objects.
  76. // Note the SimpleClass type proxy object is automatically
  77. // generated because used in the return parameter.
  78. [WebMethod]
  79. public Dictionary<String, SimpleClass> GetGenericCustomTypeDictionary()
  80. {
  81. //Instantiate the dictionary object.
  82. Dictionary<String, SimpleClass> result =
  83. new Dictionary<string, SimpleClass>();
  84. SimpleClass simple = new SimpleClass();
  85. simple.s = "custom type instance";
  86. //Add items.
  87. result.Add("Custom type", simple);
  88. return result;
  89. }
  90. // The following method accept a Dictionary of
  91. // SimpleClass2 objects.
  92. // Note the SimpleClass2 type proxy object is
  93. // not automatically generated because used in
  94. // an input parameter. You must enable its
  95. // generation by applying the GenerateScriptType
  96. // attribute to the service.
  97. [WebMethod]
  98. public String PassGenericDictionary(
  99. Dictionary<string, SimpleClass2> d)
  100. {
  101. string value = d["first"].s;
  102. string message = "Dictionary element value: " + value;
  103. return message;
  104. }
  105. // This function returns an array.
  106. [WebMethod]
  107. public ArrayList GetArray()
  108. {
  109. //Instantiate the array object.
  110. ArrayList result = new ArrayList();
  111. //Add items.
  112. result.Add("First element: " + "Test1");
  113. result.Add("Second element: " + "Test2");
  114. return result;
  115. }
  116. }
  117. }