无法在 Visual Studio 中打印异常字符串

Cannot Print Exception String in Visual Studio

我正在尝试 运行 这行代码以使用 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 的项目的 ning 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.

我尝试了像这样的帖子的建议 并将 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)
....

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

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) ....

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

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