我正在开发PowerShell二进制模块.它使用Json.NET和其他库.我收到此异常“无法加载文件或程序集’Newtonsoft.Json,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed’或其中一个依赖项.系统找...

我正在开发PowerShell二进制模块.它使用Json.NET和其他库.
我收到此异常“无法加载文件或程序集’Newtonsoft.Json,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed’或其中一个依赖项.系统找不到指定的文件.”
在硬盘上我有它的更新版本(版本7.0.2)
这样的问题很容易在控制台,Web或桌面应用程序中解决,使用app.config或“web.config”通过这样的行
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
如何为PowerShell二进制模块做类似的事情?
解决方法:
在开发使用多个第三方库(Google API,Dropbox,Graph等)的PowerShell模块时,我自己遇到了这个问题,我发现以下解决方案最简单:
public static Assembly CurrentDomain_BindingRedirect(object sender, ResolveEventArgs args)
{
var name = new AssemblyName(args.Name);
switch (name.Name)
{
case "Microsoft.Graph.Core":
return typeof(Microsoft.Graph.IBaseClient).Assembly;
case "Newtonsoft.Json":
return typeof(Newtonsoft.Json.JsonSerializer).Assembly;
case "System.Net.Http.Primitives":
return Assembly.LoadFrom("System.Net.Http.Primitives.dll");
default:
return null;
}
}
注意在方法中,我有两种可能的方法来引用程序集,但它们都做同样的事情,它们强制使用该程序集的当前版本. (无论是通过类引用还是通过dll文件加载加载)
要在任何cmd中使用它,请在PSCmdLet的BeginProcessing()方法中添加以下事件处理程序.
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_BindingRedirect;
本文标题为:c# – PowerShell二进制模块程序集依赖性错误


- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- .NET CORE DI 依赖注入 2023-09-27
- 如何使用C# 捕获进程输出 2023-03-10
- Unity Shader实现模糊效果 2023-04-27
- Unity3D实现渐变颜色效果 2023-01-16
- Oracle中for循环的使用方法 2023-07-04
- user32.dll 函数说明小结 2022-12-26
- c# 模拟线性回归的示例 2023-03-14
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- WPF使用DrawingContext实现绘制刻度条 2023-07-04