ChannelFactory:创建和处置

发布于 2024-10-01 02:26:56 字数 1250 浏览 7 评论 0原文

我编写了一个供 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;
        }

虽然这有效,但速度很慢,因为对于每个请求,客户端通道和工厂都会打开和关闭。如果我保持打开状态,速度会非常快。但我想知道最好的做法是什么?我应该保持打开状态吗?或不?如何正确处理这个问题?

I have written an Sdk that is used by a WPF client, and takes care of calling WCF services and caching. These WCF services are called using the ChannelFactory, so I don't have service references. To do that, I created a factory that handles opening and closing ChannelFactory and ClientChannel as follows:

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();
    }       
}

And each request I call like:

 using (var projectStudioService = new ProjectStudioServiceFactory())
        {
            return projectStudioService.Instance.FindAllCities(new FindAllCitiesRequest()).Cities;
        }

Although this works, it's slow because for every request the client channel and factory is opened and closed. If I keep it open, it's very fast. But I was wondering what the best practise would be? Should I keep it open? Or not? How to handle this in a correct way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残月升风 2024-10-08 02:26:56

谢谢丹尼尔,没有看到那个帖子。所以我想以下可能是一个好方法:

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();
    }       
}

Thanks Daniel, didn't see that post. So I guess that the following may be a good approach:

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();
    }       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文