我創建了2個不同的Web服務。其中之一是經典的網絡服務和wcf網絡服務,我也將它們託管在IIS上。我通過STOPWATCH類測試了性能。 但經典的網絡服務快2或3倍!你怎麼看待這件事? 我使用googling,我看到一篇文章說「WCF提供了更好的性能,比ASP.NET Web Services快25%-50%」。 爲什麼經典的asp.net web服務比wcf更快?
經典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>
我用秒錶在CLIEN控制檯應用程序來測試性能:
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網絡服務: http://blogs.msdn.com/b/rickrain/archive/2009/07/15/asp-net-web-services-to-wcf-services-answering-the-question-why.aspx。這讓我感到困惑。這是一種伎倆還是問題?我需要你的想法?