NPOI强绑定ICSharpCode.SharpZipLib

NPOI.POIXMLException ---> System.IO.FileLoadException: Could not load file or assembly 'ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   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:Usersclusource eposConsoleApp1ConsoleApp1ChuckTest.cs:line 121
at ConsoleApp1.ChuckTest.Test() in C:Usersclusource eposConsoleApp1ConsoleApp1ChuckTest.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:ReleasePackagesLISA 4LISA.Installer.ClientProgramsLISA.ControlPanel",
                @"D:ReleasePackagesLISA 4LISA.Installer.ServerProgramsLISA.BackOffice",
                @"D:ReleasePackagesLISA 4LISA.Installer.ServerProgramsLISA.Batch",
                @"D:ReleasePackagesLISA 4LISA.Installer.ServerProgramsLISA.CMSWeb.Publicin",
                @"D:ReleasePackagesLISA 4LISA.Installer.ServerProgramsLISA.FileExchange",
                @"D:ReleasePackagesLISA 4LISA.Installer.ServerProgramsLISA.IISRemotingin",
                @"D:ReleasePackagesLISA 4LISA.Installer.ServerProgramsLISA.WebAPIin",
                @"D:ReleasePackagesLISA 4LISA.Installer.ServerProgramsLISA.WebServicein",
            };
            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就可以正常工作了

原文地址:https://www.cnblogs.com/chucklu/p/12792367.html