gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:40:18 25 4
gpt4 key购买 nike

我创建了 2 个不同的网络服务。其中之一是经典的网络服务和 wcf 网络服务,我也将它们托管在 IIS 上。我通过秒表类测试了性能。 但经典网络服务要快 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>

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

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

<pre><code> }
</code></pre>

结果:

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

最佳答案

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

关于c# - 为什么经典的 asp.net web 服务比 wcf 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8195180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com