我正在开发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#实现归并排序 2023-05-31
- C#使用SQL DataAdapter数据适配代码实例 2023-01-06
- Unity虚拟摇杆的实现方法 2023-02-16
- C#执行EXE文件与输出消息的提取操作 2023-04-14
- C#使用Chart绘制曲线 2023-05-22
- C#使用NPOI将excel导入到list的方法 2023-05-22
- 如何用C#创建用户自定义异常浅析 2023-04-21
- 浅谈C# 构造方法(函数) 2023-03-03
- C# TreeView从数据库绑定数据的示例 2023-04-09
- C#中参数的传递方式详解 2023-06-27
