Exception "Attempt by security transparent method 'NPOI.OpenXml4Net.OPC.ZipPackage"

Viewed 1978

I am trying to read from an excel file in C# using NPOI. Below is the code snippet I am using to initialize my variables. But while initializing the workbook, below is the exception I am getting.

public class Excelhandler
{
    public static XSSFWorkbook workbook;
    public static FileStream fileStream;
    static Excelhandler()
    {
        try
        {
            string pth = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
            string projectPath = new Uri(actualPath).LocalPath;
            string path = projectPath + @"TestData\Data.xlsx";
            //string path = ConfigurationManager.AppSettings["TestDataPath"];
            fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
            workbook = new XSSFWorkbook(fileStream);

        }

The same piece of code was working in some other C# project but suddenly it's not working. Below are the versions I am using : -

NPOI-2.3.0

SharpZipLib(NPOI Dependency) - 1.0.0

.Net Framework -4.5.2

Exception - Attempt by security transparent method 'NPOI.OpenXml4Net.OPC.ZipPackage..ctor(System.IO.Stream, NPOI.OpenXml4Net.OPC.PackageAccess)' to access security critical method 'ICSharpCode.SharpZipLib.Zip.ZipInputStream..ctor(System.IO.Stream)' failed.

2 Answers

I also met this problem with VS2017, NPOI 2.3.0 and SharpZipLib 1.0.0.

Thanks CrazyDev.

I didn't solve the problem changing the verison of the SharpZipLib to 0.86 in NuGet. After test, I found that it should also delete the below content in the App.config if it has that:

    <assemblyIdentity name="ICSharpCode.SharpZipLib" publicKeyToken="1b03e6acf1164f73" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-1.0.0.999" newVersion="1.0.0.999"/>

So that, not only roll back the verison of the SharpZipLib to 0.86, but also modify the App.config.

Same exception in my face when updating SharpZipLib Nuget to 1.0.0. Rolled back to minimun version 0.86.0 as indicated in NPOI 2.3.0 Nuget dependency and everything working again. Its something.

Related