SharpDocx icon indicating copy to clipboard operation
SharpDocx copied to clipboard

SharpDocxCompilationException: Exception of type 'SharpDocx.SharpDocxCompilationException' was thrown.

Open nathan130200 opened this issue 3 years ago • 1 comments

Unhandled exception. SharpDocx.SharpDocxCompilationException: Exception of type 'SharpDocx.SharpDocxCompilationException' was thrown.
   at SharpDocx.DocumentCompiler.Compile(String sourceCode, List`1 referencedAssemblies)
   at SharpDocx.DocumentCompiler.Compile(String viewPath, String className, String baseClassName, Type modelType, List`1 usingDirectives, List`1 referencedAssemblies)
   at SharpDocx.DocumentAssembly..ctor(String viewPath, Type baseClass, Type modelType)
   at SharpDocx.DocumentFactory.Create(String viewPath, Object model, Type baseClassType, Boolean forceCompile)
   at Program.<Main>$(String[] args) in C:\Users\Nathan\Desktop\historico-html\v1.3\Program.cs:line 8

When attempt generate document using model.

Work-around:

Extend model with DocumentBase and leave all abstract implementations empty.

Working Code:

using SharpDocx;

var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "base.cs.docx");
var outputFile = Path.Combine(Directory.GetCurrentDirectory(), "output.docx");

var model = new DocumentVM()
{
    strRandomGuid =  Guid.NewGuid().ToString("N")
};

var document = DocumentFactory.Create<DocumentVM>(inputFile, model);
document.Generate(outputFile, model);

public class DocumentVM : DocumentBase
{
    public string strRandomGuid { get; set; } = Guid.NewGuid().ToString("N");

    public override void SetModel(object model)
    {
        // keep empty         
    }

    protected override void InvokeDocumentCode()
    {
        // keep empty 
    }
}

My suggestion, make these two methods virtual instead of abstract so we don't must be forced to reimplement these.

Generated document: output.docx

And removing DocumentBase from inheritance class:

public class DocumentVM
{
    public string strRandomGuid { get; set; }
}

Raises the exception:

Unhandled exception. SharpDocx.SharpDocxCompilationException: Exception of type 'SharpDocx.SharpDocxCompilationException' was thrown.
   at SharpDocx.DocumentCompiler.Compile(String sourceCode, List`1 referencedAssemblies)
   at SharpDocx.DocumentCompiler.Compile(String viewPath, String className, String baseClassName, Type modelType, List`1 usingDirectives, List`1 referencedAssemblies)
   at SharpDocx.DocumentAssembly..ctor(String viewPath, Type baseClass, Type modelType)
   at SharpDocx.DocumentFactory.Create(String viewPath, Object model, Type baseClassType, Boolean forceCompile)
   at Program.<Main>$(String[] args) in C:\Users\Nathan\Desktop\historico-html\v1.3\Program.cs:line 6 ``

nathan130200 avatar Mar 15 '22 11:03 nathan130200

Did you try to figure out what the error actually is? This can be made by accessing the SharpDocxCompilationException .Errors and .SourceCode properties.

SLAVONchick avatar Oct 21 '24 15:10 SLAVONchick