Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace Activator.CreateInstance to Func for avoiding error in NativeAOT #716

Merged
merged 3 commits into from
Dec 21, 2022

Conversation

itn3000
Copy link
Contributor

@itn3000 itn3000 commented Dec 21, 2022

Overview

This fixes System.MissingMethodException when using XZ decompressor in NativeAOT.

Steps of reproduce

  1. create new console project and set TargetFramework to net7.0
  2. add SharpCompress package(this issue was investigated in 0.32.2)
  3. edit cs to following reproducible code
  4. publish with NativeAOT(and rid) option
  5. create xz archive file
  6. execute published executable file

Repro code

using SharpCompress.Compressors.Xz;

using(var istm = File.OpenRead("test.xz"))
using(var xstm = new XZStream(istm))
using(var ostm = File.Create("test.txt"))
{
    xstm.CopyTo(ostm);
}

Expected

"test.txt" is created and written decompressed result

Actual

System.MissingMethodException is thrown.

Unhandled Exception: System.MissingMethodException: No parameterless constructor defined for type 'SharpCompress.Compressors.Xz.Filters.Lzma2Filter'.
   at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x208
   at System.Reflection.Runtime.General.ReflectionCoreCallbacksImplementation.ActivatorCreateInstance(Type, Boolean) + 0x2c
   at System.Activator.CreateInstance(Type, Boolean) + 0x48
   at System.Activator.CreateInstance(Type) + 0x20
   at SharpCompress.Compressors.Xz.Filters.BlockFilter.Read(BinaryReader) + 0x147
   at SharpCompress.Compressors.Xz.XZBlock.ReadFilters(BinaryReader, Int64) + 0x64
   at SharpCompress.Compressors.Xz.XZBlock.LoadHeader() + 0xdd
   at SharpCompress.Compressors.Xz.XZBlock.Read(Byte[], Int32, Int32) + 0x3f
   at SharpCompress.Compressors.Xz.XZStream.ReadBlocks(Byte[], Int32, Int32) + 0x89
   at SharpCompress.Compressors.Xz.XZStream.Read(Byte[], Int32, Int32) + 0x69
   at System.IO.Stream.CopyTo(Stream, Int32) + 0x155
   at System.IO.Stream.CopyTo(Stream) + 0x58
   at Program.<Main>$(String[]) + 0x82
   at xzcomptest!<BaseAddress>+0x2b9c57
   at xzcomptest!<BaseAddress>+0x2b9cea

Workaround

Currently, this can be avoided by adding class under SharpCompress.Compressors.Xz.Filters in rd.xml.
here is example of rd.xml

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
    <Application>
        <Assembly Name="SharpCompress">
            <Type Name="SharpCompress.Compressors.Xz.Filters.Lzma2Filter" Dynamic="Required All"/>
        </Assembly>
    </Application>
</Directives>

@adamhathcock
Copy link
Owner

Thanks. Definitely getting SharpCompress doing NativeAOT is good

@adamhathcock adamhathcock merged commit 3f24af3 into adamhathcock:master Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants