public void ProcessRequest(HttpContext context) { Stopwatch sw = new Stopwatch(); sw.Start(); this.context = context; if (context.Request["apiType"] == "xml") apiType = "xml"; string clientIPAddress = getIPAddress(); string data = "", method = "", clientName = ""; object req = null; try { if (!clientIPAddress.StartsWith("93.89.226") && !ConfigurationManager.AppSettings["allowedIPs"].Contains(clientIPAddress)) throw new Exception("Access denied for " + context.Request.UserHostAddress); if (apiType == "xml") context.Response.ContentType = "application/xml"; else context.Response.ContentType = "application/json"; method = context.Request["method"]; if (string.IsNullOrWhiteSpace(method)) throw new Exception("Service request method needed"); rfl.MethodInfo mi = this.GetType().GetMethod(method); if (mi == null) throw new Exception("There is no service method with the name " + method); if (mi.GetParameters().Length != 1) throw new Exception("A service request method should have only one parameter"); data = context.Request["data"]; if (string.IsNullOrWhiteSpace(data)) throw new Exception("Service request data needed"); data = data.Replace("%2B", "+"); Type t = getServiceRequestType(mi.GetParameters()[0].ParameterType); req = deserialize(data, t); this.Application = Provider.Database.Read<Application>("Key={0}", req.GetMemberValue("APIKey")); if(this.Application==null) throw new Exception("No such application"); this.Reseller = Provider.Database.Read<Reseller>("Id={0}", req.GetMemberValue("ResellerId")); if (this.Reseller == null) throw new Exception("No such reseller"); var client = req.GetMemberValue("Client"); clientName = client == null ? "" : client.ToString(); this.Session = Provider.Database.Read<APISession>("Token={0}", req.GetMemberValue("SessionId")); if (this.Session == null) createSession(); //TODO: session timeout'u prametrik yapabiliriz. if (this.Session.LastAccess.AddMinutes(30) < DateTime.Now) throw new APIException("Session timeout"); this.Session.LastAccess = DateTime.Now; this.Session.Save(); object res = mi.Invoke(this, new[] { req.GetMemberValue("Data") }); t = getServiceResponseType(mi.ReturnType); object serviceResponse = Activator.CreateInstance(t); serviceResponse.SetMemberValue("Data", res); serviceResponse.SetMemberValue("IsSuccessful", true); serviceResponse.SetMemberValue("ClientIPAddress", clientIPAddress); sw.Stop(); serviceResponse.SetMemberValue("ServerProcessTime", sw.ElapsedMilliseconds); context.Response.Write(serialize(serviceResponse, apiType)); } catch (Exception ex) { sw.Stop(); if (ex.InnerException is APIException) { var exInner = ex.InnerException as APIException; context.Response.Write(serialize(new ServiceResponse<object> { Data = null, IsSuccessful = false, ErrorMessage = exInner.Message, ErrorType = (int)exInner.ErrorType, ErrorCode = (int)exInner.ErrorCode, ClientIPAddress = clientIPAddress, ServerProcessTime = sw.ElapsedMilliseconds }, apiType)); } else { context.Response.Write(serialize(new ServiceResponse<object> { Data = null, IsSuccessful = false, ErrorMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message, ErrorCode = 500, ClientIPAddress = clientIPAddress, ServerProcessTime = sw.ElapsedMilliseconds }, apiType)); } } }
private void createSession() { this.Session = new APISession() { LastAccess = DateTime.Now, Token = Utility.MD5("MA"+DateTime.Now.Ticks.ToString()) }; this.Session.Save(); }