DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Send Text Messages Using Your Windows Azure Service

How to Send Text Messages Using Your Windows Azure Service

Michael Collier user avatar by
Michael Collier
·
May. 10, 12 · Interview
Like (0)
Save
Tweet
Share
21.15K Views

Join the DZone community and get the full member experience.

Join For Free

recently i was waiting at o’hare for a flight back to columbus.  i fired up evernote to catch up on some windows azure articles i wanted to read (i save a lot of things to evernote).  one of the first articles i read was a codeproject article by luke jefferson at red gate .  luke’s article provides a really nice basis for using cerebrata’s powershell cmdlets and twilio for sending sms messages when your windows azure service has “issues”.

this got me thinking – could i write something similar (i was inspired) before i returned home to columbus?  i’d like to tweak it a little to run as a windows azure worker role.  i would have to do most of the writing without the crutch of the internet (remember – no wifi at o’hare and no wifi on my plane).  i would have a brief internet connection before getting on the plane if i used my awesome nokia lumia 900 for tethering.  this is doable!

the timing couldn’t have been better!  i remembered seeing a twitter message earlier in the day from @windowsazure announcing a new promotion from twilio to get some free messages for windows azure customers .  perfect time to try this out!


by the way, if you don’t have windows azure yet, now is a good time to get it.

  • get a 3 month free trial
  • download the windows azure sdk
  • have msdn? you already have windows azure benefits!! activate now!!

while luke’s article on codeproject uses powershell cmdlets, i wanted to try it with regular c# code and run the solution in a simple windows azure worker role.  to do so i would need to work with the windows azure service management api.  as luke points out in his article, the windows azure service management api is a  rest api.  he does a great job of explaining the basics, so be sure to check it out and head over to msdn for all the details.

unfortunately there is not yet a nice .net wrapper for this api.  i took at look at azure fluent management library , but it didn’t yet have all the features needed for this little pet project (but looks to be cool – something to keep an eye on).  thankfully, i remembered i had read neil mackenzie’s excellent microsoft windows azure development cookbook and it contained a recipe for getting the properties of a hosted service.  bingo!  this recipe is a very helpful one (like many in neil’s book) and i had the code standing by in a sample project i put together while reading neil’s book.  with the starting point for using the windows azure service management api in place, the only thing i needed now was an api for working with twilio

time to fire up the phone tethering feature, sign up for twilio , and download their api.  i decided to use the twilio-csharp client library and installed that via nuget.  easy enough.  with everything that i needed downloaded, it was time to power down and get on the plane.

the basics of what i wanted to do are pretty simple:

  1. get the properties of a specific windows azure hosted service
  2. check if the service is running or not
  3. if the service is not running, send a sms message to me to let me know that something is not right.
  4. sleep for a little bit and then repeat the process.

get hosted service properties

private hostedserviceproperties gethostedserviceproperties(string subscriptionid, string servicename, string thumbprint)
{
string uri = string.format(serviceoperationformat, subscriptionid, servicename);

servicemanagementoperation operation = new servicemanagementoperation(thumbprint);
xdocument hostedserviceproperties = operation.invoke(uri);

var deploymentinformation = (from t in hostedserviceproperties.elements()
select new
{
deploymentstatus = (from deployments in t.descendants(windowsazurenamespace + "deployments")
select deployments.element(windowsazurenamespace + "deployment").element(windowsazurenamespace + "status").value).first(), rolecount = (from roles in t.descendants(windowsazurenamespace + "rolelist")
select roles.elements()).count(), instancecount = (from instances in t.descendants(windowsazurenamespace + "roleinstancelist")
select instances.elements()).count()
}).first();

var properties = new hostedserviceproperties
{
status = deploymentinformation.deploymentstatus,
rolecount = deploymentinformation.rolecount,
instancecount = deploymentinformation.instancecount
};

return properties;
}

send a message if not running

     // get the hosted service
     var serviceproperties = gethostedserviceproperties(subscriptionid, hostedservicename, managementcertificatethumbprint);

     // if the service is not running.
     if (serviceproperties.status != "running")
     {
          string message = string.format("service '{0}' is not running.  current status is '{1}'.",
                                                   hostedservicename, serviceproperties.status);

          // send the sms message
          twilio.sendsmsmessage(fromphonenumber, tophonenumber, message);
     }
}

putting it all together

 private readonly xnamespace windowsazurenamespace = "http://schemas.microsoft.com/windowsazure";
 private const string serviceoperationformat = "https://management.core.windows.net/{0}/services/hostedservices/{1}?embed-detail=true";

public override void run()
 {
 trace.writeline("staring windows azure notifier role", "information");

// get the configuration settings needed to work with the hosted service.
 var hostedservicename = getconfigurationvalue("hostedservicename");
 var subscriptionid = getconfigurationvalue("subscriptionid");
 var managementcertificatethumbprint = getconfigurationvalue("managementcertificatethumbprint");

// get the configuration settings for twilio.
 var twilioid = getconfigurationvalue("twilioid");
 var twiliotoken = getconfigurationvalue("twiliotoken");
 var fromphonenumber = getconfigurationvalue("fromphonenumber");
 var tophonenumber = getconfigurationvalue("tophonenumber");

// create an instance of the twilio client.
 var twilio = new twiliorestclient(twilioid, twiliotoken);

while (true)
 {
 // get the hosted service
 var serviceproperties = gethostedserviceproperties(subscriptionid, hostedservicename, managementcertificatethumbprint);

 // if the service is not running.
 if (serviceproperties.status != "running")
 {
  string message = string.format("service '{0}' is not running. current status is '{1}'.",
  hostedservicename, serviceproperties.status);

 // send the sms message
 twilio.sendsmsmessage(fromphonenumber, tophonenumber, message);
 }

 thread.sleep(timespan.fromminutes(5));
 trace.writeline("working", "information");
 }
 

i had most of this put together before the stewardess informed me it was time to power down in preparation for landing. the only things i needed yet was a windows azure subscription id, a windows azure management certificate thumbprint, and a hosted service to test this against. i have several windows azure hosted services running for various reasons, so all this was easy enough to get.

with all the necessary bits in place, it’s time to test this out.  in order to get this working in windows azure (as opposed to my local emulator) i would need to include my management certificate as part of the role i’m deploying.  when using the emulator, the certificate is already on my development machine so i didn’t need to do anything special.

add the certificate to the role

i would also upload this certificate as a service certificate.

windows azure hosted service certificates

with the certificates in place, i was ready to deploy the service as an extra small worker role.  i then picked one of my demo apps and told windows azure to shut it down.  shortly after that, i received a new text message from my twilio account!

while this was just a simple proof-of-concept, it was pretty cool and can be pretty powerful.

if you’d like the whole source code for this project, you can download it here .

azure

Published at DZone with permission of Michael Collier, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Main Pillars in ReactJS
  • HTTP vs Messaging for Microservices Communications
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid
  • Using GPT-3 in Our Applications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: