Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / Exception in D365 when...
Finance forum
Suggested answer

Exception in D365 when Using PDF/A-3 Creation DLL

Posted on by 12
Hi
I have developed a C# class library that creates a PDF/A-3 document with an XML attachment. The code functions correctly when executed as a console application, but I encounter an error when using it as a DLL in Dynamics 365 Finance and Operations.
 
Error : Method not found: 'Void Microsoft.Extensions.Logging.LoggerExtensions.LogError(Microsoft.Extensions.Logging.ILogger, System.Exception, System.String, System.Object[])'.
 
Code Snippet:
 
public static byte[] CreatePdfA3WithAttachment(byte[] pdfContent, byte[] xmlData, Stream iccProfileData)
{
    using (MemoryStream pdfStream = new MemoryStream())
    {
        PdfWriter writer = new PdfWriter(pdfStream);
        PdfADocument pdfDocument = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_3B,
                                                    new PdfOutputIntent("Custom ICC Profile", "", "https://www.color.org", "", iccProfileData));
        Document document = new Document(pdfDocument);
        pdfDocument.GetDocumentInfo().SetTitle("PDF/A-3 Document with XML Attachment");
        pdfDocument.GetDocumentInfo().SetAuthor("Author Name");
        pdfDocument.GetDocumentInfo().SetSubject("PDF/A-3 Document");
        pdfDocument.GetDocumentInfo().SetKeywords("PDF/A-3, XML, iText7");
        pdfDocument.SetTagged();
        pdfDocument.GetCatalog().SetLang(new PdfString("en-US"));
        pdfDocument.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
        using (MemoryStream existingPdfStream = new MemoryStream(pdfContent))
        {
            try
            {
                PdfReader pdfReader = new PdfReader(existingPdfStream);
                PdfDocument existingPdf = new PdfDocument(pdfReader);
                existingPdf.CopyPagesTo(1, existingPdf.GetNumberOfPages(), pdfDocument);
                existingPdf.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception while adding existing content: " + ex.Message);
            }
        }
        using (MemoryStream xmlStream = new MemoryStream(xmlData))
        {
            PdfDictionary fileParameter = new PdfDictionary();
            PdfFileSpec fileSpec = PdfFileSpec.CreateEmbeddedFileSpec(
                pdfDocument, xmlStream.ToArray(), "attached.xml", "attached.xml", PdfName.ApplicationXml, fileParameter, PdfName.Data);
            pdfDocument.AddFileAttachment("attached.xml", fileSpec);
            PdfArray afArray = new PdfArray();
            afArray.Add(fileSpec.GetPdfObject());
            pdfDocument.GetCatalog().Put(new PdfName("AF"), afArray);
        }
        document.Close();
        return pdfStream.ToArray();
    }
}
Any insights into why this exception occurs within D365 but not in a standalone application would be greatly appreciated. Additionally, advice on potential workarounds to avoid dependency conflicts with Microsoft.Extensions.Logging would be helpful.
Categories:
  • Martin Dráb Profile Picture
    Martin Dráb 229,173 Most Valuable Professional on at
    Exception in D365 when Using PDF/A-3 Creation DLL
    You didn't mention the version. You should develop against the version of Microsoft.Extensions.Logging.Abstractions.dll included in F&O, which is what your code will use at runtime. Maybe you developed code against a version that contains the method but F&O will actually run your code with a version where the method doesn't exist.
     
    In my environment, the version of Microsoft.Extensions.Logging.Abstractions.dll is 1.1.230427. You'll find it in PackagesLocalDirectory\bin folder.
  • Pratik545 Profile Picture
    Pratik545 12 on at
    Exception in D365 when Using PDF/A-3 Creation DLL
    I have added all the dependent Dll from which one of the Dll is Microsoft.Extensions.Logging.Abstraction which contains the LogError Method. But still I am not getting how this exception is being thrown.
     
    Here are the methods in the Dll.
    This application is built in .net Standard 2.0.3 and Dll I am using is iText7 along with all its dependent Dll.
     
    And also could you please suggest me how to get resolve from Nullable Reference as I am currently using the C# language version 7.3 but from the resource you have provided I can understand that Nullable Reference is supported in c# 8.0 later.
     
  • Martin Dráb Profile Picture
    Martin Dráb 229,173 Most Valuable Professional on at
    Exception in D365 when Using PDF/A-3 Creation DLL
    This error doesn't mean that an reference assembly wasn't found. The assembly got resolved, but the particular method (method overload) wasn't found there.
     
    It should exist (link), but it can't be found for some reason. I noticed that there are nullable reference types as parameters and I'm aware of a problem with them in X++, which was discussed in c# dll successfully added as a reference but can't be used in code. It says: Microsoft support official answer is that Nullable reference types are not yet available (no eta) in the x++ compiler and are not supported with Dynamics in the same way c# does. I'm not sure that this is the reason but it's quite possible.
  • Suggested answer
    Anton Venter Profile Picture
    Anton Venter 18,646 Super User 2024 Season 2 on at
    Exception in D365 when Using PDF/A-3 Creation DLL
    Your DLL is dependent on other DLLs and it cannot find the logging DLL. Perhaps you should copy the dependent DLLs to the PackagesLocalDIrectory/<yourmodel>/bin folder. Check the bin folder of your Visual Studio solution, all the files should be there.
  • Martin Dráb Profile Picture
    Martin Dráb 229,173 Most Valuable Professional on at
    Exception in D365 when Using PDF/A-3 Creation DLL
    What is the stack trace of the exception? LogError() apparently isn't used in CreatePdfA3WithAttachment() method.
     
    Which framework have you built your assembly for?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,867 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,173 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans