Creating an appointment reminder app with .NET SDK

Set up your .NET application

  1. Navigate to the directory in which you'd like to store your app project.
  2. Create a new folder called sinch_appointment_reminder.
  3. Open a terminal or command prompt to that location.
  4. Create a new .NET MVC app with the following command:
    Copy
    Copied
    dotnet new mvc

Installing the SDK

The easiest way to install the SDK is using the dotnet CLI:

  1. Open a command prompt or terminal to the local repository folder.
  2. Execute the following command:
    Copy
    Copied
    dotnet add package Sinch

Client information

When using the SDK, you must initialize the client.

Before initializing a client using this SDK, you'll need three pieces of information:

  • Your Project ID
  • An access key ID
  • An access key Secret
These values can be found on the Access Keys page of the Customer Dashboard. You can also create new access key IDs and Secrets, if required.
Note:
If you have trouble accessing the above link, ensure that you have gained access to the Conversation API by accepting the corresponding terms and conditions.

You'll add these values to the file described in the next section (along with others).

Setting the environment variables

Your app stores information such as your Sinch credentials as key-value pairs in the appsettings.Development.json file. This is much more flexible and secure than simply hardcoding them.
To set up your appsettings.Development.json file:
  1. Open the appsettings.Development.json file.
  2. Paste the following JSON body into your appsettings.Development.json file.
    Copy
    Copied
    "Sinch":{
        "key_id":"",
        "key_secret":"",
        "project_id":"",
        "country_code_eu": "",
        "country_code_us": "",
        "from_number": "",
        "sms_region":""
    },
  3. Populate the fields in the file with your own values. Refer to the table below for guidance:
    FieldDescription
    key_idYour access key ID, used for authentication. Refer to the Client information section for more information.
    key_secretYour access key secret, used for authentication. Refer to the Client information section for more information.
    project_idYour project ID. Refer to the Client information section for more information.
    country_code_euIf you live in the EU or UK, this is the country code of your home country. For example the UK country code is +44.
    country_code_usIf you live in the US, this is the country code of your home country. For the US, it's +1.
    from_numberAny number you've assigned to your Sinch account. Find the number on your Customer Dashboard by clicking the service plan ID link and scrolling to the bottom of the page.
    sms_regionThe region in which you would like to send SMS messages. Either us or eu.

Modifying the Program.cs file

In .NET, Program.cs has the job of setting your application up and running it. The dependencies your application needs are created as services. These services will be fed into a Controller using Dependency Injection. The SDK client is initialized and added to Services as a singleton, meaning there can only be one instance of it.
Open Program.cs and replace the contents with the following code:
Copy
Copied
using Sinch;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();


builder.Services
      .AddMvc();
builder.Services.AddSingleton<ISinchClient>(_ => new SinchClient(
    builder.Configuration["Sinch:project_id"],
     builder.Configuration["Sinch:key_id"],
    builder.Configuration["Sinch:key_secret"],

                         // ensure you set the SMS region. 
                         options =>
                         {
                             options.SmsRegion = Sinch.SMS.SmsRegion.Us;
                             options.LoggerFactory = LoggerFactory.Create(config => { config.AddConsole(); });
                             options.HttpClient = new HttpClient();
                         }));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.