Hi I had create a project with Json ... Everything that cames from webservice is converted to json but the time of response is very slow when i select some item from the menu.
Anyone knows how to reduce the time?
Regards.
Code of Default.aspx page:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZonMyAval.MyAval;
namespace ZonMyAval
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<guid> businesses = null, grades = null, components = null;
HiddenField HF_SelectedBusinesses = (HiddenField)Form.Parent.FindControl("HF_SelectedBusinesses");
HiddenField HF_SelectedGrades = (HiddenField)Form.Parent.FindControl("HF_SelectedGrades");
HiddenField HF_SelectedComponents = (HiddenField)Form.Parent.FindControl("HF_SelectedComponents");
HiddenField HF_SelectedPeriod = (HiddenField)Form.Parent.FindControl("HF_SelectedPeriod");
Period period = string.IsNullOrEmpty(HF_SelectedPeriod.Value) ? Period.Daily : (Period)System.Enum.Parse(typeof(Period), HF_SelectedPeriod.Value);
if (!string.IsNullOrEmpty(HF_SelectedBusinesses.Value))
{
businesses = new List<guid>();
foreach (string business in HF_SelectedBusinesses.Value.Split('|'))
businesses.Add(new Guid(business));
}
if (!string.IsNullOrEmpty(HF_SelectedGrades.Value))
{
grades = new List<guid>();
foreach (string grade in HF_SelectedGrades.Value.Split('|'))
grades.Add(new Guid(grade));
}
if (!string.IsNullOrEmpty(HF_SelectedComponents.Value))
{
components = new List<guid>();
foreach (string component in HF_SelectedComponents.Value.Split('|'))
components.Add(new Guid(component));
}
ConvertToJSON("Sales", "CreateSalesArray", Context.User.Identity.Name, businesses, grades, components, period);
ConvertToJSON("Metrics", "CreateMetricsArray", Context.User.Identity.Name, businesses, grades, components, null);
if (IsPostBack)
ScriptManager.RegisterStartupScript(this, this.GetType(), "Refresh", "RefreshData();", true);
}
private static List<getsales_result> CreateMetricsArray(string user, List<guid> businesses, List<guid> grades, List<guid> components)
{
DateTime searchDate = DateTime.Parse(ConfigurationManager.AppSettings["SearchDate"]);
MyAval.ZonMyAval_DAL service = new MyAval.ZonMyAval_DAL();
List<getsales_result> _Sales = new List<getsales_result>();
_Sales = service.GetSales(user, Hierarchy.Organizational, Period.Monthly, searchDate, businesses == null ? null : businesses.ToArray(), grades == null ? null : grades.ToArray(), components == null ? null : components.ToArray()).ToList();
GetSales_Result lastYearMonth = service.GetSales(user, Hierarchy.Organizational, Period.Monthly, searchDate.AddYears(-1), businesses == null ? null : businesses.ToArray(), grades == null ? null : grades.ToArray(), components == null ? null : components.ToArray()).OrderByDescending(o => o.resultorder).FirstOrDefault();
if (lastYearMonth != null)
_Sales.Add(lastYearMonth);
foreach (var sale in _Sales)
sale.data = string.Format("{0} {1}", sale.data, sale.resultorder.Value / 100).ToUpper();
return _Sales.OrderByDescending(o => o.resultorder).ToList();
}
private static List<myaval.getsalesbynode_result> CreateSalesArray(string user, List<guid> businesses, List<guid> grades, List<guid> components, Period period)
{
MyAval.ZonMyAval_DAL soap = new MyAval.ZonMyAval_DAL();
List<myaval.getsalesbynode_result> _Sales = new List<myaval.getsalesbynode_result>();
_Sales = soap.GetSalesByNode(user, Hierarchy.Organizational, period, new DateTime(2012, 06, 20), businesses == null ? null : businesses.ToArray(), grades == null ? null : grades.ToArray(), components == null ? null : components.ToArray()).ToList();
return _Sales;
}
public void ConvertToJSON(string Var, string Method, string user,List<guid> businesses, List<guid> grades, List<guid> components, Period? period)
{
Random r = new Random();
MethodInfo methodInfo = typeof(Default).GetMethod(Method, System.Reflection.BindingFlags.Static | BindingFlags.NonPublic);
object returnValue;
if (methodInfo.GetParameters().Length == 4)
returnValue = methodInfo.Invoke(null, new object[] { user, businesses, grades, components });
else if (methodInfo.GetParameters().Length > 4)
returnValue = methodInfo.Invoke(null, new object[] { user, businesses, grades, components, period.Value });
else
returnValue = methodInfo.Invoke(null, new object[] { });
JavaScriptSerializer jss1 = new JavaScriptSerializer();
string _myJSONstring = jss1.Serialize(returnValue);
string docs = "var " + Var + "=" + _myJSONstring + ";";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Var + r.Next(5000), docs, true);
}
}
}</guid></guid></guid></myaval.getsalesbynode_result></myaval.getsalesbynode_result></guid></guid></guid></myaval.getsalesbynode_result></getsales_result></getsales_result></guid></guid></guid></getsales_result></guid></guid></guid></guid>