问题描述
问题
为什么我的代码行永远不会执行:Console.WriteLine("THIS NOT EXECUTE");
我不确定是关于 Try-Catch、委托还是 RabbitMQ。
有人遇到过类似的情况吗?
进展
有一个异常(这就是为什么我的代码的某一部分永远不会执行的原因,但为什么会发生该异常尚不得而知):
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
解决方案
在我同事的计算机上,这工作正常,并且该部分代码执行...因此,我执行了以下操作:
- 在本地删除项目
- 再次克隆仓库
- 运行项目
现在一切正常,但我想知道最初的问题是什么以及为什么我的计算机上会发生该异常?
代码
private EventingBasicConsumer DecodeMessage()
{
SmellDetectionReport reportMessage = new SmellDetectionReport();
var consumer = new EventingBasicConsumer(Channel);
consumer.Received += (model,deliveryArguments) =>
{
var body = deliveryArguments.Body.ToArray();
var jsonMessage = Encoding.UTF8.GetString(body);
try
{
Console.WriteLine("THIS EXECUTE");
reportMessage = JsonConvert.DeserializeObject<SmellDetectionReport>(jsonMessage);
Console.WriteLine("THIS NOT EXECUTE");
}
catch (Exception e)
{
//Todo: write exc
Console.WriteLine("exc");
Console.WriteLine("exc");
}
finally
{
Console.WriteLine("finally");
Console.WriteLine("finally");
}
};
Console.WriteLine("end");
return consumer;
}
基本信息
语言:C#
技术:RabbitMQ
解决方法
最好的办法是通过单击 code.received 开头的代码行左侧设置断点,然后按 f11 逐句逐句执行代码。这将允许您查看代码所采用的路径以及为什么它没有到达该语句。我最初的猜测是 JSON 反序列化失败,因此执行移动到 catch 块。
,catch (Exception e)
{
//TODO: write exc
Console.WriteLine($"exc {e.Message}");
}
写一个像这样的 catch 语句来更好地了解异常。也尝试调试代码。