Files
“wanyongkang” ed3b2c653e 接口文件
2024-04-10 13:55:27 +08:00

113 lines
5.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using Hncore.Infrastructure.Autofac;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
namespace Hncore.Infrastructure.WebApi
{
/// <summary>
/// 服务集合对象(可以理解为内置的依赖注入容器)功能扩展类
/// </summary>
///
public static class ServiceCollectionExtend
{
/// <summary>
/// 通用初始化方法(被各个微服务项目所引用)
/// </summary>
/// <param name="services">服务集合对象</param>
/// <param name="configuration">配置信息对象</param>
/// <param name="version">.net core兼容版本</param>
/// <param name="serviceOption">服务自定义选项对象</param>
/// <returns>服务提供者对象</returns>
///
public static IServiceProvider Init(this IServiceCollection services, IConfiguration configuration,
CompatibilityVersion version, ServiceOption serviceOption = null)
{
//启用选项配置服务
services.AddOptions();
//将配置信息对象以单例模式添加到服务集合中
services.AddSingleton(configuration);
// services.AddCors();
var mvcbuilder = services
.AddMvc(options =>
{
options.EnableEndpointRouting = false; //关闭终端点路由
if (serviceOption != null && serviceOption.UseGlobalManageAuthFilter)
{
//如果配置并传递了自定义选项中的全局授权过滤器就将全局授权过滤器添加到MVC全局过滤器链中让其生效
options.Filters.Add(new ManageAuthAttribute());
GlobalData.UseGlobalManageAuthFilter = true;
}
else
{
GlobalData.UseGlobalManageAuthFilter = false;
}
})
.SetCompatibilityVersion(version) //设置.net core兼容版本号
.AddJsonOptions(options =>
{
//使用NewtonsoftJson插件替换掉系统默认提供的JSON插件
options.SerializerSettings.ContractResolver =
new Newtonsoft.Json.Serialization.DefaultContractResolver();
//定义日期格式化策略
options.SerializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
//定义循环引用处理策略(就是要序列化的对象类A中引用了BB引用了C……转了一圈儿之后又直接或间接引用回了A就称为循环引用)
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//序列化或反序列化时,可接受的日期时间字符串格式
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
if (serviceOption != null && serviceOption.IgnoreJsonNullValue)
{
//如果配置并传递了自定义选项中的忽略空值选项就在操作JSON时忽略掉空值数据
//忽略掉空值的意思就是如果对象中的某个属性值为null它将不会出现在最终序列化后的JSON字符串中
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
}
});
services.AddApiVersioning(option =>
{
//设置API版本信息
option.ReportApiVersions = true; //在向客户端响应的响应头中显示受支持的API版本信息
option.AssumeDefaultVersionWhenUnspecified = true; //如果客户端未提供并指定要调用的API版本就以下方的默认版本为准
option.DefaultApiVersion = new ApiVersion(1, 0); //默认版本号
});
//启用HTTP请求上下文访问器(用来访问类似传统MVC中的那个HttpContext对象)
services.AddHttpContextAccessor();
//构建并返回服务提供对象(在Build方法中主要用Autofac插件替换掉了默认的依赖注入插件并做一些自动扫描配置)
return new MvcAutoRegister().Build(services, mvcbuilder);
}
}
/// <summary>
/// 服务自定义配置类(承载一些自定义配置)
/// </summary>
///
public class ServiceOption
{
/// <summary>
/// 是否启用全局授权过滤器(默认关闭)
/// </summary>
public bool UseGlobalManageAuthFilter { get; set; } = false;
/// <summary>
/// 是否在生成的JSON字符串中忽略掉为null的属性或字段(默认为false意思就是就算字段为null也会出现在最终序列化后的JSON字符串中)
/// </summary>
public bool IgnoreJsonNullValue { get; set; } = false;
}
}