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就可以正常工作了