Wcf + ASP.NET +多线程
我有以下场景:
一个服务什么也不做,只是休眠 WPF 通过 WebRequest 对象指定的时间:
public class WorkerSvc : IWorkerSvc
{
#region IWorkerSvc Members
public void DoWork(int timeToSleep)
{
Trace.WriteLine(DateTime.Now.ToString() + "\tInside Stress Service with TimeToSleep: " + timeToSleep.ToString());
if (timeToSleep == 0)
return;
Thread.Sleep(timeToSleep * 1000);
Trace.WriteLine(DateTime.Now.ToString() + "\tThe Thread woke up.");
}
然后是一个调用该服务的 aspx 页面(单独的项目):
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tInside default stress web site page load;");
using (WorkerService.WorkerSvcClient client = new StressWebSite.WorkerService.WorkerSvcClient())
{
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tCreated a new client of Stress WCF Service;");
var rowdata = Request.QueryString["SleepValue"];
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tDetected Sleep Value in the Request: " + rowdata);
int realData = 0;
if (!string.IsNullOrEmpty(rowdata))
{
if (int.TryParse(rowdata, out realData))
{
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tBefore Dowork() with SleepValue: " + realData);
client.DoWork(realData);
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tAfter DoWork() with SleepValue: " + realData);
Response.Write(realData.ToString());
//Response.End();
}
}
}
}
一个跨多个线程的 WPF 表单,其中本质上将一些数据发布到 aspx 页面:
private void PerformStress()
{
WebRequest request = WebRequest.Create(string.Concat(this.txtStressPageUrl.Text, "?", "SleepValue", "=", this.txtSleepValue.Text));
Trace.WriteLine("Created Web Request");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
//return responseFromServer;
}
private void button3_Click(object sender, RoutedEventArgs e)
{
int stressThreadsCount = 0;
if (!int.TryParse(this.txtStressThreadCount.Text, out stressThreadsCount))
MessageBox.Show("Enter number of threads to stress");
ThreadStart start = delegate()
{
DispatcherOperation op = Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(PerformStress));
DispatcherOperationStatus status = op.Status;
while (status != DispatcherOperationStatus.Completed)
{
status = op.Wait(TimeSpan.FromMilliseconds(1000));
if (status == DispatcherOperationStatus.Aborted)
{
//
}
}
};
for (int i = 0; i < stressThreadsCount; i++)
{
var t = new Thread(start);
//this.runningThreads.Add(t);
t.Start();
//t.Join();
}
}
问题是,尽管我从 WPF 端生成多个线程来发布到 aspx 页面,但所有线程都以串行方式处理,这意味着每个线程都在等待服务返回。
我在 cassiny 下运行这个设置。
我没有将WCF 服务的行为指定为单例。 请帮助识别问题 - 我想模拟对 IIS 的多个请求以及调用 WCF 的页面来证明一个概念。
I have the following scenario:
A Service that does nothing but sleeps for the amount of time the WPF specifies through the WebRequest onject:
public class WorkerSvc : IWorkerSvc
{
#region IWorkerSvc Members
public void DoWork(int timeToSleep)
{
Trace.WriteLine(DateTime.Now.ToString() + "\tInside Stress Service with TimeToSleep: " + timeToSleep.ToString());
if (timeToSleep == 0)
return;
Thread.Sleep(timeToSleep * 1000);
Trace.WriteLine(DateTime.Now.ToString() + "\tThe Thread woke up.");
}
Then an aspx page (separate project) that calls into the service:
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tInside default stress web site page load;");
using (WorkerService.WorkerSvcClient client = new StressWebSite.WorkerService.WorkerSvcClient())
{
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tCreated a new client of Stress WCF Service;");
var rowdata = Request.QueryString["SleepValue"];
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tDetected Sleep Value in the Request: " + rowdata);
int realData = 0;
if (!string.IsNullOrEmpty(rowdata))
{
if (int.TryParse(rowdata, out realData))
{
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tBefore Dowork() with SleepValue: " + realData);
client.DoWork(realData);
System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tAfter DoWork() with SleepValue: " + realData);
Response.Write(realData.ToString());
//Response.End();
}
}
}
}
A WPF Form That spanws a number of threads which essentially post some data to an aspx page:
private void PerformStress()
{
WebRequest request = WebRequest.Create(string.Concat(this.txtStressPageUrl.Text, "?", "SleepValue", "=", this.txtSleepValue.Text));
Trace.WriteLine("Created Web Request");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
//return responseFromServer;
}
private void button3_Click(object sender, RoutedEventArgs e)
{
int stressThreadsCount = 0;
if (!int.TryParse(this.txtStressThreadCount.Text, out stressThreadsCount))
MessageBox.Show("Enter number of threads to stress");
ThreadStart start = delegate()
{
DispatcherOperation op = Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(PerformStress));
DispatcherOperationStatus status = op.Status;
while (status != DispatcherOperationStatus.Completed)
{
status = op.Wait(TimeSpan.FromMilliseconds(1000));
if (status == DispatcherOperationStatus.Aborted)
{
//
}
}
};
for (int i = 0; i < stressThreadsCount; i++)
{
var t = new Thread(start);
//this.runningThreads.Add(t);
t.Start();
//t.Join();
}
}
The problem is that it looks like eventhough I spawn several threads from the WPF side, to post to the aspx page, all the threads are processed in a serial manner, meaning each thread is waiting for the service to return.
I run this set up under cassiny.
I did not specify the WCF service's behavior to be a singleton.
Please help to ID the issue - I'd like to simulate multiple requests to the IIS and a page that calls into WCF to prove a concept.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Singleton 只会影响为服务请求而创建的对象数量。但默认情况下,WCF 服务一次仅处理一个请求。要改变这一点,您应该使用以下内容来装饰您的服务实现
。有关详细信息,请参阅此处: http://msdn.microsoft.com/en-us/library/system.servicemodel.concurrencymode.aspx
Singleton will only affect how many objects will be created to service requests. By default, though, WCF services will only process a single request at a time. To change that, you should decorate your service implementation with
For more info, see here: http://msdn.microsoft.com/en-us/library/system.servicemodel.concurrencymode.aspx