Azure Function Could not load file or assembly

150 views Asked by At

I am writing a function that grabs & decompresses a value from Redis using a key. So far I am able to successfully grab the value, however when I try to decompress the value using GZipStream, it throws this error:

FAILED- Could not load file or assembly 'System.IO.Compression, Version=7.0.0.0, Culture=neutral, PublicKeyToken=TOKEN'. The system cannot find the file specified.

(Despite having 'using System.IO.Compression' at the top of the file)

What I do not understand is that the specified file/assembly is being referenced in my solution's dependencies under the Microsoft.NETCore.App Framework. It is on my computer, within "C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\7.0.10\ref\net7.0\System.IO.Compression.dll", but not under the solution's packages, yet when re-building, no errors are thrown. Is there a way to explicitly reference the dll?

(All of the logic happens within the same file, what is also weird is that the error does not get thrown at the line that implements the package, but on the method call to the method that contains the line)

This is running locally and not in production yet. The target framework is .NET 7.

The solution is an Azure function, the purpose of it is to take in an HTML string (or Key to grab an HTML string from Redis) & return the byte[] for the pdf made from it.

Here is the code:

using IronPdf;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using ExecutionContext = Microsoft.Azure.WebJobs.ExecutionContext;


namespace NameSpace
{
    public static class Print
    {

        [FunctionName("PrintPdf")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "PDF/{GUID}")] HttpRequest req, string GUID, ILogger log, ExecutionContext context)
        {
            string result = "OK";
            log.LogInformation("Rendering PDF " + GUID);
            PdfDocument doc;

            //Get from redis
            var html = await GetContextFromRedis("keyUsedToGetValue");

            string reportHTML = "<!DOCTYPE html>\r\n<html>\r\n<body>\r\n\r\n<h1>Report</h1>\r\n\r\n<p>" + html + "</p>\r\n\r\n</body>\r\n</html>";
            doc = renderer.RenderHtmlAsPdf(reportHTML);
            string Filename = "report.pdf";

            log.LogInformation("finished rendering pdf...");
            return new FileContentResult(doc.BinaryData, "application/pdf") { FileDownloadName = Filename };
        }

        private static async Task<string> GetContextFromRedis(string Key)
        {
            try
            {
                Lazy<Task<ConnectionMultiplexer>> lazyConnection = new Lazy<Task<ConnectionMultiplexer>>(async () =>
                {
                    string Address = "address",
                        Password = "password";
                    var cacheConnection = $"{connection}";
                    return await ConnectionMultiplexer.ConnectAsync(cacheConnection).ConfigureAwait(false);
                });
                Task<ConnectionMultiplexer> Connection = lazyConnection.Value;

                IDatabase cache = (await Connection.ConfigureAwait(false)).GetDatabase();
                var compressedString = await cache.StringGetAsync(Key).ConfigureAwait(false);

                #region Decompress String

                byte[] gZipBuffer = Convert.FromBase64String(compressedString);
                using var memoryStream = new MemoryStream();

                int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
                memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);

                var buffer = new byte[dataLength];

                memoryStream.Position = 0;
                await using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    gZipStream.Read(buffer, 0, buffer.Length);
                }

                //compressedString = Encoding.UTF8.GetString(buffer);

                #endregion

                return compressedString;
                //return HttpUtility.UrlDecode(compressedString);
            }
            catch
            {
                return "";
            }
        }
    }
}
0

There are 0 answers