首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >适当避免ObjectDisposedException

适当避免ObjectDisposedException
EN

Stack Overflow用户
提问于 2015-02-05 06:46:18
回答 3查看 1.9K关注 0票数 2

我遇到了一个问题,大约有50%的时间会抛出一个ObjectDisposedException。下面的try ( finally)中的代码导致了异常。我不知道该怎么处理。我可以吃这个异常,如下所示,但是有没有一种方法可以检查和关闭对象而不发生异常呢?

代码语言:javascript
复制
    public static FindResponse Discover(FindCriteria findCriteria, 
                                        DiscoveryEndpoint discoveryEndpoint = null)
    {
        DiscoveryClient discoveryClient = null;

        try
        {
            if (discoveryEndpoint == null) { 
                 discoveryEndpoint = new UdpDiscoveryEndpoint(); 
            }

            discoveryClient = new DiscoveryClient(discoveryEndpoint);

            return discoveryClient.Find(findCriteria);
        }
        finally
        {
            try
            {
                if (discoveryClient != null)
                {
                    discoveryClient.Close();
                }
            }
            catch (ObjectDisposedException)
            {
                // Eat it.
            }
        }
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-05 06:55:27

怎么样

代码语言:javascript
复制
public static FindResponse Discover(FindCriteria findCriteria, DiscoveryEndpoint discoveryEndpoint = null)
{
    if (discoveryEndpoint == null) 
      discoveryEndpoint = new UdpDiscoveryEndpoint();

    using (var client = new DiscoveryClient(discoveryEndpoint))
    {
        return client.Find(findCriteria);
    }
}

更新

似乎DiscoveryClient.Dispose()会抛出异常。OP的原始方法似乎是唯一可以接受的答案。

票数 2
EN

Stack Overflow用户

发布于 2015-02-05 07:30:40

虽然我不太清楚您为什么会遇到它,但是您可以对普通的WCF客户端尝试如下:

如果您在discoveryClient上有"State“属性,请尝试进行以下防御检查:

代码语言:javascript
复制
finally
        {
            try
            {
                if (discoveryClient != null) 
                { 
                  if(discoveryClient.State == CommunicationState.Faulted)
                  {
                     discoveryClient.Abort();
                  }
                  else if(discoveryClient.State != CommunicationState.Closed )
                  {
                     discoveryClient.Close();
                  }
            }
            catch (ObjectDisposedException)
            {

            }
        }

希望这能帮到你。

票数 1
EN

Stack Overflow用户

发布于 2015-02-05 06:58:41

此外,我建议对IDisposable对象使用“使用”。考虑到DiscoveryClient是IDisposable

代码语言:javascript
复制
public static FindResponse Discover(FindCriteria findCriteria, DiscoveryEndpoint discoveryEndpoint = null)
    {

        FindResponse response = null;
        try
        {
            if (discoveryEndpoint == null) { discoveryEndpoint = new UdpDiscoveryEndpoint(); }

            using (DiscoveryClient discoveryClient = new DiscoveryClient(discoveryEndpoint))
            {
                response = discoveryClient.Find(findCriteria);
                discoveryClient.Close();
            }
        }
        finally
        {   
            // other finalizing works, like clearing lists, dictionaries etc.
        }
        return response;
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28337760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档