Regarding my previous post: 910220 - service methods run dependently 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:
#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:
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.