Robo.Notification.Provider 1.0.6

dotnet add package Robo.Notification.Provider --version 1.0.6
                    
NuGet\Install-Package Robo.Notification.Provider -Version 1.0.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Robo.Notification.Provider" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Robo.Notification.Provider" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="Robo.Notification.Provider" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Robo.Notification.Provider --version 1.0.6
                    
#r "nuget: Robo.Notification.Provider, 1.0.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Robo.Notification.Provider@1.0.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Robo.Notification.Provider&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=Robo.Notification.Provider&version=1.0.6
                    
Install as a Cake Tool

Robo.Notification.Provider

安装依赖

首先安装nuget包,包名:Robo.Notification.Provider

appsettings.json配置

在appsettings.json中加入配置信息。

"Dbs": {
    "ConnectionConfigs": [ //多库数组
      { // 使用的是postgres数据库
        "ConnId": "MAIN",
        "ConnectionString": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=postgres;",
        "DbType": 4, //MySql = 0, SqlServer = 1, Sqlite = 2, Oracle = 3, PostgreSQL = 4
        "IsAutoCloseConnection": true,
        "IsEventBusDataBase": true // 配置为事件总线数据库 
      }
    ]
},
"RabbitMQ": { // RabbitMQ服务器配置
    "HostName": "RabbitMQ服务主机名或IP地址",
    "Port": 5672,
    "UserName": "admin",
    "Password": "admin",
    "VirtualHost": "/"
}
新建EventBusSetup类
public static class EventBusSetup
{
    /// <summary>
    /// 配置事件总线
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    /// <exception cref="ArgumentNullException"></exception>
    public static void AddEventBusSetup(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddEventBus(cap =>
        {
            // 获取RabbitMq服务器配置
            var mq = configuration.GetSection("RabbitMq").Get<RabbitMQOptions>() ?? throw new ArgumentNullException(nameof(RabbitMQOptions));
            cap.UseRabbitMQ(cfg =>
            {
                cfg.HostName = mq.HostName;
                cfg.Port = mq.Port;
                cfg.UserName = mq.UserName;
                cfg.Password = mq.Password;
                cfg.VirtualHost = mq.VirtualHost;
            });
            // 获取数据库连接字符串
            var sqlConnectionString = configuration.GetSection("Dbs:ConnectionConfigs:0:ConnectionString").Value ?? throw new ArgumentNullException("PostgreSQLConnectionString");
            cap.UsePostgreSql(opt =>
            {
                opt.ConnectionString = sqlConnectionString;
                opt.Schema = "public";
            });
            
            cap.UseDashboard();
            cap.DefaultGroup = "Robo.EmailRequestQueue"; // 指定队列名称,可自定义
                                                         // Robo.EmailRequestFailureQueue
                                                         // Robo.EmailRequestUnavailableQueue
            cap.FailedRetryInterval = 10; // 失败重试的时间间隔
            cap.FailedRetryCount = 10; // 失败重试的次数

        });
    }
}
依赖注入

添加到ConfigureServices依赖注入容器,如下:

public void ConfigureServices(IServiceCollection services,IConfiguration Configuration)
{
    services.AddEventBusSetup(Configuration);
    services.AddNoticeProvider(); // 注入 IMailProducer
    							  // 注入 IMailRequestEvent
        						  // 注入 IMailBatchRequestEvent
}
使用

在 Controller 中注入 IMailProducer 然后使用 IMailProducer 进行发布消息。

[Route("api/[controller]/[action]")]
[ApiController]
public class MailPublishController : ControllerBase
{
    private readonly IMailProducer _pub;
    public MailPublishController(IMailProducer pub)
    {
        _pub = pub;
    }
    /// <summary>
    /// 发布邮件通知 (单个收件人)
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IActionResult> PublishMail([FromForm] MailRequest req)
    {
        await _pub.PublishMailRequestMsg(new MailRequestMsg
        {
            Body = req.Body, //发送邮件内容
            Subject = req.Subject, //发送邮件主题
            ToEmail = req.ToEmail, //收件人邮件地址
        });
        return Ok();
    }
    /// <summary>
    /// 发布邮件通知 (多个收件人)
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IActionResult> PublishMailBatch([FromForm] MailBatchRequest req)
    {
        await _pub.PublishMailRequestMsg(new MailRequestBatchMsg
        {
            ToEmail = req.ToEmail, //收件人邮件地址
            CcEmail = req.CcEmail, //抄送人邮件地址
            Body = req.Body, //发送邮件内容
            Subject = req.Subject, //发送邮件主题
        });
        return Ok();
    }
}
单人邮件发布需构造MailRequestMsg类
public class MailRequestMsg
{
    [Display(Name = "邮件地址")] //必填项
    public string ToEmail { get; set; }

    [Display(Name = "邮件主题")] //必填项
    public string Subject { get; set; }

    public string Body { get; set; } //必须项
}
多人邮件发布需构造MailRequestBatchMsg类
public class MailRequestBatchMsg
{
    [Display(Name = "收件人邮件地址")]
    public List<string> ToEmail { get; set; } //必须项

    [Display(Name = "抄送人邮件地址")]
    public List<string> CcEmail { get; set; } //非必须项

    [Display(Name = "邮件主题")]
    public string Subject { get; set; } //必须项

    public string Body { get; set; } //必须项
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.6 253 8/7/2024
1.0.5 214 8/7/2024
1.0.4 185 8/2/2024
1.0.3 210 7/11/2024
1.0.2 206 7/11/2024
1.0.1 349 7/11/2023