网站首页 网站源码
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]
);
}
}
}
上述代码定义了一个名为 MessagePackFormatterMvcOptionsSetup
的类,它实现了 IConfigureOptions<MvcOptions>
接口。这个类的主要功能是配置 ASP.NET Core MVC 的选项,以便支持 MessagePack 格式的输入和输出。
以下是代码的主要功能和结构的详细解释:
public MessagePackFormatterMvcOptionsSetup(
IOptions<MessagePackFormatterOptions> messagePackFormatterOptions
)
IOptions<MessagePackFormatterOptions>
类型的参数,这个参数用于获取 MessagePack 格式化的相关选项。messagePackFormatterOptions
为 null
,则抛出 ArgumentNullException
。public void Configure(MvcOptions options)
IConfigureOptions<MvcOptions>
接口中的 Configure
方法,用于配置 MvcOptions
。options
是否为 null
,如果是,则抛出 ArgumentNullException
。var formatterOptions = _messagePackFormatterOptions.Value;
_messagePackFormatterOptions
中获取实际的格式化选项。var supportedMediaTypes = formatterOptions
.MediaTypes.Where(t => !string.IsNullOrWhiteSpace(t))
.ToArray();
InvalidOperationException
。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)
);
MessagePackInputFormatter
和 MessagePackOutputFormatter
添加到 MVC 的输入和输出格式化器列表中,以支持 MessagePack 格式的请求和响应。foreach (var fileExtension in formatterOptions.FileExtensions)
{
options.FormatterMappings.SetMediaTypeMappingForFormat(
fileExtension,
supportedMediaTypes[0]
);
}
整体而言,MessagePackFormatterMvcOptionsSetup
类的作用是将 MessagePack 格式的支持集成到 ASP.NET Core MVC 中,通过配置输入和输出格式化器,使得应用能够处理 MessagePack 格式的请求和响应。这对于需要高效数据传输的应用场景非常有用。