Fluent Notification Sender .Net Package

 



Most modern software projects have need to an essential feature of sending notifications to clients (e.g., user registration & confirmation, automated request process follow up, topic & news subscription and alerting system…). 

  • Notifications can be in many flavors (Email, SMS, Realtime, and mobile push notification) beside send by multiple methods according to system or client preferences (e.g., sending both Email and SMS notification for specific events occurs in system).
  • Each software project has a different requirement so, it can use a different provider vendor (e.g., send email by SMTP protocol or by SendGrid API, …etc.) to fulfil it.
  • Sending notifications is expensive I/O operation and can affect application performance if it not coded will.

So, Notification project as a component designed to deal with the above concerns.


Design Specifications:

  1. Support multiple notifications methods
  2. Support multiple vendors for each method
  3. Support sending by multiple methods in one send
  4. Support Batch Send
  5. Provide application-wide configuration
  6. Provide flexibility to override the global configuration at runtime and apply within a specific scope (e.g., to maintain a clear consistent state cross application parts)
  7. Adopt "Fluent interface" pattern to increase code legibility for the consumer
  8. Return Success/Fail detailed result object rather than throwing exception (for predictability and performance considerations)
  9. Support "fire and forget" approach for sending notification
  10. Auto retry failed notifications
  11. Balance OR auto try another vendor if the current one has exceeded its quota

Code:

Simple send in service layer


Batch send:-


Fire and Forget send:-



Try with custom vendor and custom retry count:-


Results"





Configuration in "Startup.cs"

   services.AddFluentNotificationSender(o =>
            {
                o.UseSettingFile(Configuration);
                // apply your custom configuration
            });

Configuration in "appsettings.json", I set wrong emails, so you can test auto retry another vendor feature

  "FluentNotification": {
    "RetryCount": 3,
    "Email": {
      "AutoRetryAnotherVendor": {
        "Enable": true,
        "ExceptionType": "",
        "ExceptionMessageContains": "",
        "CandidateVendorGlobalIndex": null,
        "SetCandidateVendorAsDefault": true
      },
      "RetryCount": 9,
      "Smtp": [
        {
          "GlobalIndex": 0,
          "Server": "smtp.gmail.com",
          "Port": 587,
          "UserName": "eng.mustafak26@gmail.com",
          "Password": "set you password here",
          "UseSSL": true,
          "UseAsDefault": true
        },
        {
          "GlobalIndex": 6,
          "Server": "smtp.gmail.com",
          "Port": 587,
          "UserName": "eng.mustafak026@gmail.com",     // not working email 
          "Password": "ralxqmcqvgxyfmgo",
          "UseSSL": true,
          "UseAsDefault": false
        },
        {
          "GlobalIndex": 9,
          "Server": "smtp.gmail.com",
          "Port": 587,
          "UserName": "mustafak26@gmail.com",     //working email
          "Password": "bwgicmhehkzuffri",
          "UseSSL": true,
          "UseAsDefault": false
        }
      ],
      "SendGrid": [
        {
          "GlobalIndex": 2,
          "FromEmail": "eng.mustafak26@gmail.com",
          "FromName": "Mustafa Kamal Ali",
          "ApiKey": "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",
          "UseAsDefault": true
        }
      ]
    },
    "SMS": {
      "Unifonic": [
        {
          "GlobalIndex": 3,
          "APIUrl": "http://basic.unifonic.com/rest/SMS/messages",
          "AppSid": "",
          "SenderID": "Notification Sender"
        }
      ],
      "Twilio": [
        {
          "GlobalIndex": 11,
          "AccountSid": "AC29f9d88363c84cc81fa7f97dd2fce8ed",
          "AuthToken": "43d71c3595e30e893ee369833e90fad5",
          "FromNumber": "+15856394393",
          "UseAsDefault": true
        }
      ]
    },
    "Mobile": {
      "Firebase": [
        {
          "GlobalIndex": 4,
          "UseAsDefault": true,
          "type": "service_account",
          "project_id": "set your project",
          "private_key_id": "",
          "private_key": "-----BEGIN PRIVATE KEY-----
---                               --END PRIVATE KEY-----",
          "client_email": "set your client email here   .iam.gserviceaccount.com",
          "client_id": "",
          "auth_uri": "https://accounts.google.com/o/oauth2/auth",
          "token_uri": "https://oauth2.googleapis.com/token",
          "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
          "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-nde0k%40ncm-project-341915.iam.gserviceaccount.com"
        }
      ]
    }}


Code for ".Net framework:

  1. use this configure method in Global.asax.cs


2. use factory to generate IFluentNotificationService




    Comments

    Popular posts from this blog

    Async/Await - OS Concept

    Thundering Herd Problem - ASP Core Solution Architect