* Methods.cs: Set the correct element name and namespace for headers (those
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / HttpSimpleWebServiceHandler.cs
1 // 
2 // System.Web.Services.Protocols.HttpSimpleWebServiceHandler.cs
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) Ximian, Inc. 2003
8 //
9
10 using System;
11 using System.Reflection;
12 using System.IO;
13 using System.Web.Services;
14
15 namespace System.Web.Services.Protocols 
16 {
17         internal class HttpSimpleWebServiceHandler: WebServiceHandler 
18         {
19                 HttpSimpleTypeStubInfo _typeInfo;
20                 
21                 public HttpSimpleWebServiceHandler (Type type, string protocolName): base (type)
22                 {
23                         _typeInfo = (HttpSimpleTypeStubInfo) TypeStubManager.GetTypeStub (type, protocolName);
24                 }
25                 
26                 protected HttpSimpleTypeStubInfo TypeStub
27                 {
28                         get { return _typeInfo; }
29                 }
30                 
31                 public override void ProcessRequest (HttpContext context)
32                 {
33                         Context = context;
34                         string name = context.Request.PathInfo;
35                         if (name.StartsWith ("/")) name = name.Substring (1);
36                         
37                         Stream respStream = null;
38                         Exception error = null;
39                         try
40                         {
41                                 HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) _typeInfo.GetMethod (name);
42                                 if (method == null) throw new InvalidOperationException ("Method " + name + " not defined in service " + ServiceType.Name);
43                         
44                                 MimeParameterReader parameterReader = (MimeParameterReader) method.ParameterReaderType.Create ();
45                                 object[] parameters = parameterReader.Read (context.Request);
46                 
47                                 MimeReturnWriter returnWriter = (MimeReturnWriter) method.ReturnWriterType.Create ();
48                                 object result = Invoke (method.MethodInfo, parameters);
49                                 respStream = context.Response.OutputStream;
50                                 returnWriter.Write (context.Response, respStream, result);
51                         }
52                         catch (Exception ex)
53                         {
54                                 error = ex;
55                         }
56                         
57                         if (error != null)
58                         {
59                                 try
60                                 {
61                                         context.Response.ContentType = "text/plain";
62                                         context.Response.StatusCode = 500;
63                                         context.Response.Write (error.Message);
64                                 }
65                                 catch {}
66                         }
67                         
68                         if (respStream != null)
69                                 respStream.Close ();
70                 }
71                 
72                 object Invoke (LogicalMethodInfo method, object[] parameters)
73                 {
74                         try
75                         {
76                                 object server = CreateServerInstance ();
77                                 object[] res = method.Invoke (server, parameters);
78                                 if (!method.IsVoid) return res[0];
79                                 else return null;
80                         }
81                         catch (TargetInvocationException ex)
82                         {
83                                 throw ex.InnerException;
84                         }
85                 }
86         }
87 }