【问题标题】:Why classic asp.net web service more faster than wcf?为什么经典的 asp.net web 服务比 wcf 更快?
【发布时间】:2012-01-01 23:17:06
【问题描述】:

我创建了 2 个不同的网络服务。其中之一是经典 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"}};
        }
    }

MY 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>

我在客户端控制台应用程序中使用秒表来测试性能:

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

    }

结果:

经典网络服务 ms: 00.6860078 wcf 网络服务 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。这让我很困惑。这是一种技巧还是问题?我需要你的想法吗?

【问题讨论】:

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


    【解决方案1】:

    尝试运行一个测试,在其中调用每个 Web 服务 100 次。 Wcf 在第一次调用时往往会产生少量开销,但一旦创建并运行它,所有后续调用都会快得多。如果你把每一个加起来,你会看到速度增加。

    【讨论】:

      【解决方案2】:

      您的测试有缺陷。您可能只是在测量预热时间而不是实际性能。 WCF 中发生的事情比 ASMX 多得多,因此可以合理地假设它会有更长的预热时间。

      一个好的开始是点击每个服务几千次并丢弃前几百个结果。

      【讨论】:

        【解决方案3】:

        如果您担心性能,您应该考虑使用我的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" } };
            }
        }
        

        只需上面的代码is automatically made available 在 JSON、XML、JSV、CSV 和 SOAP 1.1/1.2 端点上,通过 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());
        

        您也可以轻松地为 ajax 客户端提供相同的 Web 服务,在 jQuery 中如下所示:

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

        Overall ServiceStack 是一个更快、更简洁的 Web 服务框架,它可以在 ASP.NET 中运行,也可以在具有 .NET 3.5+ 的 Windows 或具有 Mono 的 Linux 上独立运行(无需 Web 服务器的自托管)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-02
          • 2012-03-16
          • 2012-07-03
          • 1970-01-01
          • 2010-09-18
          • 1970-01-01
          相关资源
          最近更新 更多