2 // System.Web.Services.Protocols.HttpSimpleWebServiceHandler.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) Ximian, Inc. 2003
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Reflection;
34 using System.Web.Services;
36 namespace System.Web.Services.Protocols
38 internal class HttpSimpleWebServiceHandler: WebServiceHandler
40 HttpSimpleTypeStubInfo _typeInfo;
41 HttpSimpleMethodStubInfo method;
43 public HttpSimpleWebServiceHandler (Type type, string protocolName)
46 _typeInfo = (HttpSimpleTypeStubInfo) TypeStubManager.GetTypeStub (type, protocolName);
49 protected HttpSimpleTypeStubInfo TypeStub
51 get { return _typeInfo; }
54 internal override MethodStubInfo GetRequestMethod (HttpContext context)
56 string name = context.Request.PathInfo;
57 if (name.StartsWith ("/"))
58 name = name.Substring (1);
60 method = (HttpSimpleMethodStubInfo) _typeInfo.GetMethod (name);
62 WriteError (context, "Method " + name + " not defined in service " + ServiceType.Name);
67 public override void ProcessRequest (HttpContext context)
70 Stream respStream = null;
71 Exception error = null;
76 method = (HttpSimpleMethodStubInfo) GetRequestMethod (context);
78 if (method.MethodInfo.EnableSession)
79 Session = context.Session;
81 MimeParameterReader parameterReader = (MimeParameterReader) method.ParameterReaderType.Create ();
82 object[] parameters = parameterReader.Read (context.Request);
84 MimeReturnWriter returnWriter = (MimeReturnWriter) method.ReturnWriterType.Create ();
85 object result = Invoke (method.MethodInfo, parameters);
86 respStream = context.Response.OutputStream;
87 returnWriter.Write (context.Response, respStream, result);
95 WriteError (context, error.ToString ());
97 if (respStream != null)
101 void WriteError (HttpContext context, string msg)
105 context.Response.ContentType = "text/plain";
106 context.Response.StatusCode = 500;
107 context.Response.Write (msg);
108 context.Response.End ();
113 object Invoke (LogicalMethodInfo method, object[] parameters)
117 object server = CreateServerInstance ();
119 object[] res = method.Invoke (server, parameters);
120 if (!method.IsVoid) return res[0];
124 IDisposable disp = server as IDisposable;
129 catch (TargetInvocationException ex)
131 throw ex.InnerException;