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

Pen*_*uen 3 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"}};
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的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" } };
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

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>

Run Code Online (Sandbox Code Playgroud)

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

    }
Run Code Online (Sandbox Code Playgroud)

Run Code Online (Sandbox Code Playgroud)


   [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"}};
        }
    }
Run Code Online (Sandbox Code Playgroud)

结果:

经典Web服务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.这让我困惑.这是一种伎俩还是问题?我需要你的想法?

Nik*_*oli 5

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


Wya*_*ett 5

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

一个好的开始是每次服务几千次并扔出前几百个结果.