我遇到了一个问题,ObjectDisposedException
大约 50% 的时间都在抛出一个。try
下面(内)内的代码finally
导致异常。我不知道如何处理这个。我可以只吃异常,如下所示,但是有没有办法在不发生异常的情况下检查和关闭对象?
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.
}
}
}