【问题标题】:Using System.Management on Linux在 Linux 上使用 System.Management
【发布时间】:2021-08-18 09:37:47
【问题描述】:

我正在创建一个控制台 .NET 5.0 应用程序,以使用其 WorkVisual 软件中提供的 dll 与 KUKA 机器人通信。这些 dll 是使用 .NET Framework 4.6 构建的。在 Windows 上一切正常。

棘手的部分是我需要将它移植到 Linux。由于 .NET 5.0 似乎是跨平台的,我决定使用 .NET 5.0 构建我的应用程序并添加 Kuka dll (.NET 4.6) 作为参考。虽然在 Windows 上运行良好,但问题从 Linux 开始。该应用程序本身使用 .NET 5.0 运行时(来自终端的dotnet MyApp)运行,但只有这么长时间我才尝试调用连接到机器人的函数。然后我得到一个异常:

Unhandled exception. System.TypeLoadException: Could not load type 'System.Management.Instrumentation.InstrumentationClassAttribute' from assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   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(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, 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.RuntimeType.IsDefined(Type attributeType, Boolean inherit)
   at System.Diagnostics.StackTrace.ShowInStackTrace(MethodBase mb)
   at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat, StringBuilder sb)
   at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat)
   at System.Exception.get_StackTrace()
   at System.Exception.ToString()
...

我已经将 NuGet 包添加到我的项目中:

  • 系统管理
  • Microsoft.Windows.Compatibility

但他们都没有帮助。我很好奇是否可以在 Linux 上使用这些 dll 运行应用程序,如果可以,我还需要做些什么才能使其正常工作。

我对 dll 的编译没有影响,我没有源代码,只能按原样使用它们。

【问题讨论】:

  • 也许这会帮助stackoverflow.com/questions/45508989/can-net-4-6-run-on-linux。但我不抱太大希望.. 那些外部 dll 很可能依赖于 linux 不支持的某些功能(
  • @Driven-it 谢谢,我已经按照其中一个 cmets 中的建议尝试了 .NET Portability Analyzer,但尽管我的解决方案在所有 .NET Core/Standard 以及Mono,它还显示Kuka的workvisual提供的dll是“未解析的程序集”,根本没有检查......所以没有完全解决问题

标签: .net linux wmi .net-5 .net-4.6


【解决方案1】:

这个Microsoft.Windows.Compatibility nuget 是一个 nuget 包,用于提供对 .NET Framework 中 API 的访问,这些 API 大多仅在 Windows 上运行(因此“Windows”作为 nuget 名称的一部分)。

Microsoft.Windows.Compatibility nuget 包实际上也是一个元包,它提供了 .NET Framework 中可用的相关 API 列表。这还包括 System.Management nuget 包和其他仅适用于 Windows 的软件包,例如 Microsoft.Win32.Registry

因此不保证可以在 Windows 以外的其他操作系统上运行,尤其是那些需要与 Windows 互操作的软件包,例如 System.Management 和 Microsoft.Win32.Registry。

【讨论】:

  • 感谢您的回答。不幸的是,这再次证实了我所担心的......但问题仍然是 - 有没有办法避免这个问题?也许是用酒之类的?
  • 使用葡萄酒?我不知道这种可能性,我不确定。 Wine 是一个 Windows 模拟器。由于不能直接在 Windows 上运行 linux 二进制文件,所以 Wine 作为 Windows 模拟器可能无法运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
相关资源
最近更新 更多