Connecting to the TextPort SMS Gateway Using .NET WCF Web Services


The following guide outlines how to set up a WCF service reference in Microsoft Visual Studio 2010 to enable sending of text messages to the TextPort SMS gateway.


Step 1 – In your Visual Studio project Solution Explorer, locate the Service References item. Right click to display the menu, then click the Add Service Reference option.

step1

Step 2 – In the Add Service Reference dialog, enter the URL of the TextPort SMS WCF service. The URL is http://www.textport.com/WebServices/TextPortSMS.svc. Click the Go button. The service reference will be fetched from the TextPort website and you'll see the service reference item appear in the Services area of the dialog box. Clicking the TextPortSMSCLinet text will expand the service and display the web methods available (Ping(), SendMessages() and VerifyAuthentication()). Give your service reference an appropriate namespace by entering a name in the field at the bottom of the dialog. We have used TextPortSMSService in this example. Finally, click the OK button.

step2

Your solution explorer should now display the web reference as shown below.

step3

Step 3a - In this optional step we change the collection type of the service objects to a generic list rather than an array. If you'd prefer to work with an array of message objects, then you can skip this step and also step 3b. Generic lists are generally considered easier to work with, so we will make this change here. The code samples below will assume list collections are used instead of arrays.
Right-click the service that you just added and select the Configure Service Reference option from the menu. The dialog shown in step 3b will appear. Proceed to step 3b.

step3

Step 3b - The Collection Type option defaults to System.Array. Click the drop-down and select System.Collections.Generic.List. Selecting this option allows you to work with message collections as a list of messages objects rather than an array of message objects.

step3

This completes the service setup. Time to write some code...

Step 4 – Test connectivity using the Ping() method. The service includes a Ping() utility that can used to verify basic connectivity. The code below demonstrates how to call the Ping method.
A successful call to the ping method should return the string "TextPort .NET 3.5/4.0 WCF service is up". If you receive this result, then connectivity is good and you can move forward to sending messages.

                        private void pingTextPort()
                        {
                            string pingResult = pingTextPortSMSService();
                            Console.WriteLine(pingResult);
                        }

                        private string pingTextPortSMSService()
                        {
                            try
                            {
                                using (TextPortSMSService.TextPortSMSClient textPortSvc = new TextPortSMSService.TextPortSMSClient())
                                {
                                    return textPortSvc.Ping();
                                }
                            }
                            catch (Exception ex)
                            {
                                return "Ping request failed. Message: " + ex.Message;
                            }
                        }
                    

Step 5 – Send some messages. The code block below initializes an instance of the TextPortSMS service and sends two messages. Copy/paste the code below, or use it as a guide for your own implementation. Remember to replace the usrename, password, mobile numbers and messages with your own values.

                        private void submitSMSToTextPort()
                        {
                            using (TextPortSMSService.TextPortSMSClient textPortSvc = new TextPortSMSService.TextPortSMSClient())
                            {
                                // Use your TextPort.com login and password here.
                                TextPortSMSService.TextPortSMSMessages messageList = new TextPortSMSService.TextPortSMSMessages()
                                {
                                    UserName = "your_username_here ",
                                    Password = "your_password_here",
                                    Messages = new List<TextPortSMSService.TextPortSMSMessage>()
                                };

                                // Add first message
                                messageList.Messages.Add(new TextPortSMSService.TextPortSMSMessage()
                                {
                                    CountryCode = "1",
                                    MobileNumber = "2135551212",
                                    MessageText = "Place the body of the text message in this field."
                                });

                                // Add second message
                                messageList.Messages.Add(new TextPortSMSService.TextPortSMSMessage()
                                {
                                    CountryCode = "1",
                                    MobileNumber = "9045552146",
                                    MessageText = "This is the body of the second message in the request."
                                });

                                // Add more messages here if needed. In a production environment, the addition of messages to the
                                // list would most-likely be handled by a loop if there are multiple messages to send.

                                // Submit the messages to the TextPort service
                                TextPortSMSService.TextPortSMSResponses responseList = textPortSvc.SendMessages(messageList);

                                foreach (TextPortSMSService.TextPortSMSResponse resp in responseList.Responses)
                                {
                                    Console.WriteLine("Message #" + resp.ItemNumber.ToString() + " submitted with result " + resp.Result);
                                }
                            }
                        }