【问题标题】:Cannot Print Exception String in Visual Studio无法在 Visual Studio 中打印异常字符串
【发布时间】:2020-01-31 03:57:27
【问题描述】:

我正在尝试运行这行代码以使用 csharp 和 dotnet core 2.1.300 (sdk) 连接到 mysql。

 string server = "localhost";
 string database = "db";
 string uid = "root";
 string password = "";
 string connectionString;
 connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
connection.Open();

但是最后一行会引发一个异常,我无法理解解决它的问题所在。 使用 dotnet 运行项目的 dll 向我展示了这一点:

dotnet : 
At line:1 char:1
+ dotnet C:\Users\foo\bar\project ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError


Unhandled Exception: 


Cannot print exception string because Exception.ToString() failed.

我尝试通过 similar question 之类的帖子进行推荐,并将 System.Runtime.Serialization.Primitives 程序集添加到我在 VS 2017 中的引用中,但没有帮助。

我实际上正在修改这个 csharp 项目,以便将其输出写入数据库。 https://github.com/vcsjones/AuthenticodeLint

将 MySqlException 更改为 Exception 将导致以下输出:

Unhandled Exception: 
System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, 
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* 
methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] 
typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& 
lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean 
mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& 
ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, 
RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
   at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Diagnostics.StackTrace.ShowInStackTrace(MethodBase mb)
   at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat)
....

【问题讨论】:

  • 它在connection.Open(); 行吗?我们可以获取完整的堆栈跟踪吗?
  • @Çöđěxěŕ:我无法打印堆栈跟踪。(或者我不知道该怎么做!根据我的理解,虽然我捕捉到最后一行引发的异常,但打印堆栈跟踪或异常消息引发另一个异常并失败!)
  • 我正在处理这样的异常:try { connection.Open(); } catch (MySqlException ex) { verboseWriter.LogMessage(ex.StackTrace);即使我注释掉这一行 verboseWriter.LogMessage(ex.StackTrace);我仍然收到“未处理的异常”错误。
  • 将您的例外更改为 Exception 而不是 MySqlException。当你在那里放一个断点然后检查Message时你会得到什么...如果这不起作用你是否至少尝试过2.2版;如果我没记错的话,这是 2.1x 中的一个问题...
  • 我按照你的要求更新了我的帖子(更改为例外)。

标签: c# mysql exception visual-studio-2017


【解决方案1】:

更改您的异常类型后,我们有一些事情要做。

未处理的异常: System.IO.FileNotFoundException:无法加载文件或程序集'System.Drawing.Common,版本 = 0.0.0.0,文化 = 中性, PublicKeyToken=cc7b13ffcd2ddd51'。该系统找不到指定的文件。 在 System.ModuleHandle.ResolveType(RuntimeModule 模块,Int32 typeToken,IntPtr* typeInstArgs,Int32 typeInstCount,IntPtr* methodInstArgs、Int32 methodInstCount、ObjectHandleOnStack 类型) 在 System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule 模块,Int32 typeToken,RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) 在 System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken,Type[] genericTypeArguments,Type[] genericMethodArguments) 在 System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord,MetadataImport 范围,Assembly& lastAptcaOkAssembly、RuntimeModule 装饰模块、MetadataToken 装饰令牌、RuntimeType 属性FilterType、布尔值 mustBeInheritable、Object[] 属性、IList derivedAttributes、RuntimeType& attributeType、IRuntimeMethodInfo& ctor、Boolean& ctorHasParameters, Boolean& isVarArg) 在 System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule 装饰模块,Int32 装饰元数据令牌, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable) 在 System.Reflection.CustomAttribute.IsDefined(RuntimeType 类型,RuntimeType caType,布尔继承) 在 System.Diagnostics.StackTrace.ShowInStackTrace(MethodBase mb) 在 System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat) ....

这个异常是由于没有System.Drawing.Common库引起的。打开包管理器,然后运行Install-Package System.Drawing.Common -Version 4.6.0

在你拥有的这个版本中,库没有你需要的依赖项,System.Drawing.Common,他们应该已经更新了这个以包含它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多