Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
in the following code behind:
C#
namespace ResizeTester
{
        public partial class MainPage : UserControl
        {
                public MainPage()
                {
                        InitializeComponent();
                        _sr.ResizeCompleted += OnResizeCompleted;
                }

                private Service1Client _sr = new Service1Client();
                private int _counter = 0;
                private bool[] _sent = new bool[100000];

                private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
                {
                        var newSize = new ServiceReference1.Size();
                        newSize.width = (int)e.NewSize.Width;
                        newSize.height = (int)e.NewSize.Height;
                        _sr.ResizeAsync(_counter, newSize, _counter);
                        _counter++;
                }

                void OnResizeCompleted(object sender, AsyncCompletedEventArgs e)
                {
                        if (e.Error == null)
                        {
                                int counter = (int) e.UserState;
                                Debug.Assert(_sent[counter] == false);
                                _sent[counter] = true;
                        }
                        else
                        {
                                throw new Exception(string.Format("Resize Error {0}", e.Error));
                        }
                }
        }
}

and following code at server side:
C#
namespace ResizeTester.Web
{
        [ServiceContract(Namespace = "")]
        [SilverlightFaultBehavior]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class Service1
        {
                private static bool[] _received = new bool[100000];

                object _logLock = new object();

                void log(string s)
                {
                        const string logFileName = @"h:\ResizeTester.log";
                        lock (_logLock)
                        {
                                var streamWriter = new StreamWriter(logFileName, true, Encoding.ASCII);
                                streamWriter.Write(s);
                                streamWriter.Close();
                        }
                }

                [OperationContract]
                public void Resize(int counter, Size newSize)
                {
                        log(string.Format("{0}\t{1}x{2}\r\n", counter, newSize.Width, newSize.Height));
                        Debug.Assert(_received[counter] == false);
                        _received[counter] = true;
                        Thread.Sleep(3000);
                }
        }
}

i get ResizeError exception.
why?!
------------------
i thought the above code and question should be enough and no additional info might be required. but since Sandeep Mewara asked me to describe and indicated that it's not enough i've to explain more about it. u should better asked me which part needs clarification.
it's a Silverlight project using WCF RIA Service. in the first section, you see the XAML's code behind which locates at client side. in the constructor i've added OnResizeCompleted method to the ResizeCompleted delegate of the proxy. i call Resize method of the service asynchronously whenever size of user control changes. all size changes are counted and the counter is sent to the service. at client and server these size changes are saved, so that we may know which one is sent successfully to the server and which one is missed. i expect none to get missed.
Resize at server takes 3 seconds to accomplish. i expect all resizes reach server at the same time and a resize doesn't get delayed because previous resizes are not still accomplished at server since calls are asynchronous. but in OnResizeCompleted, e.Error is not always null. why?
i think the code is sufficiently clear. if it's still not clarified ask me the questions u may have about it to describe more.
thx

UPDATE from OP (put in by EdMan196 after OP reposted):


since the code and what i meant about the problem i had with it was a bit complex, i come again with a completely changed code which explains itself better. in client side, we have:
C#
#define USE_ONLY_ONE_INSTANCE
 
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using CountTester.ServiceReference1;
 
namespace CountTester
{
	public partial class MainPage : UserControl
	{
#if USE_ONLY_ONE_INSTANCE
		private readonly Service1Client _sc = new Service1Client();
#endif
 
		public MainPage()
		{
			InitializeComponent();
#if USE_ONLY_ONE_INSTANCE
			_sc.CountCompleted += OnCountCompleted;
#endif
		}
 
		void OnCountCompleted(object sender, AsyncCompletedEventArgs e)
		{
			if (e.Error != null)
				throw new Exception(string.Format("Count Error {0}", e.Error));
		}
 
		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{
			for (int i = 0; i < 100; i++)
			{
#if USE_ONLY_ONE_INSTANCE
				_sc.CountAsync(i);
#else
				var sc = new Service1Client();
				sc.CountCompleted += OnCountCompleted;
				sc.CountAsync(i);
				//sc.CloseAsync();
#endif
			}
		}
	}
}

this is code behind of the XAML. in the code i call a service method 100 times. i tried both cases and get exception in both cases:
case 1: i use only one instance of the proxy for all communications with server.
case 2: i use an instance for each communication with server.
let's see the code at server before more description:

C#
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using System.Threading;
 
namespace CountTester.Web
{
    [ServiceContract(Namespace = "")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        const string logFileName = @"h:\CountTester.log";
        object _logLock = new object();
 
        void log(string s)
        {
            lock (_logLock)
            {
                var streamWriter = new StreamWriter(logFileName, true, Encoding.ASCII);
                streamWriter.Write(s);
                streamWriter.Close();
            }
        }
 
        Service1()
        {
            //File.Delete(logFileName);
        }
 
        [OperationContract]
        public void Count(int counter)
        {
            log(string.Format("{0}\n", counter));
            Thread.Sleep(3000);
        }
    }
}

Count is the service method which is called. i deliberately wait 3 seconds in the method. what i expect is that the for at the client get accomplished with no delay. i expect the method gets called asynchronously and this means that first call doesn't affect on the second call. in another words, if i call the method and it waits to get accomplished, calling it again doesn't get delayed. while this is the case. how can i figure out that this happens? by using Tail for Windows, i find that the number which is logged is delayed in getting logged. i also figure out this when i see that i get time out exception in response to calling the service method (Count Error...). i hope i could clarify my question.
i also wonder when i see malfunctioning of the program (Exception), when i uncomment the line in which i've closed the service.
please answer these two questions.
thx
Posted
Updated 10-May-12 10:15am
v3
Comments
Sandeep Mewara 9-May-12 10:55am    
We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
Ed Nutting 10-May-12 16:16pm    
OP reposted - I have updated the question above to include his new post (and deleted the new one to save confusion.)
Ed :)
ilostmyid2 12-May-12 2:28am    
ok, thank u :)
indeed the question is answered here:
http://stackoverflow.com/questions/10532904/910221-it-seems-that-service-method-are-not-really-called-asynchronously/10534028#10534028
it's the limitation IIS causes. i would like to know whether linux has the same behavior.
ilostmyid2 10-May-12 2:57am    
ok, for your ease, i changed the code a bit. you may look here:
http://www.codeproject.com/Questions/382077/910221-service-methods-run-dependently

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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