首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么与远程处理相比,WCF的性能会如此缓慢?

问为什么与远程处理相比,WCF的性能会如此缓慢?
EN

Stack Overflow用户
提问于 2010-12-07 06:40:17
回答 1查看 3K关注 0票数 4

我正在尝试评估在新系统中使用哪种通信技术,目前看起来远程处理是我们唯一的选择,因为WCF的性能非常糟糕。

我对使用nettcp调用托管在IIS7中的WCF服务与调用托管在控制台应用程序中的远程处理接口进行了基准测试。WCF服务需要大约4.5秒来执行1000个请求,在一个端点上同步(它只是返回一个对象的新即时消息)。远程处理客户端执行相同的任务所需的时间小于0.5秒。

以下是WCF客户端代码:

代码语言:javascript
复制
public class TcpNewsService : INewsService
{
    private INewsService _service = null;

    Lazy<ChannelFactory<INewsService>> _newsFactory = new Lazy<ChannelFactory<INewsService>>(() =>
    {
        var tcpBinding = new NetTcpBinding
            {
                //MaxBufferPoolSize = int.MaxValue,
                //MaxBufferSize = int.MaxValue,
                //MaxConnections = int.MaxValue,
                //MaxReceivedMessageSize = int.MaxValue,
                PortSharingEnabled=false,
                TransactionFlow = false,
                ListenBacklog = int.MaxValue,
                Security = new NetTcpSecurity 
                { 
                    Mode = SecurityMode.None, 
                    Transport = new TcpTransportSecurity
                    {
                         ProtectionLevel = System.Net.Security.ProtectionLevel.None,
                         ClientCredentialType = TcpClientCredentialType.None
                    },
                    Message = new MessageSecurityOverTcp 
                    { 
                        ClientCredentialType = MessageCredentialType.None } 
                    },
                ReliableSession = new OptionalReliableSession { Enabled = false }
            };
        EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8089/NewsService.svc");
        return new ChannelFactory<INewsService>(tcpBinding, endpointAddress);
    });

    public TcpNewsService()
    {
        _service = _newsFactory.Value.CreateChannel();
        ((ICommunicationObject)_service).Open();

    }

    public List<NewsItem> GetNews()
    {
        return _service.GetNews();
    }
}

和一个简单的控制台应用程序来调用客户端代码:

代码语言:javascript
复制
var client = new TcpNewsService();

Console.WriteLine("Getting all news");

var sw = new System.Diagnostics.Stopwatch();
sw.Start();

for (int i = 0; i < 1000; i++)
{
    var news = client.GetNews();
}
sw.Stop();

Console.WriteLine("Finished in " + sw.Elapsed.TotalSeconds);
Console.ReadLine();

IIS主机的web.config文件如下所示:

代码语言:javascript
复制
<system.serviceModel>
<services>
  <service behaviorConfiguration="NewsServiceBehavior" name="RiaSpike.News.Service.NewsService">
    <endpoint address=""
          binding="netTcpBinding"
          bindingConfiguration="tcpBinding"
          contract="RiaSpike.News.Types.INewsService">
    </endpoint>
    <endpoint address="http://localhost:8094/NewsService.svc"
          binding="basicHttpBinding"
          bindingConfiguration="httpBinding"
          contract="RiaSpike.News.Types.INewsService">
    </endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="NewsServiceBehavior">
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <basicHttpBinding>
    <binding name="httpBinding">
      <security mode="None">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </basicHttpBinding>
  <netTcpBinding>
    <binding name="tcpBinding" portSharingEnabled="false">
      <security mode="None">
        <transport clientCredentialType="None" />
        <message clientCredentialType="None" />
      </security>
      <reliableSession enabled="false"  />
    </binding>
  </netTcpBinding>
</bindings>

​

和IIS中托管的服务类:

代码语言:javascript
复制
[ServiceBehavior(
    ConcurrencyMode = ConcurrencyMode.Multiple,
    InstanceContextMode = InstanceContextMode.Single,
    AddressFilterMode = AddressFilterMode.Any
)]

public class NewsService : MarshalByRefObject, INewsService
{

    public List<NewsItem> GetNews()
    {
        return new List<NewsItem> 
            { 
                new NewsItem { Descripion = "The Description", Id = 1, Title = "The Title"}
            };
    }
}

我已经跟踪了WCF的活动,并看到这个过程大约需要5毫秒才能完成(我无法上传图像,这里是日志中的一个活动跟踪)

来自:正在处理消息5. Transfer 3/12/201015:35:58.861

活动边界。开始3/12/201015:35:58.861

通过通道接收到消息。信息3-12-201015:35:58.861

至:执行'Ria.Spike.News.INewsService.GetNews‘Transfer 3/12/201015:35:58.864

活动边界。暂停3-12-201015:35:58.864

来自:执行'Ria.Spike.News.INewsService.GetNews‘Transfer 3/12/201015:35:58.864

活动边界。恢复3/12/201015:35:58.864

通过通道发送消息信息3/12/201015:35:58.866

活动边界。Stop 3/12-201015:35:58.866

这是它得到的最好的结果吗:s

下面是本例中使用的远程处理代码。

代码语言:javascript
复制
var  iserver = (INewsService)Activator.GetObject(typeof(INewsService), "tcp://127.0.0.1:9000/news");
var sw = new System.Diagnostics.Stopwatch();
sw.Start();

for (int i = 0; i < 1000; i++)
{
    var news = iserver.GetNews();
}

sw.Stop();

Console.WriteLine("Finished in " + sw.Elapsed.TotalSeconds);
Console.ReadLine();

以及承载在IIS中的此远程处理通道的TCP端点:

代码语言:javascript
复制
public class Global : System.Web.HttpApplication
{
    private TcpServerChannel _quote; 

    protected void Application_Start(object sender, EventArgs e)
    {
        _quote = new TcpServerChannel(9000);
        if (ChannelServices.RegisteredChannels.Length ==0)
        {
            ChannelServices.RegisterChannel(_quote, false);    
        }


        RemotingConfiguration.RegisterWellKnownServiceType(
        typeof(NewsService),
        "news",
        WellKnownObjectMode.SingleCall);

        _quote.StartListening(null);      
    }
}
EN

回答 1

Stack Overflow用户

发布于 2010-12-07 10:51:05

这只测试顺序的、同步的调用。IIS托管的WCF服务提供了更多的基础结构来处理更高的负载,并且很可能在高负载测试(具有大量并发连接的测试)中优于远程处理。

远程端点也可以驻留在IIS中,以获得相同的优点。WCF也可以托管在控制台中。你真的是在拿苹果和橙子做比较。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4371661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档