函数根本没有被评估的奇怪错误

Strange bug where function doesn't get evaluated at all

我有一个用 c# 编写的针对 Outlook 2007 和 2010 的 Outlook VSTO 加载项,这种情况很奇怪。该加载项在 2007 环境中运行良好。当我按下开始时,它在 Visual Studio (2010) 中的 Outlook 2010 调试模式下也能正常工作。但是,一旦我部署到 UAT,其中一个功能就根本没有得到评估。

其中一个函数是

private static bool HTTPTransmitEmailItem(string username, string email_file_name)
        {
            // DEBUG
            Utils.LogDebug("Got into HTTPTransmitEmailItem");
            try
            {
                Stopwatch timer = new Stopwatch();
                timer.Start();
                try
                {
                    Utils.LogDebug("Msg saved as : " + full_file_name_and_path);

                    if (HTTPPostDataToEP(username, temp_path, email_file_name))
                    {
                        File.Delete(full_file_name_and_path);
                        return true;
                    }
                    else
                    {
                        Utils.LogWarn("Trans Fail, Storing for later sending. " + email_file_name);
                        //if need start resend timer
                        TransmitFailed();
                    }
                }
                catch (Exception ex)
                {
                         Utils.HandleException("OFE HHTP Ex", ex);

   

TransmitFailed();


                }

                timer.Stop();
                Utils.LogDebug("Email File Thread took " + timer.ElapsedMilliseconds.ToString() + "(ms)");
            }
            catch (Exception ex)
            {
            }
            return false;
        }

罪魁祸首是零件:

if (HTTPPostDataToEP(username, temp_path, email_file_name))
                        {
                            File.Delete(full_file_name_and_path);
                            return true;
                        }
                        else
                        {
                            Utils.LogWarn("Trans Fail, Storing for later sending. " + email_file_name);
                            //if need start resend timer
                            TransmitFailed();
                        }

应用程序从不进入方法HTTPPostDataToEP...方法定义为

private static bool HTTPPostDataToEP(string username, string path, string name)
        {
            // DEBUG
            Utils.LogDebug("Got into HTTPPostDataToEP");
            try
            {
                var content = new MultipartFormDataContent();
                content.Add(new StringContent(username), "userAddress");
                content.Add(new StreamContent(File.Open(path + name, FileMode.Open)), "msg", name);
                // DEBUG
                Utils.LogDebug("In Line 174 in OutlookTransmitRaw");
                var client = new HttpClient();
                HttpResponseMessage result = client.PostAsync(Utils.configEndPoint, content).Result;
                // DEBUG
                Utils.LogDebug("In Line 178 in OutlookTransmitRaw. Result is " + result);
                if (result.IsSuccessStatusCode)
                    Utils.LogDebug("Msg Transmit Response : " + result.ToString());
                else
                    Utils.LogInfo("Msg Fail Transmit Response: " + result.ToString());

                return result.IsSuccessStatusCode;
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to dispatch email to API. Caused by ", ex);
            }
        }

应用程序没有引发任何异常。它只是走过 if 块并执行 Utils.LogDebug("Email File Thread took " + timer.ElapsedMilliseconds.ToString() + "(ms)"); 这只会在我发布项目并使用 set.exe 文件安装它时发生。在调试模式下,它按预期工作...

空的 catch 块可以解释您的问题:引发异常,您只需忽略它并愉快地继续。

例如,我可以想象在您部署解决方案后文件权限出现问题。但是无论出现什么问题都会被吞没,你永远不会听到它...

要么从那个 catch 块中抛出,要么在抛出之前记录一些东西,但是盲目地忽略像这样的异常从来都不是一个好主意。出问题了,你获取信息异常,拒绝查看

错误:

catch (Exception ex)
{
}

奇怪又没用:

catch (Exception ex)
{
    throw;
}

经常有用:

catch (Exception ex)
{
    Logger.Log(ex);
    throw;
}