1 <%@ WebService Language="c#" Codebehind="ConverterService.asmx.cs" Class="WebServiceTests.ConverterService" %>
8 using System.Collections;
10 using System.Xml.Serialization;
11 using System.Web.Services;
12 using System.Web.Services.Protocols;
13 using System.Web.Services.Description;
15 namespace WebServiceTests
17 public class UserInfo : SoapHeader
22 public class CurrencyInfo
24 public CurrencyInfo ()
28 public CurrencyInfo (string name, double rate)
43 [WebServiceAttribute (Namespace="urn:mono-ws-tests", Description="Web service that can make currency conversions")]
45 public class ConverterService : System.Web.Services.WebService
47 static int userCount = 0;
48 static Hashtable conversionTable;
50 public UserInfo userInfo;
52 public SoapHeader unknown1;
53 public SoapHeader[] unknown2;
54 public SoapUnknownHeader unknown3;
55 public SoapUnknownHeader[] unknown4;
57 static ConverterService ()
59 conversionTable = new Hashtable ();
60 InternalSetCurrencyRate ("USD", 1);
61 InternalSetCurrencyRate ("EUR", 0.883884 );
62 InternalSetCurrencyRate ("GBP", 0.611817 );
63 InternalSetCurrencyRate ("JPY", 118.271 );
64 InternalSetCurrencyRate ("CAD", 1.36338 );
65 InternalSetCurrencyRate ("AUD", 1.51485 );
66 InternalSetCurrencyRate ("CHF", 1.36915 );
67 InternalSetCurrencyRate ("RUR", 30.4300 );
68 InternalSetCurrencyRate ("CNY", 8.27740 );
69 InternalSetCurrencyRate ("ZAR", 7.62645 );
70 InternalSetCurrencyRate ("MXN", 10.5025 );
73 [WebMethod (Description="Registers the user into the system")]
74 [SoapHeaderAttribute ("userInfo", Direction = SoapHeaderDirection.Out)]
75 public void Login (string a)
77 userInfo = new UserInfo ();
78 userInfo.userId = ++userCount;
81 [WebMethod (Description="Converts an amount from one currency to another currency")]
82 [SoapHeaderAttribute ("userInfo")]
83 public double Convert (string sourceCurrency, string targetCurrency, double value)
86 double usd = (1 / GetCurrencyRate (sourceCurrency)) * value;
87 return usd * GetCurrencyRate (targetCurrency);
90 [WebMethod (Description="Returns a list of currency rates")]
91 [SoapHeaderAttribute ("userInfo")]
92 public CurrencyInfo[] GetCurrencyInfo ()
96 lock (conversionTable)
98 CurrencyInfo[] info = new CurrencyInfo[conversionTable.Count];
100 foreach (CurrencyInfo cinfo in conversionTable.Values)
106 [WebMethod (Description="Sets the rate of a currency")]
107 [SoapHeaderAttribute ("userInfo")]
108 public void SetCurrencyRate (string currency, double rate)
111 InternalSetCurrencyRate (currency, rate);
114 static void InternalSetCurrencyRate (string currency, double rate)
116 lock (conversionTable)
118 conversionTable [currency] = new CurrencyInfo (currency, rate);
122 [WebMethod (Description="Returns the rate of a currency")]
123 [SoapHeaderAttribute ("userInfo")]
124 public double GetCurrencyRate ([XmlElement(DataType="Name")]string cname)
127 lock (conversionTable)
129 if (!conversionTable.ContainsKey (cname))
130 throw new SoapException ("Unknown currency '" + cname + "'", SoapException.ServerFaultCode);
132 return ((CurrencyInfo) conversionTable [cname]).Rate;
137 public void Test (Simple dada1, int dada)
142 [WebMethod (MessageName="Test2")]
143 public void Test (int[] dada2, byte[] dada3, int dada)
149 public System.Collections.Specialized.StringCollection TestArrays (string[] info, string lang)
156 if (userInfo == null)
157 throw new SoapException ("User not logged", SoapException.ServerFaultCode);
161 [return: $XML_ELEMENT$("retret")]
162 public MyInfo GetTestInfo (string s, out string d)
168 [SoapHeaderAttribute ("unknown1")]
170 public void TestUnknownHeader1 ()
172 if (unknown1 == null)
173 throw new Exception ("Header is null");
175 if (unknown1.Actor != "hi")
176 throw new Exception ("Invalid actor");
179 [SoapHeaderAttribute ("unknown2")]
181 public void TestUnknownHeader2 ()
183 if (unknown2 == null || unknown2.Length != 1)
184 throw new Exception ("Header is null");
186 if (unknown2[0].Actor != "hi")
187 throw new Exception ("Invalid actor");
190 [SoapHeaderAttribute ("unknown3")]
192 public int TestUnknownHeader3 ()
194 if (unknown3 == null)
195 throw new Exception ("Header is null");
197 if (unknown3.Actor != "hi")
198 throw new Exception ("Invalid actor");
200 XmlElement child = unknown3.Element ["userId"];
201 return int.Parse (child.InnerText);
204 [SoapHeaderAttribute ("unknown4")]
206 public int TestUnknownHeader4 ()
208 if (unknown4 == null || unknown4.Length != 1)
209 throw new Exception ("Header is null");
211 if (unknown4[0].Actor != "hi")
212 throw new Exception ("Invalid actor");
214 XmlElement child = unknown4[0].Element ["userId"];
215 return int.Parse (child.InnerText);
222 public string b = "hi";