我正在尝试拨打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 !!!!!
}
}
WCF webconfig
:
<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());
}
调用服务的类:
private MemoryStream ms; private static readonly string 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秒)。 我想知道我应该改变什么吗?
答案 0 :(得分:0)
尝试此服务行为
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple)]
建议使用PerCall实例以实现可伸缩性。
答案 1 :(得分:0)
最后在客户端的配置文件中添加此代码。因为Windows有出站流量限制
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="65456”/>
</connectionManagement>
</system.net>
</configuration>