Using the Latest Twilio NuGet Package with Legacy Versions of the .NET Framework

May 18, 2020
Written by
AJ Saulsberry
Contributor
Opinions expressed by Twilio contributors are their own

twilio-nuget-legacy-dot-net.png

Developers have a natural inclination to work with the latest and greatest technologies; new libraries, language features, and tools are some of the things that keep the profession interesting. Production environments, on the other hand, often lag behind because of stability, compatibility, and cost considerations.

Twilio understands both perspectives, so .NET developers who build software for Windows machines get the best of both worlds when they work with Twilio components. You can develop with the latest tools and use the most recent Twilio libraries and API capabilities while maintaining compatibility with legacy operating systems and .NET frameworks.

This post will walk you through a quick case study project demonstrating how to code an application that can run on older Windows systems and legacy versions of the .NET Framework while using the latest version of Visual Studio 2019 and the newest Twilio NuGet package. You can use the code to test platform compatibility and deployment with the software you run in your production environment.

Prerequisites

You’ll need the following resources to complete the tutorial in this post:

Visual Studio 2019 – The Community edition is free.

.NET Framework 4.5.2 – Download and install the Developer Pack.

Twilio account – Sign up for a free trial account with this link and get an additional $10 credit.

There is a companion repository containing the complete source code for this project available on GitHub.

Obtaining your Twilio credentials

If you don’t already have a Twilio account, use the link above to create one. During the registration process be sure to register a phone number that can receive SMS messages.

Go to the Twilio console dashboard and get the Account SID and Auth Token for the ExplorationOne project (or the project of your choice, if you’ve created additional projects). You can find these credentials on the upper right-hand side of the dashboard. These are user secrets, so copy them someplace safe and handle them securely.

Obtaining an SMS-enabled Twilio phone number

Go to the Phone Numbers page on your Twilio console and add a number with SMS capabilities. You won’t be charged for the number you register in your trial account. Copy the phone number someplace handy.

For more information on getting started with your Twilio trial account, see How to Work with your Free Twilio Trial Account.

Creating the Visual Studio project

It may be a challenge remembering which features are included in all the various versions of the .NET Framework and .NET Core, but working with old and new framework versions is easy with Visual Studio 2019. Start by creating a new project and narrow down the list of templates by using the filters to select C#, Windows, and Console for the language, operating system, and project type, respectively.

Choose the Console App (.NET Framework) template.

Visual Studio 2019 Create a new project dialog box

In the Configure your new project window, name the project FrameworkReporter and select an appropriate location for the files.

In the Framework box, select .NET Framework 4.5.2.

Visual Studio 2019 Configure your project dialog box

Create the project. After the tooling is finished chugging you should see the trusty Main method of the Program class.

Note that the tooling also creates an App.config file.

The FrameworkReporter project will use the System.Configuration.ConfigurationManager class to load your Twilio credentials. In .NET Framework 4.5.2 it’s included in the System.Configuration.dll assembly, so you’ll need to add a reference.

In the Solution Explorer panel, expand the References node under the FrameworkReporter.

Right-click on References and click Add reference.

In the Reference Manager, scroll through the long list of available assemblies and add a checkmark next to System.Configuration. Click OK.

Visual Studio 2019 Reference Manager dialog box

You should see System.configuration under the Reference node. Note that the lowercase “c” in “configuration” seems to be normal; it shouldn’t affect the operation of the program.

This would be a good time to check the solution into source code control.

Adding Twilio credentials to the project

In .NET Framework console projects of this vintage it’s conventional to put configuration information in the App.config file, so that’s where your Twilio credentials will go. Since these values are user secrets connected to your account, you’ll want to exclude this file from your source code control repository if you’re planning on making it public or widely accessible.

For development purposes, open the App.config file and add the following <appSettings> node to the <configuration> node:

  <appSettings>
    <add key="TWILIO_ACCOUNT_SID" value="AC45000000000000000000000000000006" />
    <add key="TWILIO_AUTH_TOKEN" value="8700000000000000000000000000000d" />
    <add key="TWILIO_SMS_PHONE_NUMBER" value="+12125551212" />
  </appSettings>

