Search code examples
c#asp.netwcfweb-serviceswcf-binding

Why classic asp.net web service more faster than wcf?


i created 2 different web service. one of them is classic web service and wcf web service also i hosted them on IIS. i tested for performance via STOPWATCH class. but classic web service 2 or 3 times more fast!!! What do you think about it? i googling and i see an article which said "WCF offers better performance, about 25% – 50% faster than ASP.NET Web Services."

Classic web service



   [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 service and its web config below:


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>

i used stopwatch in clien console application to test performance:

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

    }

RESULT :

Classic Web service ms: 00.6860078 wcf web service ms:1.0503

But i see a knowledge wcf is more faster than asp.net web service: http://blogs.msdn.com/b/rickrain/archive/2009/07/15/asp-net-web-services-to-wcf-services-answering-the-question-why.aspx. it is confused me. is it a kind of trick or problem? i need your ideas?


Solution

  • 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:

    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" } };
        }
    }
    

    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:

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

    You can easily serve ajax clients with the same web services as well, which in jQuery looks like:

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

    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.