C#中的WCF多线程调用
Posted
技术标签:
【中文标题】C#中的WCF多线程调用【英文标题】:WCF multithread call in c# 【发布时间】:2018-10-18 08:19:45 【问题描述】:我正在尝试调用WCF
服务,您可以在此处看到:
[ServiceBehavior(UseSynchronizationContext = false,ConcurrencyMode =ConcurrencyMode.Multiple)]
public class InquiryService : IInquiryService
private DataContext _ctx;
private IUserService _userService;
public InquiryService(DataContext ctx, INajaService najaService, IUserService userService)
_ctx = ctx;
_userService = userService;
public Inquiryview Inquiry(string VisitDatetime, string Plaque)
My service !!!!!
WCFwebconfig
:
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000" />
这是我调用服务的多线程程序:
private async void btnStart_Click(object sender, RoutedEventArgs e)
btnStart.Content = "Please Wait...";
btnStart.IsEnabled = false;
await Task.Run(() =>
InquiryService service = new InquiryService();
FinalList = new List<FinalData>();
var items = Data.GetData(from, to).AsEnumerable();
start = DateTime.Now;
Parallel.ForEach(items, new ParallelOptions MaxDegreeOfParallelism = threadCount , item =>
string plaque = Plaque.GeneratePlaque(item.palll);
var result = service.Inquiry(plaque);
string serializedResult = new javascriptSerializer().Serialize(result);
FinalData finalData = new FinalData()
date = result.StartDate,
enddate = result.EndDate,
log = serializedResult,
plaque = plaque
;
lock (FinalList)
FinalList.Add(finalData);
lock (lblCount)
Dispatcher.Invoke(() =>
lblCount.Content = FinalList.Count;
);
plaque = null;
);
end = DateTime.Now;
duration = end - start;
Dispatcher.Invoke(() =>
btnStart.Content = "Insert Data...";
);
Data.InsertData(FinalList);
);
btnStart.Content = "Start";
btnStart.IsEnabled = true;
MessageBox.Show("Service Response Time = " + duration.ToString());
调用服务的类:
私有 MemoryStream 毫秒; 私有静态只读字符串 ServiceHostName = "https://****.muservice.ir/InquiryService.svc";
public InquiryService()
ms = new MemoryStream();
public Result Inquiry(string Plaque)
string result_ = "";
ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => return true; ;
DateTime start;
DateTime end;
using (var ClientRequest = new WebClient())
ClientRequest.Headers["Content-type"] = "application/json";
ClientRequest.Encoding = Encoding.UTF8;
NetworkCredential credential1 = new NetworkCredential("1", "1");
ClientRequest.Credentials = credential1;
start = DateTime.Now;
result_ = ClientRequest.
(ServiceHostName + "/Inquiry/2012-12-28" + "/" + Plaque);
end = DateTime.Now;
var javascriptserializer = new JavaScriptSerializer();
Result temp = javascriptserializer.Deserialize<Result>(result_);
temp.StartDate = start;
temp.EndDate = end;
return temp;
运行程序后正确调用服务(大约 100 毫秒),但在大约 1000 次请求后,我的请求时间增长到(8 秒)。 我想知道我应该改变什么吗?
【问题讨论】:
可能之前的 1000 个请求仍在处理中,所以下一个需要等待,直到前一千个请求中的至少一个完成。 您是否在 IIS 上托管?使用 Profiler 看看发生了什么,可能在 1000 个请求之后积累了大量未完成的请求,管道不太可能真正能够在没有任何序列化/排队的情况下并行执行 1000 个。 @dlatikay 是的,我正在使用 IIS @dlatikay 那么我应该更改任何配置设置吗?在 wcf 中 @danish 那我该怎么办? 【参考方案1】:试试这个服务行为
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple)]
建议使用 PerCall 实例以实现可扩展性。
【讨论】:
【参考方案2】:最后将此代码添加到客户端的配置文件中。因为windows有出站流量限制
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="65456”/>
</connectionManagement>
</system.net>
</configuration>
【讨论】:
以上是关于C#中的WCF多线程调用的主要内容,如果未能解决你的问题,请参考以下文章