2.9 KiB
2.9 KiB
title, date, author, top_img
| title | date | author | top_img |
|---|---|---|---|
| ASP.Net 6 | 2021-03-23 10:30:31 | 文永达 | https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/67239FBB-E15D-4F4F-8EE8-0F1C9F3C4E7C.jpeg |
ASP.Net 6
1. 部署到Docker
1.1 安装.Net SDK 6.0环境
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
sudo yum install dotnet-sdk-6.0
dotnet --info
1.2 Visual Studio添加Docker支持
1.3 Linux下构建Docker镜像
docker image build -f ./XiaodaERP/Dockerfile -t aspnetcore .
docker images
1.4 运行Docker镜像
docker run --name=aspnetcore -p 9001:80 -d aspnetcore
docker ps
cd /usr/local/jenkins_home/workspace/XiaodaERP_NetCore
echo $PWD
docker image build -f ./XiaodaERP/Dockerfile -t xiaodaerp/netcore .
docker images
docker run --name xiaodaerp/netcore -p 7274:80 -d xiaodaerp/netcore
2. 顶级语句配置Program.cs
2.1 取消默认JSON首字母小写命名
builder.Services.AddControllers().AddJsonOptions(options => {
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
2.2 Json序列化时忽略属性为null的值
builder.Services.AddControllers().AddJsonOptions(options => {
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
2.3 使用Autofac自动注入Service
通过NuGet包管理器 安装NuGet包
Autofac
Autofac.Extensions.DependencyInjection
Autofac.Extras.DynamicProxy
新建ServiceAutofac.cs类
using System.Reflection;
namespace XiaodaERP
{
public class ServiceAutofac
{
/// <summary>
/// 获取程序集名称
/// </summary>
/// <returns></returns>
public static string GetAssemblyName()
{
return Assembly.GetExecutingAssembly().GetName().Name;
}
}
}
Program.cs配置
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
{
Assembly assembly = Assembly.Load(ServiceAutofac.GetAssemblyName());//注入Service程序集 可以是其他程序集
builder.RegisterAssemblyTypes(assembly)
.AsImplementedInterfaces()
.InstancePerDependency();
});
2.4 注入Entity Framework Core 6 DbContext上下文
builder.Services.AddDbContext<OracleDbContext>(options =>
options.UseOracle(builder.Configuration.GetConnectionString("OracleDbContext")));
builder.Services.AddDbContext<SqlServerDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("SqlServerDbContext")));


