首次调用 C# Web 服务速度缓慢
我正在尝试访问此处提供的 PubMed Web 服务:
我用 java 编写了代码来访问 Web 服务,返回时间约为不到1秒。我用 C# 编写代码来访问相同的 Web 服务,初始调用的返回时间约为 12 秒,所有后续调用的返回时间不到 1 秒。
我尝试以两种方式用 C# 写入 Web 服务 - 都作为控制台应用程序。首先是标准的“右键单击引用并执行‘添加服务引用’”,它将向 app.config 添加信息,您可以使调用变得轻松愉快。第二种是使用 wsdl.exe 创建 dll 并尽可能“直接”访问 Web 服务(无向导)。两种方法都提供相同的结果。我将发布两个各自的代码片段。
1) (来自添加服务引用向导)
http://www.ncbi.nlm.nih.gov/entrez/eutils/soap/v2.0/efetch_pubmed.wsdl (as Namespace: PubMedWebServiceEfetch_pubMed)
(在代码中)
Stopwatch sw = new Stopwatch();
PubMedWebServiceEfetch_pubMed.eUtilsServiceSoapClient server = new PubMedWebServiceEfetch_pubMed.eUtilsServiceSoapClient();
try
{
PubMedWebServiceEfetch_pubMed.eFetchRequest searchRequest = new PubMedWebServiceEfetch_pubMed.eFetchRequest();
searchRequest.id = "11850928";
Console.WriteLine("Run server.run_eFetch(theRequest). [Reset stopwatch]");
sw.Restart();
PubMedWebServiceEfetch_pubMed.eFetchResult searchResult = server.run_eFetch(searchRequest);
Console.WriteLine(searchResult.Count() + " - elapsed milliseconds = " + sw.ElapsedMilliseconds);
sw.Stop();
}
catch (Exception e1) { Console.WriteLine(e1); }
finally { server.Dispose(); }
2) (从命令行)
wsdl /out:myProxyClassPubMed.cs http://eutils.ncbi.nlm.nih.gov/soap/v2.0/efetch_pubmed.wsdl
csc /t:library MyProxyClassPubMed.cs
(将 dll 添加到控制台应用程序)
Stopwatch sw = new Stopwatch();
eFetchPubmedService service = new eFetchPubmedService();
try
{
eFetchRequest theRequest = new eFetchRequest();
theRequest.id = "11850928";
Console.WriteLine("Run service.run_eFetch(theRequest). [Reset stopwatch]");
sw.Restart();
eFetchResult searchResult = service.run_eFetch(theRequest);
Console.WriteLine(searchResult.Count() + " - elapsed milliseconds = " + sw.ElapsedMilliseconds);
sw.Stop();
}
catch (Exception e1) { Console.WriteLine(e1); }
finally { service.Dispose(); }
经过多次搜索,我发现您应该能够使用 sgen 创建 XML 序列化器。我运行:
sgen /a:myProxyClassPubMed.dll /f
这创建了一个 dll myProxyClassPubMed.XmlSerializers.dll,然后我将其添加为第二个连接类型中的引用。
我还搞乱了应用程序构建区域中的“生成序列化程序集”选项,但没有发现任何改进。
我想通过 ASP.NET 页面进行这些 Web 服务调用,因此第一次调用的 12 秒返回时间是不可接受的。
我考虑过将其发布在 BioStar 上,但参加人数不如该论坛。如果在这里找不到答案,我可能会这样做。
有什么想法吗?
I am trying to access the PubMed web services as provided here:
http://www.ncbi.nlm.nih.gov/entrez/eutils/soap/v2.0/DOC/esoap_help.html
I wrote code in java to access the web service and the return times were on the order of less than 1 second. I wrote code in C# to access the same web service and the return times were on the order of 12 seconds for the initial call and then less than 1 second for all subsequent calls.
I have tried to write to the web service in C# in two ways - both as console applications. First was the standard "right click on references and do 'Add Service Reference'" which will add information to app.config and you can make the calls nice and easy. The second was to use wsdl.exe to create a dll and access the web service as "directly" as possible (no wizards). Both ways offer the same result. I will post both of the respective code snips.
1)
(from Add Service Reference wizard)
http://www.ncbi.nlm.nih.gov/entrez/eutils/soap/v2.0/efetch_pubmed.wsdl (as Namespace: PubMedWebServiceEfetch_pubMed)
(in code)
Stopwatch sw = new Stopwatch();
PubMedWebServiceEfetch_pubMed.eUtilsServiceSoapClient server = new PubMedWebServiceEfetch_pubMed.eUtilsServiceSoapClient();
try
{
PubMedWebServiceEfetch_pubMed.eFetchRequest searchRequest = new PubMedWebServiceEfetch_pubMed.eFetchRequest();
searchRequest.id = "11850928";
Console.WriteLine("Run server.run_eFetch(theRequest). [Reset stopwatch]");
sw.Restart();
PubMedWebServiceEfetch_pubMed.eFetchResult searchResult = server.run_eFetch(searchRequest);
Console.WriteLine(searchResult.Count() + " - elapsed milliseconds = " + sw.ElapsedMilliseconds);
sw.Stop();
}
catch (Exception e1) { Console.WriteLine(e1); }
finally { server.Dispose(); }
2)
(from command line)
wsdl /out:myProxyClassPubMed.cs http://eutils.ncbi.nlm.nih.gov/soap/v2.0/efetch_pubmed.wsdl
csc /t:library MyProxyClassPubMed.cs
(add dll to console app)
Stopwatch sw = new Stopwatch();
eFetchPubmedService service = new eFetchPubmedService();
try
{
eFetchRequest theRequest = new eFetchRequest();
theRequest.id = "11850928";
Console.WriteLine("Run service.run_eFetch(theRequest). [Reset stopwatch]");
sw.Restart();
eFetchResult searchResult = service.run_eFetch(theRequest);
Console.WriteLine(searchResult.Count() + " - elapsed milliseconds = " + sw.ElapsedMilliseconds);
sw.Stop();
}
catch (Exception e1) { Console.WriteLine(e1); }
finally { service.Dispose(); }
After much searching I found that you are supposed to be able to use sgen to create a XML Serializer. I ran:
sgen /a:myProxyClassPubMed.dll /f
This created a dll myProxyClassPubMed.XmlSerializers.dll which I then added as a reference in the second connection type.
I have also messed with the "Generate serialization assembly" option in the build area of the app and found no improvement.
I would like to make these web service calls through an ASP.NET page so twelve second return times on the first call are unacceptable.
I considered positing this on BioStar but it is not as well attended as this forum. I may do that if no answers are found here.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个调用打开通道(相对昂贵),第二个调用利用已经打开的通道(成本较低)。
The first call opens the channel (which is relatively expensive) and the second call utilizes that already open channel (less expensive).