HOWTO: Use "single file deployment" to publish a DotImage application using .NET Core


There is a known .NET 6 issue when publishing single file applications containing both manage and unmanaged code leads to crashing with Systsem.AccessViolationException

For DotImaage .NET Core applications this may result in the error below.  

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at <Module>.g()
--------------------------------
at <Module>.d(atalab2v*)
at <Module>.a(atalab2v*)
at <Module>..cctor()
at Atalasoft.Imaging.AtalaImage..cctor()
at Atalasoft.Imaging.AtalaImage..ctor(System.String)
at Net6AtalaConsole.Program.Main(System.String[])

If you receive this error when pubilishing using the "single file deployment" setting you can make the following project and publish setting changes to resolve this error

1. Modify the .csproj file so that any for files that must be copied to output directory also have the property "ExcludeFromSingleFile"

  <ItemGroup>
    <None Update="images\Tiger.jpg">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
                 <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
    </None>
  </ItemGroup>

2. The publish command should contain parameter IncludeAllContentForSelfExtract. This is necessary if assembly has references to C++/Cli:

The complete publish command should look like this.

dotnet publish -r win-x64 -p:PublishSingleFile=true -p:IncludeAllContentForSelfExtract=true --self-contained true