**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web.Services / Test / standalone / Conv.asmx.template
bloba06215e457880b93057cea07e5b1942e37567edc
1 <%@ WebService Language="c#" Codebehind="ConverterService.asmx.cs" Class="WebServiceTests.ConverterService" %>
3 /*
4 $TEMPLATE_WARNING$
5 */
7 using System;
8 using System.Collections;
9 using System.Xml;
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 
18         {
19                 public int userId;
20         }
21         
22         public class CurrencyInfo
23         {
24                 public CurrencyInfo ()
25                 {
26                 }
28                 public CurrencyInfo (string name, double rate) 
29                 {
30                         Name = name;
31                         Rate = rate;
32                 }
33                 
34                 public string Name;
35                 public double Rate;
36         }
37         
38         public class Simple
39         {
40                 public int Dada;
41         }
43         [WebServiceAttribute (Namespace="urn:mono-ws-tests", Description="Web service that can make currency conversions")]
44         $SERVICE_ATTRIBUTE$
45         public class ConverterService : System.Web.Services.WebService
46         {
47                 static int userCount = 0;
48                 static Hashtable conversionTable;
49                 
50                 public UserInfo userInfo;
51                 
52                 public SoapHeader unknown1;
53                 public SoapHeader[] unknown2;
54                 public SoapUnknownHeader unknown3;
55                 public SoapUnknownHeader[] unknown4;
57                 static ConverterService ()
58                 {
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 );
71                 }
72                 
73                 [WebMethod (Description="Registers the user into the system")]
74                 [SoapHeaderAttribute ("userInfo", Direction = SoapHeaderDirection.Out)]
75                 public void Login (string a)
76                 {
77                         userInfo = new UserInfo ();
78                         userInfo.userId = ++userCount;
79                 }
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)
84                 {
85                         CheckUser ();
86                         double usd = (1 / GetCurrencyRate (sourceCurrency)) * value;
87                         return usd * GetCurrencyRate (targetCurrency);
88                 }
89                 
90                 [WebMethod (Description="Returns a list of currency rates")]
91                 [SoapHeaderAttribute ("userInfo")]
92                 public CurrencyInfo[] GetCurrencyInfo ()
93                 {
94                         CheckUser ();
95                         
96                         lock (conversionTable)
97                         {
98                                 CurrencyInfo[] info = new CurrencyInfo[conversionTable.Count];
99                                 int n = 0;
100                                 foreach (CurrencyInfo cinfo in conversionTable.Values)
101                                         info [n++] = cinfo;
102                                 return info;
103                         }
104                 }
105                 
106                 [WebMethod (Description="Sets the rate of a currency")]
107                 [SoapHeaderAttribute ("userInfo")]
108                 public void SetCurrencyRate (string currency, double rate)
109                 {
110                         CheckUser ();
111                         InternalSetCurrencyRate (currency, rate);
112                 }
114                 static void InternalSetCurrencyRate (string currency, double rate)
115                 {
116                         lock (conversionTable)
117                         {
118                                 conversionTable [currency] = new CurrencyInfo (currency, rate);
119                         }
120                 }
122                 [WebMethod (Description="Returns the rate of a currency")]
123                 [SoapHeaderAttribute ("userInfo")]
124                 public double GetCurrencyRate ([XmlElement(DataType="Name")]string cname)
125                 {
126                         CheckUser ();
127                         lock (conversionTable)
128                         {
129                                 if (!conversionTable.ContainsKey (cname))
130                                         throw new SoapException ("Unknown currency '" + cname + "'", SoapException.ServerFaultCode);
131                                         
132                                 return ((CurrencyInfo) conversionTable [cname]).Rate;
133                         }
134                 }
135                 
136                 [WebMethod]
137                 public void Test (Simple dada1, int dada)
138                 {
139                         dada = 1;
140                 }
141                 
142                 [WebMethod (MessageName="Test2")]
143                 public void Test (int[] dada2, byte[] dada3, int dada)
144                 {
145                         dada = 1;
146                 }
147                 
148                 [WebMethod]
149                 public System.Collections.Specialized.StringCollection TestArrays (string[] info, string lang)
150                 {
151                         return null;
152                 }
153                 
154                 void CheckUser ()
155                 {
156                         if (userInfo == null) 
157                                 throw new SoapException ("User not logged", SoapException.ServerFaultCode);
158                 }
159                 
160                 [WebMethod]
161                 [return: $XML_ELEMENT$("retret")]
162                 public MyInfo GetTestInfo (string s, out string d)
163                 {
164                         d = "iii";
165                         return new MyInfo();
166                 }
167                 
168                 [SoapHeaderAttribute ("unknown1")]
169                 [WebMethod]
170                 public void TestUnknownHeader1 ()
171                 {
172                         if (unknown1 == null)
173                                 throw new Exception ("Header is null");
175                         if (unknown1.Actor != "hi")
176                                 throw new Exception ("Invalid actor");
177                 }
178                 
179                 [SoapHeaderAttribute ("unknown2")]
180                 [WebMethod]
181                 public void TestUnknownHeader2 ()
182                 {
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");
188                 }
189                 
190                 [SoapHeaderAttribute ("unknown3")]
191                 [WebMethod]
192                 public int TestUnknownHeader3 ()
193                 {
194                         if (unknown3 == null)
195                                 throw new Exception ("Header is null");
197                         if (unknown3.Actor != "hi")
198                                 throw new Exception ("Invalid actor");
199                                 
200                         XmlElement child = unknown3.Element ["userId"];
201                         return int.Parse (child.InnerText);
202                 }
203                 
204                 [SoapHeaderAttribute ("unknown4")]
205                 [WebMethod]
206                 public int TestUnknownHeader4 ()
207                 {
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");
213                                 
214                         XmlElement child = unknown4[0].Element ["userId"];
215                         return int.Parse (child.InnerText);
216                 }
217         }
219         public class MyInfo
220         {
221                 public int a = 4;
222                 public string b = "hi";
223         }