我正在尝试评估在新系统中使用哪种通信技术,现在看来远程处理是我们唯一的选择,因为 WCF 的性能很糟糕。与调用控制台应用程序中托管的远程处理接口(interface)相比,我对使用 nettcp 调用托管在 IIS7 中的 WCF 服务进行了基准测试。 WCF 服务需要大约 4.5 秒才能在端点上同步执行 1000 个请求(它只是返回对象的新实例)。远程客户端需要 这是 WCF 客户端代码: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(); }}还有一个简单的控制台应用程序来调用客户端代码: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 文件如下所示:<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 中的服务类:[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. 传输 3/12/2010 15:35:58.861事件边界。开始 3/12/2010 15:35:58.861通过 channel 收到一条消息。信息 3/12/2010 15:35:58.861至:执行“Ria.Spike.News.INewsService.GetNews”传输 3/12/2010 15:35:58.864事件边界。暂停 3/12/2010 15:35:58.864来自:执行 'Ria.Spike.News.INewsService.GetNews' 传输 3/12/2010 15:35:58.864事件边界。简历 3/12/2010 15:35:58.864通过 channel 发送消息 信息 3/12/2010 15:35:58.866事件边界。停止 3/12/2010 15:35:58.866这是最好的吗:s这是本示例中使用的远程处理代码。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 中的此远程处理 channel 的 TCP 端点: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); }} 最佳答案 这仅测试顺序、同步、调用。 IIS 托管的 WCF 服务提供了更多基础结构来处理更高的负载,并且可能会在高负载测试(具有大量并发连接的测试)中胜过远程处理。远程处理端点也可以托管在 IIS 中,以获得相同的优势。 WCF 也可以托管在控制台中。你真的是在这里比较苹果和橙子。关于.net - 与远程处理相比,为什么 WCF 的性能如此之慢?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4371661/ 10-12 01:24