Blob 存储基础结构 IBlobContainer 依赖于已弃用的机制。我应该如何解决这个问题?

Blob Storage infrastructure IBlobContainer relies on deprecated mechanism. How should I fix this?

我正在实施通过 Volo.Abp.BlobStoring.IBlobContainer 接口提供的 BLOB Storing capability

我相信我已经正确编码和配置了所有内容,但微软最近的弃用让我想知道是否有比我正在尝试的更好的实现。

这是我的代码:

  public async Task StorePhotoAsync(Photo photo)
  {
    var photoBytes = ObjectToByteArray(photo);
    await _blobContainer.SaveAsync(photo.Id.ToString(), photoBytes);
  }

  // TODO - Reimplement (deprecated serialization) - JLavallet 2022-03-09 10:41 
  private byte[] ObjectToByteArray(object obj)
  {
    if (obj == null)
    {
      return null;
    }

    var bf = new BinaryFormatter();
    using var ms = new MemoryStream();
    bf.Serialize(ms, obj);

    return ms.ToArray();
  }

  // TODO - Reimplement (deprecated serialization) - JLavallet 2022-03-09 10:41 
  private object ByteArrayToObject(byte[] arrBytes)
  {
    using var memStream = new MemoryStream();
    var binForm = new BinaryFormatter();
    memStream.Write(arrBytes, 0, arrBytes.Length);
    memStream.Seek(0, SeekOrigin.Begin);
    var obj = binForm.Deserialize(memStream);

    return obj;
  }

正如您从我的待办事项评论和下面的屏幕截图中看到的那样,BinaryFormatter 的序列化和反序列化方法已被弃用:

有人可以建议替代方法吗? IBlobContainer 只想保存一个字节数组。

所以在 T 先生让我直截了当之后,我阅读了 JSON UTF8 serialization and deserialization 的文档,这是我想出的:

  public async Task StorePhotoCacheItemAsync(PhotoCacheItem photoCacheItem)
  {
    var bytes = PhotoCacheItemToByteArray(photoCacheItem);
    await _blobContainer.SaveAsync(photoCacheItem.Id.ToString(), bytes);
  }

  private byte[] PhotoCacheItemToByteArray(PhotoCacheItem photoCacheItem)
  {
    if (photoCacheItem == null)
    {
      return null;
    }

    // the old way
    //var bf = new BinaryFormatter();
    //using var ms = new MemoryStream();
    //bf.Serialize(ms, obj);

    // a new way
    //using var ms = new MemoryStream();
    //using var writer = new Utf8JsonWriter(ms);
    //JsonSerializer.Serialize(writer, photoCacheItem);
    //return ms.ToArray();

    // a better new way
    var bytes = JsonSerializer.SerializeToUtf8Bytes(photoCacheItem);
    return bytes;
  }

  private async Task<PhotoCacheItem> GetPhotoCacheItemFromBlobStorage(Guid photoId)
  {
    var bytes = await _blobContainer.GetAllBytesOrNullAsync(photoId.ToString());
    if (bytes == null)
    {
      return null;
    }

    var photoCacheItem = ByteArrayToPhotoCacheItem(bytes);
    return photoCacheItem;
  }

  private PhotoCacheItem ByteArrayToPhotoCacheItem(byte[] bytes)
  {
    // the old way
    //using var ms = new MemoryStream();
    //var bf = new BinaryFormatter();
    //ms.Write(bytes, 0, bytes.Length);
    //ms.Seek(0, SeekOrigin.Begin);
    //var obj = bf.Deserialize(ms);

    // a new way
    //var utf8Reader = new Utf8JsonReader(bytes);
    //var photoCacheItem = JsonSerializer.Deserialize<PhotoCacheItem>(ref utf8Reader)!;

    // a better new way
    var readOnlySpan = new ReadOnlySpan<byte>(bytes);
    var photoCacheItem = JsonSerializer.Deserialize<PhotoCacheItem>(readOnlySpan)!;

    return photoCacheItem;
  }