2
0

ConvDocEncWra.asmx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <%@ WebService Language="c#" Codebehind="ConverterService.asmx.cs" Class="WebServiceTests.ConverterService" %>
  2. using System;
  3. using System.Collections;
  4. using System.Xml.Serialization;
  5. using System.Web.Services;
  6. using System.Web.Services.Protocols;
  7. using System.Web.Services.Description;
  8. namespace WebServiceTests
  9. {
  10. public class UserInfo : SoapHeader
  11. {
  12. public int userId;
  13. }
  14. public class CurrencyInfo
  15. {
  16. public CurrencyInfo ()
  17. {
  18. }
  19. public CurrencyInfo (string name, double rate)
  20. {
  21. Name = name;
  22. Rate = rate;
  23. }
  24. public string Name;
  25. public double Rate;
  26. }
  27. public class Simple
  28. {
  29. public int Dada;
  30. }
  31. [WebServiceAttribute (Namespace="urn:mono-ws-tests", Description="Web service that can make currency conversions")]
  32. [SoapDocumentServiceAttribute (Use=SoapBindingUse.Encoded, ParameterStyle=SoapParameterStyle.Wrapped)]
  33. public class ConverterService : System.Web.Services.WebService
  34. {
  35. static int userCount = 0;
  36. static Hashtable conversionTable;
  37. public UserInfo userInfo;
  38. static ConverterService ()
  39. {
  40. conversionTable = new Hashtable ();
  41. InternalSetCurrencyRate ("USD", 1);
  42. InternalSetCurrencyRate ("EUR", 0.883884 );
  43. InternalSetCurrencyRate ("GBP", 0.611817 );
  44. InternalSetCurrencyRate ("JPY", 118.271 );
  45. InternalSetCurrencyRate ("CAD", 1.36338 );
  46. InternalSetCurrencyRate ("AUD", 1.51485 );
  47. InternalSetCurrencyRate ("CHF", 1.36915 );
  48. InternalSetCurrencyRate ("RUR", 30.4300 );
  49. InternalSetCurrencyRate ("CNY", 8.27740 );
  50. InternalSetCurrencyRate ("ZAR", 7.62645 );
  51. InternalSetCurrencyRate ("MXN", 10.5025 );
  52. }
  53. [WebMethod (Description="Registers the user into the system")]
  54. [SoapHeaderAttribute ("userInfo", Direction = SoapHeaderDirection.Out)]
  55. public void Login (string a)
  56. {
  57. userInfo = new UserInfo ();
  58. userInfo.userId = ++userCount;
  59. }
  60. [WebMethod (Description="Converts an amount from one currency to another currency")]
  61. [SoapHeaderAttribute ("userInfo")]
  62. public double Convert (string sourceCurrency, string targetCurrency, double value)
  63. {
  64. CheckUser ();
  65. double usd = (1 / GetCurrencyRate (sourceCurrency)) * value;
  66. return usd * GetCurrencyRate (targetCurrency);
  67. }
  68. [WebMethod (Description="Returns a list of currency rates")]
  69. [SoapHeaderAttribute ("userInfo")]
  70. public CurrencyInfo[] GetCurrencyInfo ()
  71. {
  72. CheckUser ();
  73. lock (conversionTable)
  74. {
  75. CurrencyInfo[] info = new CurrencyInfo[conversionTable.Count];
  76. int n = 0;
  77. foreach (CurrencyInfo cinfo in conversionTable.Values)
  78. info [n++] = cinfo;
  79. return info;
  80. }
  81. }
  82. [WebMethod (Description="Sets the rate of a currency")]
  83. [SoapHeaderAttribute ("userInfo")]
  84. public void SetCurrencyRate (string currency, double rate)
  85. {
  86. CheckUser ();
  87. InternalSetCurrencyRate (currency, rate);
  88. }
  89. static void InternalSetCurrencyRate (string currency, double rate)
  90. {
  91. lock (conversionTable)
  92. {
  93. conversionTable [currency] = new CurrencyInfo (currency, rate);
  94. }
  95. }
  96. [WebMethod (Description="Returns the rate of a currency")]
  97. [SoapHeaderAttribute ("userInfo")]
  98. public double GetCurrencyRate ([XmlElement(DataType="Name")]string cname)
  99. {
  100. CheckUser ();
  101. lock (conversionTable)
  102. {
  103. if (!conversionTable.ContainsKey (cname))
  104. throw new SoapException ("Unknown currency '" + cname + "'", SoapException.ServerFaultCode);
  105. return ((CurrencyInfo) conversionTable [cname]).Rate;
  106. }
  107. }
  108. [WebMethod]
  109. public void Test (Simple dada1, int dada)
  110. {
  111. dada = 1;
  112. }
  113. [WebMethod (MessageName="Test2")]
  114. public void Test (int[] dada2, byte[] dada3, int dada)
  115. {
  116. dada = 1;
  117. }
  118. void CheckUser ()
  119. {
  120. if (userInfo == null)
  121. throw new SoapException ("User not logged", SoapException.ServerFaultCode);
  122. }
  123. }
  124. }