为什么经典的asp.net web服务比wcf更快?

时间:2011-11-19 15:52:42

标签: c# asp.net wcf web-services wcf-binding

我创建了2个不同的Web服务。其中一个是经典的Web服务和wcf Web服务,我也在IIS上托管它们。我通过STOPWATCH课程测试了性能。  但经典的网络服务快2或3倍!你怎么看待这件事? 我在谷歌搜索,我看到一篇文章说“WCF提供更好的性能,比ASP.NET Web服务快25% - 50%。”

经典网络服务



   [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服务及其网络配置如下:


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();

    }

}

结果:

Classic Web service ms:00.6860078 wcf web service 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。这让我困惑。这是一种伎俩还是问题?我需要你的想法?

3 个答案:

答案 0 :(得分:5)

尝试运行测试,每次调用每个Web服务100次。 Wcf在第一次调用时往往会有很小的开销,但是一旦创建并运行所有后续调用就会快得多。如果你拿走每个的总和,你会看到速度增加。

答案 1 :(得分:5)

您的测试存在缺陷。您可能只是测量预热时间而不是实际性能。 WCF比ASMX还要多得多,所以假设它有更长的预热时间是合理的。

一个良好的开端是将每项服务命中几千次并抛出前几百个结果。

答案 2 :(得分:0)

如果您担心表现,请考虑使用我的ServiceStack Open Source Web Services framework .NET server benchmarks shows 它始终执行其他.NET库。它具有最快的.NET文本序列化程序,包括.NET's fastest JSON serializer

它不仅比WCF快得多,而且更容易 - ServiceStack中的相同服务看起来像:

public class MyCustomers : ServiceBase<Customers>
{
    public override object Run(Customers request)
    {
       return new List<Customers>() { 
           new Customers() { id = 1, Name = "murat", SurName = "xyzk" }, 
           new Customers() { id = 2, Name = "ali", SurName = "Yılmaz" } };
    }
}

仅使用JSON,XML,JSV,CSV和SOAP 1.1 / 1.2端点上的代码is automatically made available,通过HTTP GET或HTTP POST,无需任何配置。

由于它是代码优先的Web服务框架,因此您无需代码生成,因为您可以使用您定义Web服务的DTO调用Web服务,提供这种简洁,简洁的API:

IServiceClient client = new JsonServiceClient("http://host/baseurl");
//IServiceClient client = new XmlServiceClient("http://host/baseurl"); //to use xml instead
var customers = client.Send<List<Customers>>(new Customers());

您可以使用相同的Web服务轻松地为ajax客户端提供服务,这在jQuery中看起来像:

$.getJSON("http://host/baseurl/customers", function(r) {
   console.log("customers received: #" + r.length);
});

整体ServiceStack是一个更快,更清洁的Web服务框架,可以在带有.NET 3.5+的Windows或带有Mono的Linux上以ASP.NET或独立(无Web服务器自托管)运行。