Search code examples
c#.net-coregembox-spreadsheet

Why does Gembox spreadsheet crash when calling Excelfile.save: FileNotFoundException Could not load file or assembly System.Security.Permissions


I am using Gembox to open, modify and save a xlsx file. Calling Save on the Excelfile causes a System.IO.FileNotFoundException.

The problem happens with our company serial key and with the free key.

Sample Code

using GemBox.Spreadsheet;
namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        { 
            var path = @"C:\code\GemboxTest\App.xlsx"; 
            SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
            ExcelFile ef = ExcelFile.Load(path); 
            ExcelWorksheet ws = ef.Worksheets[0]; 
            //ws.Columns[0].Cells[0].Value = 42; 
            ef.Save(path); // <--------------------------------- Crash!
        }
    }
}

Error message

Could not load file or assembly 'System.Security.Permissions, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
System.Security.Permissions, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51

Stacktrace

at   .(Stream , Byte[] , Int32 , Int32 , String )
at   .(Stream )
at   .(Stream )
at   . ()
at   .(Stream )
at    .Dispose()
at   .   ​ (Boolean )
at   .Dispose()
at    .    ​ ()
at    .(Boolean )
at    .Dispose()
at GemBox.Spreadsheet.XlsxSaveOptions.(ExcelFile , Stream ,     )
at GemBox.Spreadsheet.XlsxSaveOptions.Save(ExcelFile excelFile, Stream stream, String path)
at GemBox.Spreadsheet.SaveOptions.(ExcelFile , String )
at GemBox.Spreadsheet.ExcelFile.Save(String path, SaveOptions options)
at GemBox.Spreadsheet.ExcelFile.Save(String path)
at ConsoleApp.Program.Main(String[] args) in C:\code\GemboxTest\ConsoleApp\Program.cs:line 14

Versions

  1. .NET Core 3.1 (also fails with 3.0)
  2. GemBox.Spreadsheet Version=45.0.1131
  3. Visual Studio 2019 (VisualStudioVersion = 16.0.29905.134)
  4. Windows 10 pro 64bit

Sample csproj file

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GemBox.Spreadsheet" Version="45.0.1131" />
</ItemGroup>
</Project>

Solution

  • It appears the package has a dependency on an assembly that isn't referenced or included.

    Typically, a package author would reference their dependencies in their nuspec file for the Targetframework (each TargetFramework).


    You can work around it by adding the latest System.Security.Permissions nuget package as a dependency to your project.


    Update 1: Additionally

    Looking at their example project github repo, I do see references to netcoreapp3.1 there.

    I tested both netcoreapp3.1 and netcoreapp3.0, received the dependency package issue with Save, worked around it by adding it as a dependency (as this answer suggests). Gembox.Spreadsheet.Example

    The example and usage of netcoreapp2.2 had no issues with missing dependencies of the package when attempting a Save.

    Update 2

    Removed reference to observed issue with Load mentioned in Update 1. It appears to be unrelated and probably a runtime and\or IDE issue I was experiencing.

    Also, this issue was only tested and observed with a console application on windows platform.