Skip to content

AssemblyLoadContext.LoadFromStream relys on Length being available #81737

Open
@MaxwellDAssistek

Description

@MaxwellDAssistek

Description

When using AssemblyLoadContext.LoadFromStream to load an assembly from a Stream that is returned by FileSystem.OpenAppPackageFileAsync, I end up with a System.NotSupportedException on Android. I started with a ticket in Xamarin.Android: dotnet/android#7763. However, in the end it was determined that there is no way to reliably get the Length of an Asset Stream in Android.

I believe that in this function, it might be possible to avoid depending on the Stream.Length.

static ReadOnlySpan<byte> ReadAllBytes(Stream stream)

Reproduction Steps

        using (var resStream = FileSystem.OpenAppPackageFileAsync("MyModule.dll").Result)
        {
            var loadCtx = new AssemblyLoadContext("Module");

            var assem = loadCtx.LoadFromStream(resStream);
        }

Expected behavior

LoadFromStream should not require knowing the Stream Length.

Actual behavior

LoadFromStream causes an exception due to requiring to get the Stream Length.

Regression?

No response

Known Workarounds

Copy the stream into a MemoryStream first.

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions