Aspose.cells not working in azure functions (.net core 3.1)

We are evaluating the aspose.cells (evaluation version) to perform excel creation and it has to run as part of azure function app. Everything works locally on the development environment, but having issues when azure function app deployed.

Please find the error details.
Error|The type initializer for ‘Aspose.Cells.Workbook’ threw an exception

System.TypeInitializationException: The type initializer for ‘Aspose.Cells.Workbook’ threw an exception.
—> System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0

I have checked the references and those are added correctly.

aspose.cells version used in project: 21.1.0

Code sample
Workbook workbook = new Workbook(FileFormatType.Xlsx);
Worksheet worksheet = workbook.Worksheets[0];

Could you please help regarding this?

@avijit12,

Please note, latest version of Aspose.Cells for NetStandard20 assembly (Aspose.Cells v21.1) also requires to install System.Drawing.Common (version >= 5.0.0) and System.Text.Encoding.CodePages (version >= 5.0.0) APIs to make it work. So, you should install these APIs manually (you may get these namespaces from nuget). Alternatively, try installing the new version (Aspose.Cells for .NET v21.1) from NuGet repos. (if possible) as when you get/install Aspose.Cells from nuget repos., these additional dependencies (APIs) will be installed automatically.

If this still does not work after you have installed/referenced System.Text.Encoding.CodePages in your .NetCore program, you may try to call the following method to register encoding Provider, see the line of code which you need to add the line at the start:
i.e.,
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
(we also provide “CellsHelper.InitForDotNetCore()” method, and you may also call it first for all .NetCore initializations).

Hope, this helps a bit

Hi @Amjad_Sahi,

Thank you so much for the response.
Aspose.cells was installed using nuget and required dependencies were already present in project.
But, we tried adding the the below lines in code.

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
CellsHelper.InitForDotNetCore();

                Workbook workbook = new Workbook(FileFormatType.Xlsx); 

But still having the following issue,

Error|Could not load file or assembly ‘System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’. The system cannot find the file specified.|457af7ee-fd55-4383-8da6-16f152856212

Could you please help?

@avijit12,

Please create a separate VS.NET Core project (runnable) to reproduce the issue on our end, zip the project and post us, we will check it soon.

Hi @Amjad_Sahi ,

Thank you so much for your help.
This is a function app specific issue, we downgraded the aspose.cells version to v 20.12.0 and it worked.

Thanks,
Avijit

@avijit12,
Good to know that your issue is sorted. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Hi @avijit12
If it works fine on v 20.12.0 , but fail on 21.1.0, the reason is:
On 21.1.0, we upgraded the nuget package version:
System.Text.Encoding.CodePages 4.7.0 -> 5.0.0
System.Drawing.Common 4.7.0 -> 5.0.0

Please update them in your configuration, you will be able to use 21.1.0.
Hope it will help you.

Hi @leoluo ,

We have installed v21.1.0 via nuget package and all the dependencies are also got added(System.Text.Encoding.CodePages 5.0.0 and System.Drawing.Common 5.0.0). But, still it had error. But this happens only when we try from azure function app, app service(api app) does not have any issues.

Thanks,
Avijit

@avijit12,

May be you could refer to the thread on how to correctly use nuget packages in azure functions for your complete reference.

I create an empty Azure Function APP, only reference “System.Text.Encoding.CodePages”, try to call:

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

When I set “System.Text.Encoding.CodePages” version to 4.7.0, this Function app run without any exception, but if the version set to 5.0.0, will fail.
To deal with this, we decided to revert the NuGet package version referenced by Aspose.Cells to the previous version:

System.Drawing.Common 5.0.0 - > 4.7.0
System.Text.Encoding.CodePages 5.0.0 - > 4.7.0

New fix will publish soon.

Hi @avijit12
we revert the NuGet package version referenced by Aspose.Cells to the previous version:
System.Drawing.Common 5.0.0 - > 4.7.0
System.Text.Encoding.CodePages 5.0.0 - > 4.7.0

Please try the latest fix 21.1.4.
Aspose.Cells21.1.4 For .Net2_AuthenticodeSigned.Zip (5.5 MB)
Aspose.Cells21.1.4 For .Net4.0.Zip (5.5 MB)
Aspose.Cells21.1.4 For .NetStandard20.Zip (5.5 MB)

I had a similar issue in Aspose.PSD, but not with Azure functions, where I had a .net6 project which needed to be re-made in aspnetcore 3.1. The CodePages assembly being searched was 5.0.0, but the Aspose.PSD packages had 4.7.0 and the csproj file only said 4.7.0. After using NuGet to install the CodePages 5.0.0, the error went away.
This has almost nothing to do with the post here except, I was using this post and got the clues from it to try installing 5.0.0 to see if that would work (it seems in reverse of the instruction for this problem), and sure enough it did. It seems to be an obscure topic because all the other threads I find on it are for Java and not C#.