Wcf concurreny模式不起作用 [英] Wcf concurreny mode not working
问题描述
我有WCF服务,它返回字符串列表中方法的开始和结束时间。我已将并发模式设置为多个并且InstanceContextMode = PerCall。当我从客户端应用程序使用await调用服务方法时,两个请求不会并行执行。请在下面找到服务代码和客户:
服务代码
I have WCF service which is returning start and end time of a method in list of string. I have set Concurrency mode to multiple and InstanceContextMode=PerCall. When i call the method of service using await from the client application, Both request are not executed in parallel. Please find the code of service and client below:
Service Code
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple,InstanceContextMode =InstanceContextMode.PerCall)]
public class Service1 : IService1
{
public List<string> GetData(int value)
{
List<string> lst = new List<string>();
DateTime dt1 = DateTime.Now;
lst.Add("Request :"+value+"---Start Time ----"+dt1.Hour + "_" + dt1.Minute + "_" + dt1.Second + "_" + dt1.Millisecond);
Thread.Sleep(2000);
dt1 = DateTime.Now;
lst.Add("Request :" + value + "---End Time ----"+dt1.Hour + "_" + dt1.Minute + "_" + dt1.Second + "_" + dt1.Millisecond);
return lst;
}
}
客户
Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestWCFConClient
{
public class MyClient
{
public async void Do()
{
List<Task> lsttask = new List<Task>();
DateTime dtf = DateTime.Now;
Console.WriteLine("Execution Begin " + dtf.Hour + "_" + dtf.Minute + dtf.Second + "_" + dtf.Millisecond);
// ThreadPool.SetMaxThreads(8,8);
//ThreadPool.SetMinThreads(8, 8);
//int c;
//int io;
//ThreadPool.GetMaxThreads(out c, out io);
List<string[]> lststr = new List<string[]>();
ServiceReference1.Service1Client objClient = new ServiceReference1.Service1Client();
for (int i = 0; i < 2; i++)
{
try
{
DateTime dtCurrent = DateTime.Now;
int current = i + 1;
var objTask = await Task<string[]>.Run(() =>
{
Task<string[]> obj = objClient.GetDataAsync(current);
for(int a=0;a<obj.Result.Length;a++ )
{
Console.WriteLine(current + obj.Result[a]);
}
return obj;
});
lststr.Add(objTask);
// Thread.Sleep(100);
}
catch (Exception ex)
{
}
}
DateTime dte = DateTime.Now;
Console.WriteLine("Execution End " + dte.Hour + "_" + dte.Minute + "_" + dte.Second + "_" + dte.Millisecond);
}
}
}
输出
Output
Execution Begin 22_5321_403
1Request :1---Start Time ----22_53_22_703
1Request :1---End Time ----22_53_24_704
2Request :2---Start Time ----22_53_24_798
2Request :2---End Time ----22_53_26_799
Execution End 22_53_26_810
我需要请求1和2都应该同时开始
我尝试了什么:
我已尝试过上述解决方案,但未给出预期结果
I need request 1 and 2 both should start at same time
What I have tried:
I have tried the above solution but it is not giving the expected result
推荐答案
Await说在我完成之前不要继续。在这些条件下你怎么期望循环?
使用Parallel.ForEach写一个简单的并行程序。 Microsoft Docs [ ^ ]
Await says "don't continue until I finish". How do you expect to "loop" under those conditions?
Write a simple parallel program using Parallel.ForEach | Microsoft Docs[^]
这篇关于Wcf concurreny模式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!