网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using MessagePack;
using Microsoft.AspNetCore.Mvc.Formatters;

namespace Dpz.Core.WebApi.MessagePackFormatters;

/// <summary>
///     MVC output formatter.
/// </summary>
public class MessagePackOutputFormatter : OutputFormatter
{
    readonly MessagePackSerializerOptions _options;

    /// <summary>
    ///     ctor
    /// </summary>
    /// <param name="options">MessagePack 格式化选项</param>
    /// <param name="mediaTypes">支持类型</param>
    /// <exception cref="T:System.ArgumentNullException">
    ///     <paramref name="options" /> or <paramref name="mediaTypes" /> is
    ///     <c>null</c>.
    /// </exception>
    /// <exception cref="T:System.ArgumentException"><paramref name="mediaTypes" /> collection is empty.</exception>
    public MessagePackOutputFormatter(
        MessagePackSerializerOptions options,
        ICollection<string> mediaTypes
    )
    {
        _options = options ?? throw new ArgumentNullException(nameof(options));
        if (mediaTypes == null)
            throw new ArgumentNullException(nameof(mediaTypes));
        if (mediaTypes.Count == 0)
            throw new ArgumentException("Media type must be specified.", nameof(mediaTypes));

        foreach (var mediaType in mediaTypes)
            SupportedMediaTypes.Add(mediaType);
    }

    /// <inheritdoc />
    public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
    {
        var writer = context.HttpContext.Response.BodyWriter;
        if (context.Object == null)
        {
            new MessagePackWriter(writer).WriteNil();
        }
        else
        {
            var objectType =
                context.ObjectType is null || context.ObjectType == typeof(object)
                    ? context.Object.GetType()
                    : context.ObjectType;

            MessagePackSerializer.Serialize(
                objectType,
                writer,
                context.Object,
                _options,
                context.HttpContext.RequestAborted
            );
        }

        await writer.FlushAsync();
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

上述代码定义了一个名为 MessagePackOutputFormatter 的类,它是 ASP.NET Core 中的一个输出格式化器,用于将响应数据序列化为 MessagePack 格式。MessagePack 是一种高效的二进制序列化格式,常用于网络通信和数据存储。

以下是代码的主要功能和组成部分的详细解释:

1. 类的定义

  • MessagePackOutputFormatter 继承自 OutputFormatter,这是 ASP.NET Core 中用于处理 HTTP 响应输出的基类。

2. 构造函数

  • 构造函数接受两个参数:
    • MessagePackSerializerOptions options:用于配置 MessagePack 序列化的选项。
    • ICollection<string> mediaTypes:支持的媒体类型(如 "application/x-msgpack")。
  • 构造函数中包含了参数的有效性检查:
    • 如果 optionsmediaTypesnull,则抛出 ArgumentNullException
    • 如果 mediaTypes 为空,则抛出 ArgumentException
  • 将支持的媒体类型添加到 SupportedMediaTypes 集合中。

3. WriteResponseBodyAsync 方法

  • 这是重写自 OutputFormatter 的方法,用于将响应体写入 HTTP 响应。
  • 方法的参数 OutputFormatterWriteContext context 包含了响应的上下文信息。
  • 方法的主要逻辑如下:
    • 获取 HttpContext.Response.BodyWriter,用于写入响应体。
    • 如果 context.Objectnull,则使用 MessagePackWriter 写入一个 nil 值。
    • 如果 context.Object 不为 null,则确定要序列化的对象类型。如果 context.ObjectTypenull 或者是 object 类型,则使用 context.Object 的实际类型。
    • 使用 MessagePackSerializer.Serialize 方法将对象序列化为 MessagePack 格式,并写入响应体。
    • 最后,调用 FlushAsync 方法确保所有数据都被写入响应。

总结

MessagePackOutputFormatter 类的主要功能是将 ASP.NET Core 的 HTTP 响应数据序列化为 MessagePack 格式,以便客户端能够以高效的二进制格式接收数据。它通过构造函数配置支持的媒体类型和序列化选项,并在 WriteResponseBodyAsync 方法中实现了具体的序列化逻辑。

loading