nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.72k stars 1.43k forks source link

Could not load file or assembly 'NPOI.OpenXmlFormats.XmlSerializers #143

Closed anuragdhawan closed 4 years ago

anuragdhawan commented 5 years ago

Hi @tonyqus , @antony-liu , @jstedfast I have installed the NPOI 2.4.0 NuGet package in to my project to read and write the excel (xlsx) files. I am first reading an excel file and evaluating the formulas. I am then writing that workbook to a different location. I am using XSSFWorkBook.Write(FileStream) function for that. But I am getting the following error message:

System.IO.FileNotFoundException HResult=0x80070002 Message=Could not load file or assembly 'NPOI.OpenXmlFormats.XmlSerializers, Version=2.4.0.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1' or one of its dependencies. The system cannot find the file specified. Source=mscorlib StackTrace: at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

The code generates a corrupted excel file. Note: The NuGet package has installed all the required dlls and dependencies.

Could you please advise what am I missing?

Thanks. -Anu

tonyqus commented 5 years ago

which .NET version are you using?

Chang228 commented 5 years ago

我使用 2.3 和 2.4 版本保存xlsx格式都会有相同错误,.net 4.6.1

引发了异常: mscorlib.dll 中的“System.IO.FileNotFoundException”(“未能加载文件或程序集“NPOI.OpenXmlFormats.XmlSerializers, Version=2.3.0.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1”或它的某一个依赖项。系统找不到指定的文件。”) 引发了异常: mscorlib.dll 中的“System.IO.FileNotFoundException”(“未能加载文件或程序集“NPOI.OpenXmlFormats.XmlSerializers, Version=2.3.0.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1”或它的某一个依赖项。系统找不到指定的文件。”)

引发了异常: NPOI.OpenXml4Net.dll 中的“System.TypeAccessException”(“安全透明方法“NPOI.OpenXml4Net.OPC.ZipPackage.SaveImpl(System.IO.Stream)”尝试访问安全关键类型“ICSharpCode.SharpZipLib.Zip.ZipOutputStream”失败。

程序集“NPOI.OpenXml4Net, Version=2.3.0.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1”标记为 AllowPartiallyTrustedCallersAttribute 并且使用 2 级安全透明模型。默认情况下,2 级透明将导致 AllowPartiallyTrustedCallers 程序集中的所有方法都变成安全透明的,这可能是导致发生此异常的原因。”) 引发了异常: NPOI.OpenXml4Net.dll 中的“System.TypeAccessException”(“安全透明方法“NPOI.OpenXml4Net.OPC.ZipPackage.SaveImpl(System.IO.Stream)”尝试访问安全关键类型“ICSharpCode.SharpZipLib.Zip.ZipOutputStream”失败。

程序集“NPOI.OpenXml4Net, Version=2.3.0.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1”标记为 AllowPartiallyTrustedCallersAttribute 并且使用 2 级安全透明模型。默认情况下,2 级透明将导致 AllowPartiallyTrustedCallers 程序集中的所有方法都变成安全透明的,这可能是导致发生此异常的原因。”)

klexas commented 5 years ago

Could we get a response on this.. I am getting it intermittently also..

dotNet 4.6.2

Working reproduction:

IWorkbook xssfwb;
using (FileStream fs = new FileStream(ts.ExcelFilePath, FileMode.Create, FileAccess.Write))
                {
                    xssfwb.Write(fs);
                    fs.Close();
                }
chucklu commented 4 years ago

I did a test on version 2.5.1, the same problem still exists. I got some detail info as below NPOI.OpenXmlFormats.XmlSerializers, Version=2.5.1.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1.HTM.zip

Pre-bind state information LOG: DisplayName = NPOI.OpenXmlFormats.XmlSerializers, Version=2.5.1.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL (Fully-specified) LOG: Appbase = file:///C:/Users/clu/source/repos/ConsoleApp1/ConsoleApp1/bin/Debug/ LOG: Initial PrivatePath = NULL LOG: Dynamic Base = NULL LOG: Cache Base = NULL LOG: AppName = ConsoleApp1.exe Calling assembly : System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.

LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Users\clu\source\repos\ConsoleApp1\ConsoleApp1\bin\Debug\ConsoleApp1.exe.Config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: NPOI.OpenXmlFormats.XmlSerializers, Version=2.5.1.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL LOG: GAC Lookup was unsuccessful. LOG: Attempting download of new URL file:///C:/Users/clu/source/repos/ConsoleApp1/ConsoleApp1/bin/Debug/NPOI.OpenXmlFormats.XmlSerializers.DLL. LOG: Attempting download of new URL file:///C:/Users/clu/source/repos/ConsoleApp1/ConsoleApp1/bin/Debug/NPOI.OpenXmlFormats.XmlSerializers/NPOI.OpenXmlFormats.XmlSerializers.DLL. LOG: Attempting download of new URL file:///C:/Users/clu/source/repos/ConsoleApp1/ConsoleApp1/bin/Debug/NPOI.OpenXmlFormats.XmlSerializers.EXE. LOG: Attempting download of new URL file:///C:/Users/clu/source/repos/ConsoleApp1/ConsoleApp1/bin/Debug/NPOI.OpenXmlFormats.XmlSerializers/NPOI.OpenXmlFormats.XmlSerializers.EXE. LOG: All probing URLs attempted and failed.

According the log, it means the NPOI.OpenXmlFormats.XmlSerializers was referenced by System.Xml. How could it be possible? https://indexoutofrange.com/Could_not_load_file_or_assembly_or_one_of_its_dependencies/

chucklu commented 4 years ago

submit a bug report for Fuslogvw here https://developercommunity.visualstudio.com/content/problem/1013037/bug-in-fuslogvwexe.html

chucklu commented 4 years ago

Response from https://developercommunity.visualstudio.com/content/problem/1013037/bug-in-fuslogvwexe.html

In this case it is likely that System.Xml caused types to load in NPOI.OpenXmlFormats.XmlSerializers, which caused the assembly to be loaded (or searched for). This is not uncommon for framework types to take type parameters from user code. Thanks for following up on this, but from what you shared in the post this looks like it is working as expected.

My recommendation is to consider the call that is causing the assembly load failure.

chucklu commented 4 years ago

@antony-liu @tonyqus @klexas Please take a look at https://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor

Like Martin Sherburn said, this is normal behavior. The constructor of the XmlSerializer first tries to find an assembly named [YourAssembly].XmlSerializers.dll which should contain the generated class for serialization of your type. Since such a DLL has not been generated yet (they are not by default), a FileNotFoundException is thrown. When that happenes, XmlSerializer's constructor catches that exception, and the DLL is generated automatically at runtime by the XmlSerializer's constructor (this is done by generating C# source files in the %temp% directory of your computer, then compiling them using the C# compiler). Additional constructions of an XmlSerializer for the same type will just use the already generated DLL.

UPDATE: Starting from .NET 4.5, XmlSerializer no longer performs code generation nor does it perform compilation with the C# compiler in order to create a serializer assembly at runtime, unless explicitly forced to by setting a configuration file setting (useLegacySerializerGeneration). This change removes the dependency on csc.exe and improves startup performance. Source: .NET Framework 4.5 Readme, section 1.3.8.1.

The exception is handled by XmlSerializer's constructor. There is no need to do anything yourself, you can just click 'Continue' (F5) to continue executing your program and everything will be fine. If you're bothered by the exceptions stopping the execution of your program and popping up an exception helper, you either have 'Just My Code' turned off, or you have the FileNotFoundException set to break execution when thrown, instead of when 'User-unhandled'.

To enable 'Just My Code', go to Tools >> Options >> Debugging >> General >> Enable Just My Code. To turn off breaking of execution when FileNotFound is thrown, go to Debug >> Exceptions >> Find >> enter 'FileNotFoundException' >> untick the 'Thrown' checkbox from System.IO.FileNotFoundException.

chucklu commented 3 years ago

@chucklu mark this issue

IbrahimMicroware commented 1 year ago

Could not load file or assembly 'NPOI.OOXML, Version=2.5.3.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1' or one of its dependencies. The system cannot find the file specified. why i am getting this issue suddenly