dependency NPOI强绑定ICSharpCode.SharpZipLib check library depend on how many other libaraies
at NPOI.OpenXml4Net.OPC.ZipPackage.GetPartsImpl()
NPOI.POIXMLException ---> System.IO.FileNotFoundException: Could not load file or assembly 'ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73' or one of its dependencies. The system cannot find the file specified.
at NPOI.OpenXml4Net.OPC.ZipPackage.GetPartsImpl()
at NPOI.OpenXml4Net.OPC.OPCPackage.GetParts()
at NPOI.OpenXml4Net.OPC.OPCPackage.GetPart(PackagePartName partName)
at NPOI.OpenXml4Net.OPC.PackageRelationshipCollection..ctor(OPCPackage container, PackagePart part)
at NPOI.OpenXml4Net.OPC.PackagePart.LoadRelationships()
at NPOI.OpenXml4Net.OPC.Internal.PackagePropertiesPart..ctor(OPCPackage pack, PackagePartName partName)
at NPOI.OpenXml4Net.OPC.OPCPackage.ConfigurePackage(OPCPackage pkg)
at NPOI.OpenXml4Net.OPC.OPCPackage.Create(Stream output)
at NPOI.XSSF.UserModel.XSSFWorkbook.newPackage()
--- End of inner exception stack trace ---
at NPOI.XSSF.UserModel.XSSFWorkbook.newPackage()
at NPOI.XSSF.UserModel.XSSFWorkbook..ctor()
at ConsoleApp1.ChuckTest.DataTableToExcelByte(String excelFormat, ExcelConfig excelConfig, DataTable dataTable) in C:\Users\clu\source\repos\ConsoleApp1\ConsoleApp1\ChuckTest.cs:line 121
at ConsoleApp1.ChuckTest.Test() in C:\Users\clu\source\repos\ConsoleApp1\ConsoleApp1\ChuckTest.cs:line 67
https://github.com/icsharpcode/SharpZipLib/issues/345
https://github.com/icsharpcode/SharpZipLib/issues/270
@markhobson As I know, this error is caused by the new security transparent model introduced by .NET 4.x. Please read https://docs.microsoft.com/en-us/dotnet/framework/misc/security-transparent-code-level-1. Microsoft is getting rid of old assembly attribute AllowPartiallyTrustedCallersAttribute, which is used in NPOI assembly setting.
Assembly does not allow partially trusted caller
Assuming you have access to the sources of your library.
- Give the library you are trying to call a strong name.
- Add [assembly:AllowPartiallyTrustedCallers] to the library that you are trying to call.
- Create a code group to set permissions to the library
A pretty good and detailed explanation is given here Also read the links at the bottom to get a better understanding.
There is a possibility that not your assembly is the problem but you are calling another assembly that does not allow partially trusted callers. At runtime you can use fuslogvw to find which assembly is giving you the problems. If this is the problem and you have the sources of this assembly you need to also apply the [assembly:AllowPartiallyTrustedCallers] attribute to that assembly, if you don't have the sources the only option I know of is to replace the troublesome library.
4.3在IISRemoting的solution中,升级NPOI之后
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="ICSharpCode.SharpZipLib" publicKeyToken="1b03e6acf1164f73" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-0.86.0.518" newVersion="0.86.0.518" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="CMS.ISearchEngine" publicKeyToken="834b12a258f213f9" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-7.0.5354.21097" newVersion="7.0.5354.21097" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="CMS.DataEngine" publicKeyToken="834b12a258f213f9" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-7.0.5354.21102" newVersion="7.0.5354.21102" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="CMS.SearchProviderSQL" publicKeyToken="834b12a258f213f9" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-7.0.5354.21102" newVersion="7.0.5354.21102" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>
CMS.Controls.dll依赖于CMS.ISearchEngine.dll
CMS.DocumentEngine.dll
CMS.IO.dll依赖于ICSharpCode.SharpZipLib.dll
写代码来检查依赖
[Test] public void AssemblyTest() { var list = new List<string>() { @"D:\ReleasePackages\LISA 4\LISA.Installer.Client\Programs\LISA.ControlPanel", @"D:\ReleasePackages\LISA 4\LISA.Installer.Server\Programs\LISA.BackOffice", @"D:\ReleasePackages\LISA 4\LISA.Installer.Server\Programs\LISA.Batch", @"D:\ReleasePackages\LISA 4\LISA.Installer.Server\Programs\LISA.CMSWeb.Public\bin", @"D:\ReleasePackages\LISA 4\LISA.Installer.Server\Programs\LISA.FileExchange", @"D:\ReleasePackages\LISA 4\LISA.Installer.Server\Programs\LISA.IISRemoting\bin", @"D:\ReleasePackages\LISA 4\LISA.Installer.Server\Programs\LISA.WebAPI\bin", @"D:\ReleasePackages\LISA 4\LISA.Installer.Server\Programs\LISA.WebService\bin", }; foreach (var item in list) { PrintWhoDependOnICSharpCodeSharpZipLib(item); } Console.WriteLine("==end=="); var tempList = libraryList.OrderBy(x => x).ToList(); Console.WriteLine($"count = {tempList.Count}"); foreach (var item in tempList) { Console.WriteLine($"\"{item}\","); } } List<string> libraryList = new List<string>(); public void PrintWhoDependOnICSharpCodeSharpZipLib(string folder) { Console.WriteLine(folder); DirectoryInfo directoryInfo = new DirectoryInfo(folder); var extensions = new[] { ".dll", ".exe" }; var totalFiles = directoryInfo.GetFiles(); var assemblyFiles = totalFiles.Where(x => extensions.Contains(x.Extension)); var libraryName = "ICSharpCode.SharpZipLib"; var libraries = new[] { libraryName, }; int i = 0; foreach (var assemblyFile in assemblyFiles) { AssemblyName assemblyName = AssemblyName.GetAssemblyName(assemblyFile.FullName); var assembly = Assembly.Load(assemblyName); var allDependencies = assembly.GetReferencedAssemblies().ToList(); var dependencies = allDependencies.Where(x => libraries.Contains(x.Name)).ToList(); if (dependencies.Count > 0) { i++; Console.WriteLine(i); Console.WriteLine(assemblyName); var tempName = assemblyName.Name; if (!libraryList.Contains(tempName)) { libraryList.Add(tempName); } foreach (var dependency in dependencies) { Console.WriteLine($"{dependency.FullName}"); Console.WriteLine(); } } } Console.WriteLine("==="); }
1
Accor.Office.Excel, Version=4.3.1.43, Culture=neutral, PublicKeyToken=null
ICSharpCode.SharpZipLib, Version=0.85.3.365, Culture=neutral, PublicKeyToken=1b03e6acf1164f73
2
CMS.ImportExport, Version=7.0.5354.21112, Culture=neutral, PublicKeyToken=834b12a258f213f9
ICSharpCode.SharpZipLib, Version=0.85.3.365, Culture=neutral, PublicKeyToken=1b03e6acf1164f73
3
CMS.IO, Version=7.0.5354.21097, Culture=neutral, PublicKeyToken=834b12a258f213f9
ICSharpCode.SharpZipLib, Version=0.85.3.365, Culture=neutral, PublicKeyToken=1b03e6acf1164f73
4
CMS.TranslationServices, Version=7.0.5354.21114, Culture=neutral, PublicKeyToken=834b12a258f213f9
ICSharpCode.SharpZipLib, Version=0.85.3.365, Culture=neutral, PublicKeyToken=1b03e6acf1164f73
5
LISA.Module.Broker.Repository, Version=4.3.1.43, Culture=neutral, PublicKeyToken=null
ICSharpCode.SharpZipLib, Version=0.85.3.365, Culture=neutral, PublicKeyToken=1b03e6acf1164f73
6
LISA.Module.FileImporting.BLL, Version=4.3.1.43, Culture=neutral, PublicKeyToken=null
ICSharpCode.SharpZipLib, Version=0.85.3.365, Culture=neutral, PublicKeyToken=1b03e6acf1164f73
7
NPOI, Version=2.1.3.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1
ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73
8
NPOI.OpenXml4Net, Version=2.1.3.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1
ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73
NPOI 2.1.3可以降级使用ICSharpCode.SharpZipLib
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="ICSharpCode.SharpZipLib" culture="neutral" publicKeyToken="1b03e6acf1164f73" /> <bindingRedirect oldVersion="0.0.0.0-0.86.0.518" newVersion="0.85.3.365" /> </dependentAssembly> </assemblyBinding> </runtime>
排查之后发现是因为xmlns的问题导致的
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">这里的xmlns和<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">里面的xmlns不能相互兼容导致的。
直接删掉configuration里面的xmlns,然后bindingRedirect就可以正常工作了
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-04-28 EvansClassification
2019-04-28 Catalog of Patterns of Enterprise Application Architecture
2019-04-28 ValueObject
2019-04-28 Data Transfer Object
2019-04-28 ASP.NET - Validators
2016-04-28 public static float CompareExchange(ref float location1,float value,float comparand)
2015-04-28 继承与方法重写