gpt4 book ai didi

.net - 与 Remoting 相比,为什么 WCF 性能如此缓慢?

转载 作者:行者123 更新时间:2023-12-03 17:48:57 24 4
gpt4 key购买 nike

我正在尝试评估在新系统中使用哪种通信技术,现在看起来 Remoting 是我们唯一的选择,因为 WCF 的性能很糟糕。

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

这是 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. 转移 2010 年 3 月 12 日 15:35:58.861
事件边界。开始时间 2010 年 3 月 12 日 15:35:58.861
通过 channel 收到消息。信息 2010 年 3 月 12 日 15:35:58.861
至:执行“Ria.Spike.News.INewsService.GetNews”传输 3/12/2010 15:35:58.864
事件边界。暂停 2010 年 3 月 12 日 15:35:58.864
来自:执行“Ria.Spike.News.INewsService.GetNews”传输 3/12/2010 15:35:58.864
事件边界。简历 2010 年 3 月 12 日 15:35:58.864
通过 channel 信息发送消息 3/12/2010 15:35:58.866
事件边界。停止 2010 年 3 月 12 日 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 - 与 Remoting 相比,为什么 WCF 性能如此缓慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4371661/

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