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

namespace Dpz.Core.WebApi.MessagePackFormatters;

/// <summary>
///     格式化选项设置
/// </summary>
public class MessagePackFormatterMvcOptionsSetup(
    IOptions<MessagePackFormatterOptions> messagePackFormatterOptions
) : IConfigureOptions<MvcOptions>
{
    private readonly IOptions<MessagePackFormatterOptions> _messagePackFormatterOptions =
        messagePackFormatterOptions
        ?? throw new ArgumentNullException(nameof(messagePackFormatterOptions));

    /// <inheritdoc />
    /// <exception cref="T:System.InvalidOperationException">不支持的类型</exception>
    /// <exception cref="T:System.ArgumentNullException"><paramref name="options" /> is <see langword="null" /></exception>
    public void Configure(MvcOptions options)
    {
        if (options == null)
            throw new ArgumentNullException(nameof(options));

        var formatterOptions = _messagePackFormatterOptions.Value;
        var supportedMediaTypes = formatterOptions
            .MediaTypes.Where(t => !string.IsNullOrWhiteSpace(t))
            .ToArray();
        if (supportedMediaTypes.Length == 0)
            throw new InvalidOperationException("No supported media types were specified.");

        var msgpackOptions = MessagePackSerializerOptions.Standard;
        msgpackOptions = msgpackOptions.WithResolver(
            formatterOptions.FormatterResolver ?? ContractlessStandardResolver.Instance
        );
        msgpackOptions = msgpackOptions
            .WithAllowAssemblyVersionMismatch(formatterOptions.AllowAssemblyVersionMismatch)
            .WithCompression(formatterOptions.Compression)
            .WithOldSpec(formatterOptions.UseOldSpecification)
            .WithOmitAssemblyVersion(formatterOptions.OmitAssemblyVersion);

        options.InputFormatters.Add(
            new MessagePackInputFormatter(msgpackOptions, supportedMediaTypes)
        );
        options.OutputFormatters.Add(
            new MessagePackOutputFormatter(msgpackOptions, supportedMediaTypes)
        );

        foreach (var fileExtension in formatterOptions.FileExtensions)
        {
            options.FormatterMappings.SetMediaTypeMappingForFormat(
                fileExtension,
                supportedMediaTypes[0]
            );
        }
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

上述代码定义了一个名为 MessagePackFormatterMvcOptionsSetup 的类,它实现了 IConfigureOptions<MvcOptions> 接口。这个类的主要功能是配置 ASP.NET Core MVC 的选项,以便支持 MessagePack 格式的输入和输出。

以下是代码的主要功能和结构的详细解释:

1. 构造函数

public MessagePackFormatterMvcOptionsSetup(
    IOptions<MessagePackFormatterOptions> messagePackFormatterOptions
)
  • 构造函数接受一个 IOptions<MessagePackFormatterOptions> 类型的参数,这个参数用于获取 MessagePack 格式化的相关选项。
  • 如果传入的 messagePackFormatterOptionsnull,则抛出 ArgumentNullException

2. Configure 方法

public void Configure(MvcOptions options)
  • 该方法实现了 IConfigureOptions<MvcOptions> 接口中的 Configure 方法,用于配置 MvcOptions
  • 首先检查传入的 options 是否为 null,如果是,则抛出 ArgumentNullException

3. 获取格式化选项

var formatterOptions = _messagePackFormatterOptions.Value;
  • _messagePackFormatterOptions 中获取实际的格式化选项。

4. 媒体类型处理

var supportedMediaTypes = formatterOptions
    .MediaTypes.Where(t => !string.IsNullOrWhiteSpace(t))
    .ToArray();
  • 从格式化选项中获取支持的媒体类型,并过滤掉空白的媒体类型。
  • 如果没有指定任何支持的媒体类型,则抛出 InvalidOperationException

5. 配置 MessagePack 序列化选项

var msgpackOptions = MessagePackSerializerOptions.Standard;
msgpackOptions = msgpackOptions.WithResolver(
    formatterOptions.FormatterResolver ?? ContractlessStandardResolver.Instance
);
msgpackOptions = msgpackOptions
    .WithAllowAssemblyVersionMismatch(formatterOptions.AllowAssemblyVersionMismatch)
    .WithCompression(formatterOptions.Compression)
    .WithOldSpec(formatterOptions.UseOldSpecification)
    .WithOmitAssemblyVersion(formatterOptions.OmitAssemblyVersion);
  • 创建一个标准的 MessagePack 序列化选项,并根据格式化选项进行配置。
  • 这些配置包括解析器、是否允许程序集版本不匹配、压缩选项、是否使用旧规范以及是否省略程序集版本。

6. 添加输入和输出格式化器

options.InputFormatters.Add(
    new MessagePackInputFormatter(msgpackOptions, supportedMediaTypes)
);
options.OutputFormatters.Add(
    new MessagePackOutputFormatter(msgpackOptions, supportedMediaTypes)
);
  • 将自定义的 MessagePackInputFormatterMessagePackOutputFormatter 添加到 MVC 的输入和输出格式化器列表中,以支持 MessagePack 格式的请求和响应。

7. 文件扩展名映射

foreach (var fileExtension in formatterOptions.FileExtensions)
{
    options.FormatterMappings.SetMediaTypeMappingForFormat(
        fileExtension,
        supportedMediaTypes[0]
    );
}
  • 遍历格式化选项中的文件扩展名,并将这些扩展名映射到支持的媒体类型上,以便在处理文件上传时能够正确识别 MessagePack 格式。

总结

整体而言,MessagePackFormatterMvcOptionsSetup 类的作用是将 MessagePack 格式的支持集成到 ASP.NET Core MVC 中,通过配置输入和输出格式化器,使得应用能够处理 MessagePack 格式的请求和响应。这对于需要高效数据传输的应用场景非常有用。

loading