我正在重构一个通过wcf进行进程间通信的大型程序。由于客户端可以直接访问服务接口,因此使用channelfactory创建通道,因此不需要额外的客户端服务存根。通信包含许多高频请求的大消息(目前使用NetTcpBinding,我正在考虑切换到NetNamedPipeBinding)。什么创建/关闭个人要求:ChannelFactory或创建频道?
我的问题是关于创建/关闭通道与创建/关闭通道工厂之间的区别。更确切地说:渠道工厂创建一个渠道。现在,就个别请求而言:我是否应该创建和关闭渠道工厂,并根据这个渠道与单个请求相关(请参阅解决方案2),还是在性能方面更安全/更好,只创建/关闭与个别请求有关的通道,并将通道工厂留在多个请求中(请参阅解决方案1)。
1)
//set up the channel factory right when I start the whole applicaton
ChannelFactory<IMyService> cf = new ChannelFactory<IMyService>();
//call this trillion of times over time period of hours whenever I want to make a request to the service; channel factory stays open for the whole time
try
{
IMyService myService = cf.CreateChannel();
var returnedStuff = myService.DoStuff();
((IClientChannel)myService).Close();
}
catch ...
//close the channel factory when I stop the whole application
cf.Close();
2)
//call this trillion of times over time period of hours whenever I want to make a request to the service
try
{
ChannelFactory<IMyService> cf = new ChannelFactory<IMyService>();
IMyService myService = cf.CreateChannel();
var returnedStuff = myService.DoStuff();
cf.Close();
}
catch ...
什么不同之实践?什么是正确的做法?还有更好的选择吗?
你有什么用交谈的服务绑定? – 2012-02-16 14:18:07
'ChannelFactory'实现'IDisposable',它的清理应该由'using'语句处理。 –
2012-02-16 14:21:56
当前nettcp被用作绑定。我正在考虑切换到namedpipes,但考虑到当前的架构,这将是一个重大变化。 – user1182735 2012-02-16 14:28:30