Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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);

            //TEST

            //_Sales.Add(new GetSales_Result
            //{
            //    VendasLiquidas = 14090,
            //    VendasBrutas = 19500,
            //    Ativacoes = 18000,
            //    Pendentes = 2000,
            //    resultorder = 201204,
            //    data = "Abril"
            //});

            //_Sales.Add(new GetSales_Result
            //{
            //    VendasLiquidas = 15700,
            //    VendasBrutas = 21070,
            //    Ativacoes = 18200,
            //    Pendentes = 3000,
            //    resultorder = 201203,
            //    data = "Março"
            //});

            //_Sales.Add(new GetSales_Result
            //{
            //    VendasLiquidas = 17700,
            //    VendasBrutas = 20070,
            //    Ativacoes = 16020,
            //    Pendentes = 2500,
            //    resultorder = 201202,
            //    data = "Fevereiro"
            //});

            //_Sales.Insert(_Sales.Count - 1, new GetSales_Result
            //{
            //    VendasLiquidas = 23090,
            //    VendasBrutas = 18700,
            //    Ativacoes = 18080,
            //    Pendentes = 1400,
            //    resultorder = 201106,
            //    data = "Junho"
            //});

            //END TEST

            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 + ";";

            //Page.ClientScript.RegisterClientScriptBlock(typeof(UpdatePanel), Var + r.Next(5000), docs, true);
            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>
Posted
Updated 4-Oct-12 0:52am
v3
Comments
Dylan Morley 4-Oct-12 6:16am    
No one has any idea when the webservice is doing. Is it a simple operation, or complex with database operations etc.

Perhaps post the code for the method that is running slowly, or give some sort of indiciation what it's doing. Otherwise, there's not enough information
hh_7 4-Oct-12 6:37am    
Ok sorry i will post some code...

1 solution

JSON data is best used in Javascript, not in C# code. As per your code you are doing a really big string crunching operation, and that's unavoidably reducing the performance. Try to use the JSON string in javascript and see the difference.
 
Share this answer
 
Comments
hh_7 8-Oct-12 5:19am    
Thanks works a lot better!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900