【问题标题】:Is it better to open and close a WCF soap client for each call or make multiple calls with the same client每次调用打开和关闭 WCF 肥皂客户端还是使用同一个客户端进行多次调用会更好吗
【发布时间】:2017-06-05 14:17:22
【问题描述】:

我有一个数据提供程序类,它以您期望的方式进行 SOAP 调用。我创建客户端,拨打电话,关闭客户端,返回数据。我已将其列为下面的原始代码。

我正在做一些批处理。我将一次处理 30 到 40 条记录。我将从另一个类的 for 循环中调用数据提供者。

我想知道我是否应该像原始代码中所示那样为每个请求打开和关闭 SOAP 客户端,或者我是否应该打开客户端一次,只从 for 循环中检索数据然后关闭退出循环后的 SOAP 客户端(修改代码)。

如果您能提供每种实现的优缺点,我将不胜感激。我不确定在每次调用时创建和关闭客户端(如我的原始代码中所示)是否是一项昂贵的操作,或者它是否微不足道。如果我打开/关闭每个呼叫,我会产生太多开销吗?仅供参考,我使用的 SOAP 客户端不知道 ChannelFactory。

原始代码

    public int RetrieveSomeInfo(string someWayToIdentify)
    {
        int result = 0;           

        try
        {
            _client = new SomeSoapClient();

            var someResponse = _client.Search(someWayToIdentify);

            if (someResponse.Any())
            {
                result = someResponse.First().SomeIntData;
            }

            _client.Close();
        }
        catch (CommunicationException e)
        {
            _logger.Error(e.Message, e);
            _client.Abort();
        }
        catch (TimeoutException e)
        {
            _logger.Error(e.Message, e);
            _client.Abort();
        }
        catch (Exception e)
        {
            _logger.Error(e.Message, e);
            _client.Abort();
            throw;
        }

        return result;
    }

这样称呼

foreach (var record in records)
{                                
    var someData = _sometDataProvider.RetrieveSomeInfo(someWayToIdentify);                
}

修改后的代码

public int RetrieveSomeInfo(string someWayToIdentify)
{
    int result = 0;

    try
    {
        //Client created through DI 
        if (_client == null || _client.State != System.ServiceModel.CommunicationState.Opened)
        {
            _client = new SomeService();
        }

        var someResponse = _client.Search(someWayToIdentify);

        if (someResponse.Any())
        {
            result = someResponse.First().SomeIntData;
        }
    }
    catch (Exception e)
    {
        _logger.Error(e.Message, e);
        throw;
    }

    return result;
}



public void Close()
{
    try
    {
        _client.Close();
    }
    catch (CommunicationException e)
    {
        _logger.Error(e.Message, e);
        _client.Abort();
    }
    catch (TimeoutException e)
    {
        _logger.Error(e.Message, e);
        _client.Abort();
    }
    catch (Exception e)
    {
        _logger.Error(e.Message, e);
        _client.Abort();
        throw;
    }
}

这样称呼

foreach (var record in records)
{                                
    var someData = _sometDataProvider.RetrieveSomeInfo(someWayToIdentify);                
}
_someDataProvider.Close();

【问题讨论】:

    标签: asp.net web-services wcf soap


    【解决方案1】:

    在这里对所有批处理调用使用相同的客户端实例是可以的。唯一可能导致问题的情况是 WCF 服务本身设置了一个状态完整的会话。

    您可以为您的 WCF 服务设置一个连接池,然后最好在使用后关闭连接(即,将其返回到池中)。在连接池场景中,连接永远不会真正完全关闭。这种做法与数据库连接的典型工作方式非常相似,其中一组连接在池中进行管理,关闭连接只是将连接标记为可用于对池的下一个请求。

    【讨论】:

      猜你喜欢
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 2019-10-20
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      相关资源
      最近更新 更多