为什么经典的 ASP.NET Web 服务比 WCF 更快?
我创建了 2 个不同的网络服务。其中之一是经典的 Web 服务和 WCF Web 服务,我也将它们托管在 IIS 上。我通过 STOPWATCH 类测试了性能。 但经典网络服务速度快 2 或 3 倍!你对此有何看法? 我在谷歌上搜索,看到一篇文章说“WCF 提供了更好的性能,比 ASP.NET Web 服务快大约 25% – 50%。”
经典 Web 服务
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public List < Customers>GetMyCustomers()
{
return new Customers().GetCustomers();
}
}
public class Customers
{
private int id { get; set; }
private string Name { get; set; }
private string SurName { get; set; }
public Customers()
{
}
public List<Customers> GetCustomers()
{
return new List<Customers>(){ new Customers(){ id=1, Name="murat", SurName="xyzk"},
new Customers(){ id=2, Name="ali", SurName="Yılmaz"}};
}
}
我的 WCF 服务及其 Web 配置如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace WcfServiceLib
{
[ServiceContract]
public interface ICustomers
{
[OperationContract]
List<Customers> GetCustomers();
}
[DataContract]
public class Customers
{
public int id { get; set; }
public string Name { get; set; }
public string SurName { get; set; }
}
public class MyCustomers : ICustomers
{
public List<Customers> GetCustomers()
{
return new List<Customers>() {
new Customers() { id = 1, Name = "murat", SurName = "xyzk" },
new Customers() { id = 2, Name = "ali", SurName = "Yılmaz" } };
}
}
}
WEB.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
<httpRuntime executionTimeout="999999" maxRequestLength="2097151"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfServiceLib.MyCustomers" behaviorConfiguration="CustomersBehavior">
<host>
<baseAddresses>
<add baseAddress = "http://pc/WcfServiceLib/MyCustomers/" />
</baseAddresses>
</host>
<endpoint address ="" binding="basicHttpBinding" contract="WcfServiceLib.ICustomers">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CustomersBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我在客户端控制台应用程序中使用秒表来测试性能: <代码>
static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); klasikservis.Service1 srv = new klasikservis.Service1(); srv.GetMyCustomers(); int count = srv.GetMyCustomers().Count(); Console.WriteLine(count.ToString()); stopwatch.Stop(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); Console.Read(); stopwatch.Start(); WcfServis.CustomersClient WcfSrv = new WcfServis.CustomersClient(); count = WcfSrv.GetCustomers().Count(); Console.WriteLine(count.ToString()); stopwatch.Stop(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); Thread.Sleep(10000); Console.Read();
}
<代码>
结果:
经典 Web 服务 ms:00.6860078 wcf web 服务 ms:1.0503
但我看到一个知识 wcf 比 asp.net web 服务更快: http://blogs.msdn.com/b/rickrain/archive/2009/07/15/asp-net-web-services-to-wcf-services-answering-the-question-why。 .aspx.这让我很困惑。这是一种技巧还是问题?我需要你的想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试运行一个测试,调用每个 Web 服务 100 次。 Wcf 在第一次调用时往往会产生很小的开销,但一旦创建并运行所有后续调用就会快得多。如果你把每一个的总和你会看到速度的提高。
Try running a test where you call each webservice 100 times. Wcf tends to have a small overhead on the first call but once it is created and running all subsequent calls are much faster. If you take the total of each you will see the speed increase.
你的测试有缺陷。您可能只是测量热身时间而不是实际性能。 WCF 中发生的事情比 ASMX 多得多,因此可以合理地假设它会有更长的预热时间。
一个好的开始是访问每个服务几千次并丢弃前几百个结果。
Your test is flawed. You are probably just measuring warm-up time rather than actual performance. There is a lot more going on in WCF than ASMX so it would be reasonable to presume it would have a longer warm-up time.
A good start would be to hit each service a few thousand times and throw out the first couple hundred results.
如果您担心性能,您应该考虑使用我的ServiceStack 开源 Web 服务框架。 .NET 服务器基准测试显示它的性能始终优于其他 .NET 库。它具有最快的 .NET 文本序列化程序,包括 .NET 最快的 JSON 序列化程序。
它不仅比 WCF 快得多,而且也更容易 - ServiceStack 中的相同服务如下所示:
只需上面的代码 通过 HTTP GET 或 HTTP POST 在 JSON、XML、JSV、CSV 和 SOAP 1.1/1.2 端点上自动可用,无需任何配置。
由于它是一个代码优先的 Web 服务框架,因此不需要代码生成,因为您可以使用定义 Web 服务的 DTO 来调用您的 Web 服务,并提供这个漂亮的类型化、简洁的 api:
您可以轻松地使用相同的 Ajax 客户端服务Web 服务也是如此,在 jQuery 中看起来像:
总体而言,ServiceStack 是一个更快、更干净的 Web 服务框架,它可以在 ASP.NET 中运行,也可以在带有 .NET 的 Windows 上独立运行(无需 Web 服务器的自托管) 3.5+ 或带有 Mono 的 Linux。
If you're concerned about performance you should consider using my ServiceStack Open Source Web Services framework. The .NET server benchmarks shows it consistently out performs other .NET libraries. It has the fastest text serializer for .NET including .NET's fastest JSON serializer.
Not only is it much faster than WCF it's a lot easier too - same service in ServiceStack looks like:
With just the code above it is automatically made available on JSON, XML, JSV, CSV and SOAP 1.1/1.2 endpoints, via HTTP GET, or HTTP POST without any configuration required.
Since it's a code-first web services framework no code-gen is required either as you can call your web services using the DTO's you defined your web service with, providing this nice typed, succinct api:
You can easily serve ajax clients with the same web services as well, which in jQuery looks like:
Overall ServiceStack is a faster and cleaner web services framework which can run in an ASP.NET or stand-alone (self-hosting without a web server) on Windows with .NET 3.5+ or Linux with Mono.