Replace the placeholder values with your actual Twilio Account SID, Auth Token, and SMS-enabled phone number. The phone number should be entered in E.164 format.

Adding the Twilio NuGet package

This is a simple demo, so you’ll only need one essential NuGet package. Open the Package Manager Console window and execute the following Package Manager CLI instruction:

Install-Package Twilio

This will install the latest version of the Twilio helper library for .NET. As of this writing it’s 5.41.0. A number of dependencies will also be installed and will appear under the References node.

Great! Now it’s time to code all 16 lines of the program (not including comments).

Sending SMS messages from a .NET Framework 4.5.2 console app

Open the Program.cs file and replace the existing using directives with the following:

using System.Configuration;
using System.Diagnostics;
using System.Net;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

Replace the contents of the Main method with the following:

// TLS 1.2 is not enabled by default in .NET Framework 4.5.2 and is required by the Twilio API.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

var appSettings = ConfigurationManager.AppSettings;

TwilioClient.Init(appSettings["TWILIO_ACCOUNT_SID"], appSettings["TWILIO_AUTH_TOKEN"]);

//Use your registered phone number to test with a Twilio trial account.
var recipient = new PhoneNumber("+8005551212"); 

var message = MessageResource.Create(
    to: recipient,
    from: new PhoneNumber(appSettings["TWILIO_SMS_PHONE_NUMBER"]),
    body: "This runs on .NET Framework 4.5.2");
Debug.WriteLine(message.ToString());

As of June, 2019, Twilio’s REST APIs require Transport Layer Security (TLS) version 1.2. This is pretty standard for apps built with the current versions of .NET Core and the .NET Framework, but it’s not turned on by default in .NET Framework 4.5.2. The first statement in the Main method forces TLS 1.2 to be used.

Note for Windows 7, Windows Server 2008, and Windows Server 2012 users: TLS 1.2 might not be enabled if you haven’t been keeping up with service packs and Windows Update. See this Microsoft Support article for more information: Update to enable TLS 1.1 and TLS 1.2 as default secure protocols in WinHTTP in Windows. As always, make a backup, create a restore point, and devise a rollback plan before making manual modifications to the Windows Registry.

Testing the application

Press F5 and pick up your phone. If everything is working correctly you should receive an SMS message from the application momentarily.

If you don’t get a message and your application isn’t throwing errors, you can check the message log for the number to see if Twilio is receiving the message. Go to the Phone Number section of the Twilio console, click on the phone number you’re using in the app, and select the Messages Log tab.

If you’re having problems with TLS, verify the machine has TLS 1.2 enabled by going to Internet Options in Windows Settings or the Control Panel. Click on the Advanced tab and scroll down. Use TLS 1.2 should be listed and checked.

Potential enhancements

Although this is a simple demo app, there are a number of things you could do to make it more useful. Here are some ideas:

  • Add functionality to determine which .NET Framework versions are installed on a machine.
  • Have the messages the app sends identify which .NET Framework versions are installed.
  • Provision the app with a JSON file identifying a number of phone numbers to receive messages.
  • Turn the app into a Windows service so it can run continuously on a server, checking for installed (or uninstalled) .NET Framework versions periodically.

Additional resources

Programmable SMS – Twilio documentation, including quickstarts for multiple languages, tutorials, and the API reference.

Default SecurityProtocol in .NET 4.5 – One of the more exhaustive Stack Overflow topics on TLS support in .NET Framework 4.5.

Lifecycle FAQ - .NET Framework – This is the place to start for understanding how long you have before your operating system or .NET Framework edition is no longer supported.

TwilioQuest – Level up your programming skills by completing missions in Twilio’s adventure game. It’s fun for the whole quarantined family!

Encrypting the Web – The EFF explains why encryption is so important to security and what you can do to help

AJ Saulsberry is a Technical Editor at Twilio. Reach out to him if you’ve been “there and back again” with a .NET development topic and want to get paid to write about it for the Twilio blog.