private void ExecuteInAppDomain(Type T, int roomCatalogId, HttpContext context, string verb, MemoryStream GetValueCollectionStream, HttpCookieCollection cookieCollection) { try { //Log4NetHandler logger = new Log4NetHandler(); //logger.SendToLogger("RESTService", "INFO", String.Format("ExecuteInAppDomain roomCatalogId: {0} Verb: {1}", roomCatalogId, verb)); AppDomain appDomain = appRoomCatalog.GetCatalogAppDomain(roomCatalogId.ToString()); if (appDomain == null) { appDomain = CreateAppDomain(roomCatalogId); } if (appDomain == null) { throw new SystemException(String.Format("CreateAppDomain roomCatalogId: {0} Error: appDomain is null", roomCatalogId)); } //hangoutCookies HangoutCookiesCollection oCookies = new HangoutCookiesCollection(); oCookies.InitCookies(context.Request.Cookies); appDomain.SetData("AppCookies", oCookies); HangoutPostedFile oFile = new HangoutPostedFile(); if (context.Request.Files.Count > 0) { oFile.InitPostedFile(context.Request.Files[0]); } appDomain.SetData("PostedFile", oFile); AppErrorInformation oErrorInfo = new AppErrorInformation(); oErrorInfo.RequestType = context.Request.RequestType; oErrorInfo.URL = context.Request.Url.ToString(); oErrorInfo.UserHostAddress = context.Request.UserHostAddress; oErrorInfo.Timestamp = context.Timestamp; oErrorInfo.MachineName = Dns.GetHostName().ToString(); appDomain.SetData("AppErrorInfo", oErrorInfo); mServiceLog.Log.Debug("ExecuteInAppDomain (create instance of ) (" + verb + ")"); AppRestHandler mbrt = (AppRestHandler)appDomain.CreateInstanceAndUnwrap("AppService", "Hangout.Server.WebServices.AppService.AppRestHandler"); mServiceLog.Log.Debug("ExecuteInAppDomain (process request through specific dll) (" + verb + ")"); //Specified room catalog of 1 for the data if we branch db this needs to change to be the roomCatalogId. MemoryStream outputStream = mbrt.ProcessRequest(T, verb, 1, GetValueCollectionStream); Session.Refresh(); mServiceLog.Log.Debug("ExecuteInAppDomain (sending response) (" + verb + ")"); // serialize the response based on the methods return type context.Response.OutputStream.Write(outputStream.GetBuffer(), 0, (int)outputStream.Length); // System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); // string sTemp = enc.GetString(outputStream.GetBuffer()); } catch (System.Exception ex) { if (ex.GetType() == typeof(TargetInvocationException)) { ex = ex.InnerException; } bool debugMode = (ConfigurationManager.AppSettings["DebugMode"] == "true") ? true : false; HangoutException hangoutException = ex as HangoutException; if (hangoutException == null) { hangoutException = new HangoutException(ex); } RESTerror e = hangoutException.GetRestError(debugMode); // Add additional debug information hangoutException.RequestType = context.Request.RequestType; hangoutException.RequestUrl = context.Request.Url.ToString(); hangoutException.ClientIp = context.Request.UserHostAddress; hangoutException.TimeStamp = context.Timestamp; hangoutException.MachineName = Dns.GetHostName().ToString(); if (Session.GetCurrentUserId() != null) { hangoutException.CurrentUserId = Session.GetCurrentUserId().Id; } //Log4NetHandler logger = new Log4NetHandler(); //logger.SendToLogger("RESTService", "ERROR", String.Format("RequestUrl: {0} User: {1}", context.Request.Url.ToString(), hangoutException.CurrentUserId), ex); // Handle exception through exception handling application block. //ExceptionPolicy.HandleException(hangoutException, "Global Policy"); //context.Response.StatusCode = 404; new XmlSerializer(typeof(RESTerror)).Serialize(context.Response.OutputStream, e); return; } // finally // { // AppDomain.Unload(appDomain); // } }
public void DoProcessRequest(HttpContext context, string verb) { try { // find the method using the verb name // TODO: add argIndex test to make sure it is argIndex static method MethodInfo methodInfo = typeof(T).GetMethod(verb); if (methodInfo == null) { throw new ArgumentException("Service verb could not be found: " + verb); } // loop though each parameter in the method and look for argIndex corresponding // query parameter in the REST request URL. string argString = ""; string delimiter = ""; int numArgs = methodInfo.GetParameters().Length; object[] args = new object[numArgs]; for (int argIndex = 0; argIndex < numArgs; argIndex++) { ParameterInfo parameterInfo = methodInfo.GetParameters()[argIndex]; if (parameterInfo.ParameterType == typeof(int)) { string arg = Util.GetHttpValue(parameterInfo.Name); int p = Int32.Parse(arg); args[argIndex] = p; argString += delimiter + parameterInfo.Name + "=" + Util.GetHttpValue(parameterInfo.Name); delimiter = "; "; } else if (parameterInfo.ParameterType == typeof(long)) { string arg = Util.GetHttpValue(parameterInfo.Name); long p = Int64.Parse(arg); args[argIndex] = p; argString += delimiter + parameterInfo.Name + "=" + Util.GetHttpValue(parameterInfo.Name); delimiter = "; "; } else if (parameterInfo.ParameterType == typeof(uint)) { string arg = Util.GetHttpValue(parameterInfo.Name); uint p = UInt32.Parse(arg); args[argIndex] = p; argString += delimiter + parameterInfo.Name + "=" + Util.GetHttpValue(parameterInfo.Name); delimiter = "; "; } else if (parameterInfo.ParameterType == typeof(string)) { args[argIndex] = Util.GetHttpValue(parameterInfo.Name); argString += delimiter + parameterInfo.Name + "=" + Util.GetHttpValue(parameterInfo.Name); delimiter = "; "; } else if (parameterInfo.ParameterType == typeof(HangoutPostedFile)) { HangoutPostedFile oFile = new HangoutPostedFile(); oFile.InitPostedFile(context.Request.Files[parameterInfo.Name]); args[argIndex] = oFile; } else if (parameterInfo.ParameterType == typeof(Session.UserId)) { Session.UserId userId; string stringUserId = Util.GetHttpValue("userId"); if (stringUserId == null) { userId = Session.GetCurrentUserId(); if (userId == null) { throw new UserNotLoggedin("No user session active and userId not provided."); } } else { int intUserId = System.Convert.ToInt32(stringUserId); userId = new Session.UserId(intUserId); } args[argIndex] = userId; } //else if (parameterInfo.ParameterType == typeof(RoomCatalog)) { // args[argIndex] = RestUrl.version; //} else if (parameterInfo.ParameterType == typeof(HangoutCookiesCollection)) { HangoutCookiesCollection oCookies = new HangoutCookiesCollection(); oCookies.InitCookies(context.Request.Cookies); args[argIndex] = oCookies; } else { throw new ArgumentException("unsupported argument type"); } } if (mDebugMode) { mServiceLog.Log.Debug("DoProcessRequest (Invoke Method) (" + methodInfo.ReflectedType.Name + ")(" + verb + ") args: " + argString); } //Log4NetHandler logger = new Log4NetHandler(); //logger.SendToLogger("RESTService", "INFO", String.Format("Noun: {0} Verb: {1}", methodInfo.ReflectedType.Name, verb)); T instance = Activator.CreateInstance <T>(); object reflectionObject = methodInfo.Invoke(instance, args); // refresh the session if it has one Session.Refresh(); // serialize the response based on the methods return type new XmlSerializer(methodInfo.ReturnType).Serialize(context.Response.OutputStream, reflectionObject); Globals.RemoveCurrentRestRequest(); if (mDebugMode) { mServiceLog.Log.Debug("---- Current RestService Requests: " + Globals.GetCurrentRestRequests); } return; } catch (System.Exception ex) { if (ex.GetType() == typeof(TargetInvocationException)) { ex = ex.InnerException; } bool debugMode = (ConfigurationManager.AppSettings["DebugMode"] == "true") ? true : false; HangoutException hangoutException = ex as HangoutException; if (hangoutException == null) { hangoutException = new HangoutException(ex); } RESTerror e = hangoutException.GetRestError(debugMode); // Add additional debug information hangoutException.RequestType = context.Request.RequestType; hangoutException.RequestUrl = context.Request.Url.ToString(); hangoutException.ClientIp = context.Request.UserHostAddress; hangoutException.TimeStamp = context.Timestamp; hangoutException.MachineName = Dns.GetHostName().ToString(); if (Session.GetCurrentUserId() != null) { hangoutException.CurrentUserId = Session.GetCurrentUserId().Id; } if (mDebugMode) { mServiceLog.Log.Debug("DoProcessRequest (HangoutException) (" + verb + "): " + "RequestType: " + hangoutException.RequestType + "RequestUrl: " + hangoutException.RequestUrl + "ClientIp: " + hangoutException.ClientIp + "TimeStamp: " + hangoutException.TimeStamp + "MachineName: " + hangoutException.MachineName ); } //Log4NetHandler logger = new Log4NetHandler(); //logger.SendToLogger("RESTService", "ERROR", String.Format("RequestUrl: {0} User: {1}", context.Request.Url.ToString(), hangoutException.CurrentUserId), ex); // Handle exception through exception handling application block. // TODO: lucas commented this out... eventually we'll figure out if this needs to be in here //ExceptionPolicy.HandleException( hangoutException, "Global Policy" ); context.Response.StatusCode = 500; if (mDebugMode) { mServiceLog.Log.Debug("DoProcessRequest (ErrorMessage) (" + verb + "): " + e.ErrorMessage); } new XmlSerializer(typeof(RESTerror)).Serialize(context.Response.OutputStream, e); Globals.RemoveCurrentRestRequest(); if (mDebugMode) { mServiceLog.Log.Debug("---- Current RestService Requests: " + Globals.GetCurrentRestRequests); } return; } }