object ReadResponse(HttpClientMethod method, WebResponse response, Stream responseStream) { HttpWebResponse httpResponse = response as HttpWebResponse; if (httpResponse != null && (int)httpResponse.StatusCode >= 300) { throw new WebException(RequestResponseUtils.CreateResponseExceptionString(httpResponse, responseStream), null, WebExceptionStatus.ProtocolError, httpResponse); } if (method.readerType == null) { return(null); } // CONSIDER,yannc: in future versions when we allow additional mime formatters we // : should consider giving them access to the response even if there is no // : response content. if (responseStream != null) { MimeReturnReader reader = (MimeReturnReader)MimeFormatter.CreateInstance(method.readerType, method.readerInitializer); return(reader.Read(response, responseStream)); } else { return(null); } }
public static MimeFormatter CreateInstance(Type type, object initializer) { MimeFormatter ob = (MimeFormatter)Activator.CreateInstance(type); ob.Initialize(initializer); return(ob); }
void BuildInitializers(FormatterKind formatter) { Hashtable types = new Hashtable(); foreach (HttpSimpleMethodStubInfo met in Methods) { AddType(types, met.GetFormatterInfo(formatter).Type, met); } foreach (DictionaryEntry ent in types) { Type t = (Type)ent.Key; ArrayList list = (ArrayList)ent.Value; LogicalMethodInfo[] mets = new LogicalMethodInfo [list.Count]; for (int n = 0; n < list.Count; n++) { mets[n] = ((MethodStubInfo)list[n]).MethodInfo; } object[] inits = MimeFormatter.GetInitializers(t, mets); for (int n = 0; n < list.Count; n++) { ((HttpSimpleMethodStubInfo)list[n]).GetFormatterInfo(formatter).Initializer = inits[n]; } } }
internal override object[] ReadParameters() { if (this.serverMethod.readerTypes == null) { return(new object[0]); } for (int i = 0; i < this.serverMethod.readerTypes.Length; i++) { MimeParameterReader reader; if (!this.hasInputPayload) { if (!(this.serverMethod.readerTypes[i] != typeof(UrlParameterReader))) { goto Label_0061; } continue; } if (this.serverMethod.readerTypes[i] == typeof(UrlParameterReader)) { continue; } Label_0061: reader = (MimeParameterReader)MimeFormatter.CreateInstance(this.serverMethod.readerTypes[i], this.serverMethod.readerInitializers[i]); object[] objArray = reader.Read(base.Request); if (objArray != null) { return(objArray); } } if (!this.hasInputPayload) { throw new InvalidOperationException(Res.GetString("WebInvalidRequestFormat")); } throw new InvalidOperationException(Res.GetString("WebInvalidRequestFormatDetails", new object[] { base.Request.ContentType })); }
object ReadResponse(HttpClientMethod method, WebResponse response, Stream responseStream) { HttpWebResponse httpResponse = response as HttpWebResponse; if (httpResponse != null && (int)httpResponse.StatusCode >= 300) { throw new WebException(RequestResponseUtils.CreateResponseExceptionString(httpResponse, responseStream), null, WebExceptionStatus.ProtocolError, httpResponse); } if (method.readerType == null) { return(null); } // if (responseStream != null) { MimeReturnReader reader = (MimeReturnReader)MimeFormatter.CreateInstance(method.readerType, method.readerInitializer); return(reader.Read(response, responseStream)); } else { return(null); } }
internal override void WriteReturns(object[] returnValues, Stream outputStream) { if (this.serverMethod.writerType != null) { ((MimeReturnWriter)MimeFormatter.CreateInstance(this.serverMethod.writerType, this.serverMethod.writerInitializer)).Write(base.Response, outputStream, returnValues[0]); } }
MimeParameterWriter GetParameterWriter(HttpClientMethod method) { if (method.writerType == null) { return(null); } return((MimeParameterWriter)MimeFormatter.CreateInstance(method.writerType, method.writerInitializer)); }
internal HttpClientType(Type type) { LogicalMethodInfo[] infoArray = LogicalMethodInfo.Create(type.GetMethods(), LogicalMethodTypes.Sync); Hashtable formatterTypes = new Hashtable(); for (int i = 0; i < infoArray.Length; i++) { LogicalMethodInfo info = infoArray[i]; try { object[] customAttributes = info.GetCustomAttributes(typeof(HttpMethodAttribute)); if (customAttributes.Length != 0) { HttpMethodAttribute attribute = (HttpMethodAttribute)customAttributes[0]; HttpClientMethod method = new HttpClientMethod { readerType = attribute.ReturnFormatter, writerType = attribute.ParameterFormatter, methodInfo = info }; AddFormatter(formatterTypes, method.readerType, method); AddFormatter(formatterTypes, method.writerType, method); this.methods.Add(info.Name, method); } } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } throw new InvalidOperationException(Res.GetString("WebReflectionError", new object[] { info.DeclaringType.FullName, info.Name }), exception); } } foreach (Type type2 in formatterTypes.Keys) { ArrayList list = (ArrayList)formatterTypes[type2]; LogicalMethodInfo[] methodInfos = new LogicalMethodInfo[list.Count]; for (int j = 0; j < list.Count; j++) { methodInfos[j] = ((HttpClientMethod)list[j]).methodInfo; } object[] initializers = MimeFormatter.GetInitializers(type2, methodInfos); bool flag = typeof(MimeParameterWriter).IsAssignableFrom(type2); for (int k = 0; k < list.Count; k++) { if (flag) { ((HttpClientMethod)list[k]).writerInitializer = initializers[k]; } else { ((HttpClientMethod)list[k]).readerInitializer = initializers[k]; } } } }
internal HttpClientType(Type type) { LogicalMethodInfo[] methodInfos = LogicalMethodInfo.Create(type.GetMethods(), LogicalMethodTypes.Sync); Hashtable formatterTypes = new Hashtable(); for (int i = 0; i < methodInfos.Length; i++) { LogicalMethodInfo methodInfo = methodInfos[i]; try { object[] attributes = methodInfo.GetCustomAttributes(typeof(HttpMethodAttribute)); if (attributes.Length == 0) { continue; } HttpMethodAttribute attribute = (HttpMethodAttribute)attributes[0]; HttpClientMethod method = new HttpClientMethod(); method.readerType = attribute.ReturnFormatter; method.writerType = attribute.ParameterFormatter; method.methodInfo = methodInfo; AddFormatter(formatterTypes, method.readerType, method); AddFormatter(formatterTypes, method.writerType, method); methods.Add(methodInfo.Name, method); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } throw new InvalidOperationException(Res.GetString(Res.WebReflectionError, methodInfo.DeclaringType.FullName, methodInfo.Name), e); } } foreach (Type t in formatterTypes.Keys) { ArrayList list = (ArrayList)formatterTypes[t]; LogicalMethodInfo[] m = new LogicalMethodInfo[list.Count]; for (int j = 0; j < list.Count; j++) { m[j] = ((HttpClientMethod)list[j]).methodInfo; } object[] initializers = MimeFormatter.GetInitializers(t, m); bool isWriter = typeof(MimeParameterWriter).IsAssignableFrom(t); for (int j = 0; j < list.Count; j++) { if (isWriter) { ((HttpClientMethod)list[j]).writerInitializer = initializers[j]; } else { ((HttpClientMethod)list[j]).readerInitializer = initializers[j]; } } } }
internal override void WriteReturns(object[] returnValues, Stream outputStream) { if (serverMethod.writerType == null) { return; } MimeReturnWriter writer = (MimeReturnWriter)MimeFormatter.CreateInstance(serverMethod.writerType, serverMethod.writerInitializer); writer.Write(Response, outputStream, returnValues[0]); }
private object ReadResponse(HttpClientMethod method, WebResponse response, Stream responseStream) { HttpWebResponse response2 = response as HttpWebResponse; if ((response2 != null) && (response2.StatusCode >= HttpStatusCode.MultipleChoices)) { throw new WebException(RequestResponseUtils.CreateResponseExceptionString(response2, responseStream), null, WebExceptionStatus.ProtocolError, response2); } if ((method.readerType != null) && (responseStream != null)) { MimeReturnReader reader = (MimeReturnReader)MimeFormatter.CreateInstance(method.readerType, method.readerInitializer); return(reader.Read(response, responseStream)); } return(null); }
internal override object[] ReadParameters() { if (serverMethod.readerTypes == null) { return(new object[0]); } for (int i = 0; i < serverMethod.readerTypes.Length; i++) { if (!hasInputPayload) { // only allow URL parameters if doesn't have payload if (serverMethod.readerTypes[i] != typeof(UrlParameterReader)) { continue; } } else { // don't allow URL params if has payload if (serverMethod.readerTypes[i] == typeof(UrlParameterReader)) { continue; } } MimeParameterReader reader = (MimeParameterReader)MimeFormatter.CreateInstance(serverMethod.readerTypes[i], serverMethod.readerInitializers[i]); object[] parameters = reader.Read(Request); if (parameters != null) { return(parameters); } } if (!hasInputPayload) { throw new InvalidOperationException(Res.GetString(Res.WebInvalidRequestFormat)); } else { throw new InvalidOperationException(Res.GetString(Res.WebInvalidRequestFormatDetails, Request.ContentType)); } }
public static object[] GetInitializers(Type type, LogicalMethodInfo[] methodInfos) { MimeFormatter ob = (MimeFormatter)Activator.CreateInstance(type); return(ob.GetInitializers(methodInfos)); }
public MimeFormatter Create() { return(MimeFormatter.CreateInstance(Type, Initializer)); }
internal HttpServerType(Type type) : base(type) { this.methods = new Hashtable(); WebServicesSection current = WebServicesSection.Current; Type[] returnWriterTypes = current.ReturnWriterTypes; Type[] parameterReaderTypes = current.ParameterReaderTypes; LogicalMethodInfo[] methods = WebMethodReflector.GetMethods(type); HttpServerMethod[] methodArray = new HttpServerMethod[methods.Length]; object[] objArray = new object[returnWriterTypes.Length]; for (int i = 0; i < objArray.Length; i++) { objArray[i] = MimeFormatter.GetInitializers(returnWriterTypes[i], methods); } for (int j = 0; j < methods.Length; j++) { LogicalMethodInfo info = methods[j]; HttpServerMethod method = null; if (info.ReturnType == typeof(void)) { method = new HttpServerMethod(); } else { for (int num3 = 0; num3 < returnWriterTypes.Length; num3++) { object[] objArray2 = (object[])objArray[num3]; if (objArray2[j] != null) { method = new HttpServerMethod { writerInitializer = objArray2[j], writerType = returnWriterTypes[num3] }; break; } } } if (method != null) { method.methodInfo = info; methodArray[j] = method; } } objArray = new object[parameterReaderTypes.Length]; for (int k = 0; k < objArray.Length; k++) { objArray[k] = MimeFormatter.GetInitializers(parameterReaderTypes[k], methods); } for (int m = 0; m < methods.Length; m++) { HttpServerMethod method2 = methodArray[m]; if (method2 != null) { LogicalMethodInfo info2 = methods[m]; if (info2.InParameters.Length > 0) { int index = 0; for (int num7 = 0; num7 < parameterReaderTypes.Length; num7++) { object[] objArray3 = (object[])objArray[num7]; if (objArray3[m] != null) { index++; } } if (index == 0) { methodArray[m] = null; } else { method2.readerTypes = new Type[index]; method2.readerInitializers = new object[index]; index = 0; for (int num8 = 0; num8 < parameterReaderTypes.Length; num8++) { object[] objArray4 = (object[])objArray[num8]; if (objArray4[m] != null) { method2.readerTypes[index] = parameterReaderTypes[num8]; method2.readerInitializers[index] = objArray4[m]; index++; } } } } } } for (int n = 0; n < methodArray.Length; n++) { HttpServerMethod method3 = methodArray[n]; if (method3 != null) { WebMethodAttribute methodAttribute = method3.methodInfo.MethodAttribute; method3.name = methodAttribute.MessageName; if (method3.name.Length == 0) { method3.name = method3.methodInfo.Name; } this.methods.Add(method3.name, method3); } } }
internal HttpServerType(Type type) : base(type) { WebServicesSection config = WebServicesSection.Current; Type[] returnWriterTypes = config.ReturnWriterTypes; Type[] parameterReaderTypes = config.ParameterReaderTypes; LogicalMethodInfo[] methodInfos = WebMethodReflector.GetMethods(type); HttpServerMethod[] methods = new HttpServerMethod[methodInfos.Length]; object[] initializersByType = new object[returnWriterTypes.Length]; for (int i = 0; i < initializersByType.Length; i++) { initializersByType[i] = MimeFormatter.GetInitializers(returnWriterTypes[i], methodInfos); } for (int i = 0; i < methodInfos.Length; i++) { LogicalMethodInfo methodInfo = methodInfos[i]; HttpServerMethod method = null; if (methodInfo.ReturnType == typeof(void)) { method = new HttpServerMethod(); } else { for (int j = 0; j < returnWriterTypes.Length; j++) { object[] initializers = (object[])initializersByType[j]; if (initializers[i] != null) { method = new HttpServerMethod(); method.writerInitializer = initializers[i]; method.writerType = returnWriterTypes[j]; break; } } } if (method != null) { method.methodInfo = methodInfo; methods[i] = method; } } initializersByType = new object[parameterReaderTypes.Length]; for (int i = 0; i < initializersByType.Length; i++) { initializersByType[i] = MimeFormatter.GetInitializers(parameterReaderTypes[i], methodInfos); } for (int i = 0; i < methodInfos.Length; i++) { HttpServerMethod method = methods[i]; if (method == null) { continue; } LogicalMethodInfo methodInfo = methodInfos[i]; if (methodInfo.InParameters.Length > 0) { int count = 0; for (int j = 0; j < parameterReaderTypes.Length; j++) { object[] initializers = (object[])initializersByType[j]; if (initializers[i] != null) { count++; } } if (count == 0) { methods[i] = null; } else { method.readerTypes = new Type[count]; method.readerInitializers = new object[count]; count = 0; for (int j = 0; j < parameterReaderTypes.Length; j++) { object[] initializers = (object[])initializersByType[j]; if (initializers[i] != null) { method.readerTypes[count] = parameterReaderTypes[j]; method.readerInitializers[count] = initializers[i]; count++; } } } } } for (int i = 0; i < methods.Length; i++) { HttpServerMethod method = methods[i]; if (method != null) { WebMethodAttribute methodAttribute = method.methodInfo.MethodAttribute; method.name = methodAttribute.MessageName; if (method.name.Length == 0) { method.name = method.methodInfo.Name; } this.methods.Add(method.name, method); } } }