FreeSql+ Rabbit+CAP

发布时间 2023-11-09 09:43:42作者: 小雨转晴被占用了

1.下载FreeSql.Provider.SqlServer,如果是MaSql就把最后改成MySql 参考链接 入门 | FreeSql 官方文档

 

Func<IServiceProvider, IFreeSql> fsqlFactory = r =>
{
    IFreeSql fsql = new FreeSql.FreeSqlBuilder()
        .UseConnectionString(FreeSql.DataType.SqlServer, builder.Configuration.GetConnectionString("Connstr"))
        .UseLazyLoading(true)
        //.UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}"))//监听SQL语句
        .UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
        .Build();
    return fsql;
};
builder.Services.AddSingleton(fsqlFactory);

var app = builder.Build();
//在项目启动时,从容器中获取IFreeSql实例,并执行一些操作:同步表,种子数据, FluentAPI等
//using (IServiceScope serviceScope = app.Services.CreateScope())
//{
//    var fsql = serviceScope.ServiceProvider.GetRequiredService<IFreeSql>();
//    fsql.CodeFirst.SyncStructure(typeof(UserInfo));//Topic 为要同步的实体类
//}

运行项目的时候就自动映射到数据库了

二、1.配置RabbitMQ

安装包 DotNetCore.CAP

 

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "Connstr": "server=.;uid=sa;pwd=111111;database=CAP1108;TrustServerCertificate=True",
    "OrderCap": "server=.;uid=sa;pwd=1111111;database=OrderCap;TrustServerCertificate=True"//CAP的连接字符串
  },
//连接对应的RabbitMQ
"RabbitMQ": { "Host": "10.10.10.10", "VirtualHost": "/", "Port": 5672, "UserName": "admin", "Password": "111111" } }

2.配置Program 参考连接 快速开始 - CAP (dotnetcore.xyz)

builder.Services.AddCap(x =>
{
    x.UseSqlServer(builder.Configuration["ConnectionStrings:OrderCap"]); // SQL Server
    x.UseRabbitMQ(cfg =>
    {
        cfg.HostName = builder.Configuration["RabbitMQ:Host"];
        cfg.VirtualHost = builder.Configuration["RabbitMQ:VirtualHost"];
        cfg.Port = Convert.ToInt32(builder.Configuration["RabbitMQ:Port"]);
        cfg.UserName = builder.Configuration["RabbitMQ:UserName"];
        cfg.Password = builder.Configuration["RabbitMQ:Password"];
    }); // RabbitMQ

    // Below settings is just for demo
    x.FailedRetryCount = 2;//重试
    x.FailedRetryInterval = 5;
});

 

 end…