locked
silverlight-enabled wcf service load data RRS feed

  • Question

  • I have class Cube:

     public class Cube
        {
         
            string MaNha;
    
            public List<RectEntities> ListRect = new List<RectEntities>();
            public RectEntities rectMax = new RectEntities(); // dùng để làm nền nhà
            RectEntities rectEntities;
            Service1Client _data;
    
            float _lenght = 6.0f;
            float _long = 10.0f;
            float _height = 5.0f;
    
            public Cube()
            {
                MaNha = "01";
                GetRect();
    
            }
            public float Long
            {
                get { return _long; }
                set { _long = value; }
            }
            public float Lenght
            {
                get { return _lenght; }
                set { _lenght = value; }
            }
            public float Height
            {
                get { return _height; }
                set { _height = value; }
            }
    
    
            public void GetRect()
            {
                //BasicHttpBinding bind = new BasicHttpBinding();
                //EndpointAddress endpoint = new EndpointAddress("http://localhost:20379/Service1.svc");
               
                ListRect.Clear();
                _data = new Service1Client();//(bind, endpoint);
    
                _data.GetRectCompleted += new EventHandler<GetRectCompletedEventArgs>(_data_GetRectCompleted);
                
                _data.GetRectAsync(MaNha);
            }
            public void _data_GetRectCompleted(object sender, GetRectCompletedEventArgs e)
            {
               
    
                foreach (var x in e.Result)
                {
                    rectEntities = new RectEntities();
                    rectEntities.NhaID = x.HinhChuNhatID.ToString();
                    rectEntities.MaNha = x.MaNha;
                    rectEntities.x1 = (double)x.X1;
                    rectEntities.y1 = (double)x.Y1;
                    rectEntities.width = 10;//((double)x.Width)/100;
                    rectEntities.lenght = 10;//((double)x.Lenght)/100;
    
                    ListRect.Add(rectEntities);
                }
               // MaxRect();
            }

        public class Scene
        {
       
       .....
        Cube cube = new Cube();
       public Scene()
            {
                           
                count = cube.ListRect.Count;
            }
    }
    
    
    But cube.ListRect.Count = 0. Why?
    when I debug, it create cube = new Cube(), but it not run  method
    _data_GetRectCompleted(object sender, GetRectCompletedEventArgs e)
    Please help me!

     

    Friday, August 17, 2012 10:55 AM

Answers

  • But cube.ListRect.Count = 0. Why?

    because the time you are trying to get the list of rectangles, is not getting filled. the list of rectangles is empty, it will take time to fill the list becoz the items will get filled from webservice.

    public class Cube
        {
         
            string MaNha;
    
            public List<RectEntities> ListRect = new List<RectEntities>();
            public RectEntities rectMax = new RectEntities(); // dùng d? làm n?n nhà
            RectEntities rectEntities;
            Service1Client _data;
    
            float _lenght = 6.0f;
            float _long = 10.0f;
            float _height = 5.0f;
          
            public float Long
            {
                get { return _long; }
                set { _long = value; }
            }
            public float Lenght
            {
                get { return _lenght; }
                set { _lenght = value; }
            }
            public float Height
            {
                get { return _height; }
                set { _height = value; }
            }
    }
            
        public class Scene
        {      
        Cube cube = new Cube();
       public Scene()
            {
    		 MaNha = "01";
                             _data = new Service1Client();//(bind, endpoint);
    
                _data.GetRectCompleted += new EventHandler<GetRectCompletedEventArgs>(_data_GetRectCompleted);
                
                _data.GetRectAsync(MaNha);
                
            }
    		string MaNha;
    		public void _data_GetRectCompleted(object sender, GetRectCompletedEventArgs e)
            {          
                foreach (var x in e.Result)
                {
                    rectEntities = new RectEntities();
                    rectEntities.NhaID = x.HinhChuNhatID.ToString();
                    rectEntities.MaNha = x.MaNha;
                    rectEntities.x1 = (double)x.X1;
                    rectEntities.y1 = (double)x.Y1;
                    rectEntities.width = 10;//((double)x.Width)/100;
                    rectEntities.lenght = 10;//((double)x.Lenght)/100;
    
                    ListRect.Add(rectEntities);
                }
               
            }
    }
    

    I made changes in your code, follow this you get the output in scene class only.

    Friday, August 17, 2012 11:53 AM
  • is there a way to get data directly when call _data.GetRectAsync(MaNha);

    Actually Each and every call from Silverlight is Asynchronous, asynchronous calls will get the data in completed event.

    if you want the data directly, then i suggest you to go with synchronous calls, follow the following link.

    http://www.codeproject.com/Articles/91528/How-to-Call-WCF-Services-Synchronously-and-Asynchr

    http://johnleitch.blogspot.in/2010/03/easy-way-to-synchronously-call-wcf.html

    Saturday, August 18, 2012 3:08 AM

All replies

  • But cube.ListRect.Count = 0. Why?

    because the time you are trying to get the list of rectangles, is not getting filled. the list of rectangles is empty, it will take time to fill the list becoz the items will get filled from webservice.

    public class Cube
        {
         
            string MaNha;
    
            public List<RectEntities> ListRect = new List<RectEntities>();
            public RectEntities rectMax = new RectEntities(); // dùng d? làm n?n nhà
            RectEntities rectEntities;
            Service1Client _data;
    
            float _lenght = 6.0f;
            float _long = 10.0f;
            float _height = 5.0f;
          
            public float Long
            {
                get { return _long; }
                set { _long = value; }
            }
            public float Lenght
            {
                get { return _lenght; }
                set { _lenght = value; }
            }
            public float Height
            {
                get { return _height; }
                set { _height = value; }
            }
    }
            
        public class Scene
        {      
        Cube cube = new Cube();
       public Scene()
            {
    		 MaNha = "01";
                             _data = new Service1Client();//(bind, endpoint);
    
                _data.GetRectCompleted += new EventHandler<GetRectCompletedEventArgs>(_data_GetRectCompleted);
                
                _data.GetRectAsync(MaNha);
                
            }
    		string MaNha;
    		public void _data_GetRectCompleted(object sender, GetRectCompletedEventArgs e)
            {          
                foreach (var x in e.Result)
                {
                    rectEntities = new RectEntities();
                    rectEntities.NhaID = x.HinhChuNhatID.ToString();
                    rectEntities.MaNha = x.MaNha;
                    rectEntities.x1 = (double)x.X1;
                    rectEntities.y1 = (double)x.Y1;
                    rectEntities.width = 10;//((double)x.Width)/100;
                    rectEntities.lenght = 10;//((double)x.Lenght)/100;
    
                    ListRect.Add(rectEntities);
                }
               
            }
    }
    

    I made changes in your code, follow this you get the output in scene class only.

    Friday, August 17, 2012 11:53 AM
  • Thanks syed amjad!

    is there a way to get data directly when call _data.GetRectAsync(MaNha);

    I relize, using silverlight_enable WCF service  run all method before execute method 

    public void _data_GetRectCompleted(object sender, GetRectCompletedEventArgs e)
    {
    .... 
    }         
    Friday, August 17, 2012 11:24 PM
  • is there a way to get data directly when call _data.GetRectAsync(MaNha);

    Actually Each and every call from Silverlight is Asynchronous, asynchronous calls will get the data in completed event.

    if you want the data directly, then i suggest you to go with synchronous calls, follow the following link.

    http://www.codeproject.com/Articles/91528/How-to-Call-WCF-Services-Synchronously-and-Asynchr

    http://johnleitch.blogspot.in/2010/03/easy-way-to-synchronously-call-wcf.html

    Saturday, August 18, 2012 3:08 AM