dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.73k stars 1.07k forks source link

BinaryFormatter is disabled in WinForms or WPF projects in preview6 #32941

Closed Tanya-Solyanik closed 1 year ago

Tanya-Solyanik commented 1 year ago

Describe the bug

The intent behind this change - https://github.com/dotnet/sdk/commit/ccfcbe828cda905f1f995f63f26485dd9a7a7dd5 was that projects with these properties

    <UseWindowsForms>true</UseWindowsForms>
    <UseWPF>true</UseWPF>

Have BinaryFormatter.Serialize/Deserialize methods enabled in .NET8. However in SDK Preview6, we are getting a NotSupportedException

To Reproduce

  1. Install the latest SDK
  2. Create a winforms application targeting .NET 8 in the VS (app is here https://github.com/Tanya-Solyanik/Repro)
  3. Add <NoWarn>SYSLIB0011</NoWarn> to the project file
  4. Add a call to BinaryFormatter.Serialize()
  5. Run that code

Result: _System.NotSupportedException: BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information. at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) at WinFormsApp29.Form1.button1Click(Object sender, EventArgs e) in C:\Users\tanyaso\source\repos\WinFormsApp29\WinFormsApp29\Form1.cs:line 17

Further technical details

Tanya-Solyanik commented 1 year ago

This is blocking WinForms out of process designer from using resources and controls, such as ImageList. https://github.com/microsoft/winforms-designer/issues/5233

GrabYourPitchforks commented 1 year ago

This appears to be a regression from 8.0 Preview 4, where the scenario worked correctly. I'm trying to narrow down the change.

GrabYourPitchforks commented 1 year ago

Some initial assessment of various file contents follow. Note the unintentional inclusion of EnableUnsafeBinaryFormatterSerialization = false by default in the coreapp's runtimeconfig.json for P5 and the WindowsDesktop's runtimeconfig.json for P6. I'll ask around to see if anybody knows how these settings got added to these files.

Preview 4 (BF works by default in WinForms)

C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0-preview.4.23259.5\Microsoft.NETCore.App.runtimeconfig.json

{
  "runtimeOptions": {
    "tfm": "net8.0",
    "configProperties": {
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false
    }
  }
}

C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\8.0.0-preview.4.23260.1\Microsoft.WindowsDesktop.App.runtimeconfig.json

{
  "runtimeOptions": {
    "tfm": "net8.0",
    "rollForward": "LatestPatch",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "8.0.0-preview.4.23259.5"
    },
    "configProperties": {
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false
    }
  }
}

Preview 5 (BF fails in WinForms)

C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0-preview.5.23280.8\Microsoft.NETCore.App.runtimeconfig.json

{
  "runtimeOptions": {
    "tfm": "net8.0",
    "configProperties": {
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
    }
  }
}

C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\8.0.0-preview.5.23281.1\Microsoft.WindowsDesktop.App.runtimeconfig.json

{
  "runtimeOptions": {
    "tfm": "net8.0",
    "rollForward": "LatestPatch",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "8.0.0-preview.5.23280.8"
    },
    "configProperties": {
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false
    }
  }
}

Preview 6 (BF fails in WinForms)

C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.0-preview.6.23280.5\Microsoft.NETCore.App.runtimeconfig.json

{
  "runtimeOptions": {
    "tfm": "net8.0",
    "configProperties": {
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
    }
  }
}

C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\8.0.0-preview.6.23280.3\Microsoft.WindowsDesktop.App.runtimeconfig.json

{
  "runtimeOptions": {
    "tfm": "net8.0",
    "rollForward": "LatestPatch",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "8.0.0-preview.5.23276.1"
    },
    "configProperties": {
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
    }
  }
}
GrabYourPitchforks commented 1 year ago

The root cause of this bug seems to be https://github.com/dotnet/sdk/issues/32969.

Amy-Li03 commented 1 year ago

Verified issue https://github.com/microsoft/winforms-designer/issues/5233 on .NET SDK 8.0.100-preview.5.23301.12 test pass build, it was fixed. In winforms VB project, 'Explorer Form' template form designer(contains ImageList) can be loaded successfully. Fixed

marcpopMSFT commented 1 year ago

https://github.com/dotnet/sdk/pull/33004