Skip to content

Error using System.Drawing.Common with FSI #57922

Closed

Description

@auslavs

When calling System.Drawing.Common within FSharp Interactive, the assembly cannot be loaded

error FS0193: Could not load file or assembly 'System.Drawing.Common, Version=5.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Repro fsx script for the error:

#r "nuget: System.Drawing.Common, 5.0.2"

open System.IO
open System.Drawing

let getBitmapWidth =
  use stream = new FileStream("image.jpg",FileMode.Open,FileAccess.Read)
  use bm = new Bitmap(stream)
  bm.Width

FSI version details:

Microsoft (R) F# Interactive version 11.4.2.0 for F# 5.0

Activity

  1. ghost added
    untriaged
    on Aug 23, 2021
  2. tarekgh commented on Aug 23, 2021

    @tarekgh
    Member

    @ericstj any thoughts what can cause that?

  3. added this to the 5.0.x milestone on Aug 23, 2021
  4. removed
    untriaged
    on Aug 23, 2021
  5. ericstj commented on Aug 23, 2021

    @ericstj
    Member

    I'm guessing it's an issue with FSI's loader for nuget packages. The package works fine when consumed from an application.
    sdc.zip

  6. am11 commented on Aug 23, 2021

    @am11
    Member

    Yup, dotnet fsi with #r "nuget: System.Drawing.Common, 5.0.2" seems to be (incorrectly?) picking up netcoreapp3.0, instead of netstandard2.0:

    file: /var/folders/lt/lqsrf7fs78d93xwjzhj62bg40000gn/T/nuget/24947--bb21e9ae-ff7d-4b0c-a258-233acaf14353/Project.fsproj.fsx

    // Generated from #r "nuget:Package References"
    // ============================================
    //
    // DOTNET_HOST_PATH:(C:\Program Files\dotnet\dotnet.exe)
    // MSBuildSDKsPath:(C:\Program Files\dotnet\sdk\3.1.200-preview-014883\Sdks)
    // MSBuildExtensionsPath:(C:\Program Files\dotnet\sdk\3.1.200-preview-014883\)
    //
    // References
    //
    #r @"/Users/am11/.nuget/packages/microsoft.win32.systemevents/5.0.0/lib/netstandard2.0/Microsoft.Win32.SystemEvents.dll"
    #r @"/Users/am11/.nuget/packages/system.drawing.common/5.0.2/runtimes/unix/lib/netcoreapp3.0/System.Drawing.Common.dll"

    (note the netcoreapp3.0 reference).
    If we explicitly reference the netstandard2.0 assembly instead, it works fine:

    $ dotnet fsi
    
    > #r "/Users/am11/.nuget/packages/system.drawing.common//5.0.2/lib/netstandard2.0/System.Drawing.Common.dll"
    - open System.IO
    - open System.Drawing
    - 
    - let getBitmapWidth =
    -   use stream = new FileStream("image.jpg",FileMode.Open,FileAccess.Read)
    -   use bm = new Bitmap(stream)
    -   bm.Width;;
    
    --> Referenced '/Users/am11/.nuget/packages/system.drawing.common//5.0.2/lib/netstandard2.0/System.Drawing.Common.dll' (file may be locked by F# Interactive process)
    
    ...
  7. ericstj commented on Aug 24, 2021

    @ericstj
    Member

    netcoreapp3.0 is "better" than netstandard2.0 for a net5.0 project. See https://nugettools.azurewebsites.net/5.11.0/framework-precedence?framework=net5.0&exludedIdentifiers=

    So choosing netcoreapp3.0 is correct, you can see that in the project I attached if you restore and examine the assets file you can see what an actual project will resolve. Choosing a runtime asset is dubious for reference, how I can see why this might be correct for interactive since here reference and runtime are the same thing.

    That doesn't really explain why fsi couldn't load it though. Your workaround of using ns2.0 is probably just treating the symptom. Something in the runtime loader for fsi isn't consistent with how it's compiling.

    This issue should be transferred to https://github.com/dotnet/fsharp. @KevinRansom @TIHan @cartermp can you help with that?

  8. tarekgh commented on Aug 24, 2021

    @tarekgh
    Member

    This issue was moved to dotnet/fsharp#12023

  9. locked and limited conversation to collaborators on Aug 24, 2021
  10. removed this from the 5.0.x milestone on Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions