简体   繁体   English

加载 Bitmap 资源时,.net 5 应用程序不支持 BinaryFormatter

[英]BinaryFormatter not supported in .net 5 app when loading Bitmap resource

I have migrated a couple of ASP.Net Core 2.2 projects to.Net 5, the last issue I have is that I get a System.NotSupported exception when trying to load bitmaps from the project resources.我已经将几个 ASP.Net Core 2.2 项目迁移到 .Net 5,我遇到的最后一个问题是尝试从项目资源加载位图时出现 System.NotSupported 异常。

RtfUtility.AppendLogo(result, Properties.Resources.Logo);

System.NotSupportedException HResult=0x80131515 Message=BinaryFormatter serialization and deserialization are disabled within this application. System.NotSupportedException HResult=0x80131515 Message=BinaryFormatter 序列化和反序列化在此应用程序中被禁用。 See https://aka.ms/binaryformatter for more information.有关详细信息,请参阅https://aka.ms/binaryformatter Source=System.Runtime.Serialization.Formatters Source=System.Runtime.Serialization.Formatters

I'm not using BinaryFormatter explicitly but oddly I don't get an error when loading a binary PDF file in the same way:我没有明确地使用BinaryFormatter但奇怪的是我在以相同方式加载二进制 PDF 文件时没有收到错误:

processor.LoadDocument(new MemoryStream(Properties.Resources.Certificate));

Both mechanisms use ResourceManager.GetObject , so I'm not sure whats going on.两种机制都使用ResourceManager.GetObject ,所以我不确定发生了什么。 I know I can turn off the error in the project file, but that seems to be a short term solution, I'd rather fix it and forget it.我知道我可以关闭项目文件中的错误,但这似乎是一个短期解决方案,我宁愿修复它并忘记它。 Thanks for any advice you can give...感谢您提供的任何建议...

Edit:编辑:

Stack Trace is below, The error is not caused by the library, it is when accessing the resource.堆栈跟踪如下,该错误不是由库引起的,而是在访问资源时引起的。 It happens when the image is embedded or linked - seems to make no difference, but doesn't happen with a (binary) PDF file.它在图像被嵌入或链接时发生 - 似乎没有区别,但不会发生在(二进制)PDF 文件中。

Thanks for looking at this...谢谢你看这个...

at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) at System.Resources.ResourceReader.<>c__DisplayClass7_0`1.b__0(Object obj, Stream stream) at System.Resources.ResourceReader.DeserializeObject(Int32 typeIndex) at System.Resources.ResourceReader._LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode) at System.Resources.ResourceReader.LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode) at System.Resources.ResourceReader.LoadObject(Int32 pos, ResourceTypeCode& typeCode) at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase, Boolean isString) at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase) at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream) at System.Resources.ResourceManager.GetObject(在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) 在 System.Resources.ResourceReader.<>c__DisplayClass7_0`1.b__0(Object obj, Stream 流) 在 System.Resources.ResourceReader.DeserializeObject(Int32 typeIndex ) 在 System.Resources.Resources.ResourceReader.LoadObject(Int32 pos, ResourceTypeCode& typeCode) 在 System.Resources 的 System.Resources.ResourceReader.LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode) 的 System.Resources.ResourceReader._LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode) .RuntimeResourceSet.GetObject(String key, Boolean ignoreCase, Boolean isString) at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase) at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream) at System.资源.ResourceManager.GetObject( String name, CultureInfo culture) at Project.Infrastructure.Properties.Resources.get_Logo() in C:\Development\Project\Project.Infrastructure\Properties\Resources.Designer.cs:line 227 at Project.Infrastructure.Models.ShowWording.Generate() in C:\Development\Project\Project.Infrastructure\Models\ShowWording.cs:line 146字符串名称,CultureInfo 文化)在 Project.Infrastructure.Properties.Resources.get_Logo() in C:\Development\Project\Project.Infrastructure\Properties\Resources.Designer.cs:line 227 at Project.Infrastructure.Models.ShowWording.Generate () 在 C:\Development\Project\Project.Infrastructure\Models\ShowWording.cs:146 行

There is an option to re enable binaryformatter in .Net 5 asp.Net apps by adding the following to the project file.通过将以下内容添加到项目文件中,可以选择在 .Net 5 asp.Net 应用程序中重新启用 binaryformatter。

<PropertyGroup>
  <TargetFramework>net5.0</TargetFramework>
  <!-- Warning: Setting the following switch is *NOT* recommended in web apps. -->
  <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>

From BinaryFormatter serialization methods are obsolete and prohibited in ASP.NET apps .BinaryFormatter 序列化方法已过时并在 ASP.NET 应用程序中被禁止 Note the warnings about doing this, since binaryformatter is inherently insecure.请注意有关这样做的警告,因为 binaryformatter 本质上是不安全的。

If this is used indirectly by a library you might consider contacting the library vendor for an updated version, or switch to another library.如果这是由库间接使用的,您可能会考虑联系库供应商以获取更新版本,或切换到另一个库。

I found a workaround, that doesn't make sense, but I trust it more than allowing unsafe Binary Formatters in my projects.我找到了一个没有意义的解决方法,但我相信它而不是在我的项目中允许不安全的二进制格式化程序。

Following on from your suggestions I tried deleting and recreating the resource file, removing and re-adding the affected image, but nothing worked.根据您的建议,我尝试删除并重新创建资源文件,删除并重新添加受影响的图像,但没有任何效果。

So, because reading a pdf file worked, I removed the .png extension from my image and saved it as a File resource.因此,由于读取 pdf 文件有效,我从图像中删除了 .png 扩展名并将其保存为文件资源。

I can now read it from resources as binary and convert it to Bitmap and it works fine.我现在可以从资源中以二进制形式读取它并将其转换为 Bitmap 并且它工作正常。

using (var ms = new MemoryStream(Properties.Resources.LogoBinary))
{
   return  new Bitmap(ms);
}

I can't believe this is a bug in the framework, as it would affect a lot of people, it must be something peculiar to my project, but it is odd that I get the BinaryFormatter exception when reading an image, but not a Binary file...我不敢相信这是框架中的一个错误,因为它会影响很多人,它一定是我的项目所特有的,但奇怪的是我在读取图像时得到 BinaryFormatter 异常,而不是 Binary文件...

Thanks everyone for your guidance & assistance...感谢大家的指导和帮助...

It seems that Visual Studio/.Net previously made images added as resources accessible as Image , which makes use of the BinaryFormatter , while if I now add images they are accessible as byte[] .似乎 Visual Studio/.Net 以前将图像添加为可作为Image访问的资源,这利用了BinaryFormatter ,而如果我现在添加图像,它们可以作为byte[]访问。

I was able to update the resource file to only return byte[] by simply:我能够通过简单地更新资源文件以仅返回byte[]

  1. Replacing System.Drawing.Bitmap with byte[] in the *.Designer.cs file用 *.Designer.cs 文件中的byte[]替换System.Drawing.Bitmap
  2. Replacing System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a with System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 in the *.resx fileSystem.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3aSystem.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089中的 *。 .resx 文件

Previously:之前:

// In the *.Designer.cs file
public static System.Drawing.Bitmap MyImage {
  get {
    object obj = ResourceManager.GetObject("MyImage", resourceCulture);
    return ((System.Drawing.Bitmap)(obj));
  }
}

// In the *.resx file
<data name="MyImage" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>Path\To\MyImage.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
  </data>

Now:现在:

// In the *.Designer.cs file
public static byte[] MyImage {
  get {
    object obj = ResourceManager.GetObject("MyImage", resourceCulture);
    return ((byte[])(obj));
  }
}

// In the *.resx file
<data name="MyImage" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>Path\To\MyImage.jpg;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></value>
  </data>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM