【问题标题】:Simulate error "The underlying connection was closed: The connection was closed unexpectedly."模拟错误“底层连接已关闭:连接意外关闭。”
【发布时间】:2015-03-20 10:49:48
【问题描述】:

我正在运行一个漫长的过程,它会在 Microsoft Dynamics CRM 4 中逐个更新一些机会。有时这会在中途停止并出现错误:“基础连接已关闭:连接已意外关闭。” 由于它是间歇性的,我认为这是由于网络故障而发生的,但不确定。在任何情况下,如果我重新启动进程(从停止的地方开始),它会立即重新开始。

我编写了以下代码来尝试应对这种情况,因为我希望这是一个通宵的过程(1 次):

int Retries = 0;
bool Ready = false;
while (!Ready && Retries < 5)
{
    try
    {
        using (CrmService service = GetCrmServiceInstance())
        {
            service.Update(opp);
            Ready = true; //break out of the while loop as connection is working.
        }
    }
    catch (SoapException se)
    {
        Retries++;
        if (Retries > 4)
        {
            throw new Exception("Error occurred updating opportunity " + opp.opportunityid.Value + ". Error: " + se.Detail.InnerXml);
        }
    }
    catch (Exception ex)
    {
        Retries++;
        if (Retries > 4)
        {
            throw new Exception("Error occurred updating opportunity " + opp.opportunityid.Value + ". Error: " + ex.Message);
        }
    }
}

我的理论是,如果它最初收到此错误,它将重试进行更新并希望再次工作。如果重试次数超过 4 则抛出错误。

我现在想测试这段代码,看看它是否足以满足我的目的。我尝试运行漫长的过程(6 小时)以查看是否出现错误,SoapException 和 Exception 块上有断点,以便能够在它在执行更新时中断但它没有进行单步执行。那么,有没有办法模拟这个错误,以便我可以测试我的新代码?

【问题讨论】:

  • 重启 CRM 应用程序池?拉网线?
  • @stuartd - 我的应用程序在虚拟机上运行,​​因此可能无法选择拔网线。我也无权访问 crm 所在的服务器以重新启动应用程序池。不过,我会看看能不能找人做。
  • @stuartd - 刚刚得到了一个 dba 以按照您的建议重新启动 CRM 应用程序池,这似乎满足了我的需求。如果您将其作为答案,我很乐意将其标记为正确。

标签: c# exception-handling microsoft-dynamics


【解决方案1】:

模拟连接失败的一种方法是重新启动 CRM 应用程序池,默认情况下为 CrmAppPool。

【讨论】:

    猜你喜欢
    • 2013-05-31
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多