我已经编写了一个由WPF客户端使用的Sdk,并负责调用WCF服务和缓存。这些WCF服务是使用ChannelFactory调用的,所以我没有服务引用。为此,我创建了一个处理打开和关闭ChannelFactory和ClientChannel的工厂,如下所示:
public class ProjectStudioServiceFactory : IDisposable
{
private IProjectStudioService _projectStudioService;
private static ChannelFactory<IProjectStudioService> _channelFactory;
public IProjectStudioService Instance
{
get
{
if (_channelFactory==null) _channelFactory = new ChannelFactory<IProjectStudioService>("ProjectStudioServiceEndPoint");
_projectStudioService = _channelFactory.CreateChannel();
((IClientChannel)_projectStudioService).Open();
return _projectStudioService;
}
}
public void Dispose()
{
((IClientChannel)_projectStudioService).Close();
_channelFactory.Close();
}
}我调用的每个请求如下所示:
using (var projectStudioService = new ProjectStudioServiceFactory())
{
return projectStudioService.Instance.FindAllCities(new FindAllCitiesRequest()).Cities;
}虽然这种方法可以工作,但速度很慢,因为对于每个请求,客户端通道和工厂都是打开和关闭的。如果我让它开着,它会很快。但我想知道最好的做法是什么?我应该让它一直开着吗?还是不想?如何以正确的方式处理这个问题?
发布于 2010-11-07 02:57:29
谢谢大牛,我没看到那个帖子。所以我猜以下可能是一个好的方法:
public class ProjectStudioServiceFactory : IDisposable
{
private static IProjectStudioService _projectStudioService;
private static ChannelFactory<IProjectStudioService> _channelFactory;
public IProjectStudioService Instance
{
get
{
if (_projectStudioService == null)
{
_channelFactory = new ChannelFactory<IProjectStudioService>("ProjectStudioServiceEndPoint");
_projectStudioService = _channelFactory.CreateChannel();
((IClientChannel)_projectStudioService).Open();
}
return _projectStudioService;
}
}
public void Dispose()
{
//((IClientChannel)_projectStudioService).Close();
//_channelFactory.Close();
}
}https://stackoverflow.com/questions/4112684
复制相似问题