StackSpot.Notification.SNS 1.0.0

dotnet add package StackSpot.Notification.SNS --version 1.0.0
NuGet\Install-Package StackSpot.Notification.SNS -Version 1.0.0
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="StackSpot.Notification.SNS" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StackSpot.Notification.SNS --version 1.0.0
#r "nuget: StackSpot.Notification.SNS, 1.0.0"
#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.
// Install StackSpot.Notification.SNS as a Cake Addin
#addin nuget:?package=StackSpot.Notification.SNS&version=1.0.0

// Install StackSpot.Notification.SNS as a Cake Tool
#tool nuget:?package=StackSpot.Notification.SNS&version=1.0.0

StackSpot Notification SNS

Componente projetado para padronizar a comunicação com a Amazon Simple Notification Service (SNS) para aplicações .NET Core.

Versões suportadas

  • net5.0
  • net6.0

Uso

1. Adicione o pacote NuGet StackSpot.Notification.SNS ao seu projeto.
dotnet add package StackSpot.Notification.SNS
2. Configure as variáveis.
  • Region - Região da AWS em que o serviço SNS foi criado - Campo Obrigatório.

Você pode sobrescrever a configuração padrão adicionando a seção Sns em seu appsettings.json.

  "Sns": {
      "Region": "us-east-1"
  }

É possivel adicionar nessa seção o parâmetro topicArn para comunicação com o seu tópico. - Não Obrigatório.

  "Sns": {
      "Region": "us-east-1",
      "TopicArn": "arn:aws:sns:us-east-1:000000000000:mytopic",
  }
3. Adicione ao seu IServiceCollection via services.AddNotificationSns() no Startup da aplicação ou Program tendo como parametro de entrada IConfiguration e IWebHostEnvironment.
//using StackSpot.Notification.SNS;

services.AddNotificationSns(configuration);

Você tem disponível as opções para customizar o seu serviço utilizando os parâmetros e sobrecargas do método AddNotificationSns para ServiceLifetime e AWSOptions.

Implementação
  • A classe da mensagem que será enviada para o serviço de notificação, deverá herdar da classe SnsMessage.
  • Publish - Publica uma mensagem no serviço de notificação. Por padrão utiliza o Arn do tópico configurado no appsetings.json. Ele irá retornar o MessageId em caso de sucesso.
// using StackSpot.Notification.SNS.Common;
// using StackSpot.Notification.SNS;

public class Message : SnsMessage
{
    public Message(string text)
    {
        _text = text;
    }

    private readonly string _text;

    public string text
    {
        get { return _text; }
    }
}

[ApiController]
[Route("[controller]")]
public class SampleController : ControllerBase
{
    private readonly INotification<SnsMessage> _sns;

    public SampleController(INotification<SnsMessage> sns)
    {
        _sns = sns;
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody] string text)
    {        
        var message = new Message(text);

        var result = await _sns.Publish(test);

        return Ok(result.Content);
    }
}

Adicionalmente ao plubicar uma mensagem você pode utilizar a sobrecarga do método Publish e informar para qual Tópico será enviada a mensagem através do parâmetro topicArn.

  • A interface INotification também disponibiliza os métodos ListTopics e ListSubscriptionsByTopic que listam os Tópicos e Inscritos respectivamente.
4. Ambiente local
  • Esta etapa não é obrigatória.
  • Recomendamos, para o desenvolvimento local, a criação de um contâiner com a imagem do Localstack.
  • Para o funcionamento local você deve preencher a variável de ambiente LOCALSTACK_CUSTOM_SERVICE_URL com o valor da url do serviço. O valor padrão do localstack é http://localhost:4566.
  • Abaixo um exemplo de arquivo docker-compose com a criação do contâiner:
version: '2.1'

services:
  localstack:
    image: localstack/localstack
    ports:
      - "4566:4566"
    environment:
      - SERVICES=sns
      - AWS_DEFAULT_OUTPUT=json
      - DEFAULT_REGION=us-east-1

Após a criação do contâiner, crie um tópico para realizar os testes com o componente. Recomendamos que você tenha instalado em sua estação o AWS CLI. Abaixo um exemplo de comando para criação de uma fila:

aws  sns create-topic --endpoint-url=http://localhost:4566 --region=us-east-1 --name [NOME DO SEU TÓPCIO]

Componentes Utilizados

Referências

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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. 
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.0 200 5/3/2022