About
This project presents a simple WCF Positive Affirmations Service that demos architectural styles of setting up, hosting via Azure Cloud Services, configuring services, and testing the service for http protocols. The service accepts a simple string, which represents a name, and returns a simple string with a randomized positive affirmation that acknowledges the name. Instead of using IIS Express, the simple service is hosted using Azure Cloud Services. The Visual Studio solution also has one client “tester” Windows Form application that tests the http protocol connection to the hosted service.
Architecture
The demo project consists of these component topics:
- PositiveAffirmationsWCFServiceWebRole Class Library for Cloud Service
- IPositiveAffirmationService (Interface for Service)
- PositiveAffirmationService (Code that Implements the Service Interface)
- MyAzureWCFServiceDemo “Service Host” Azure Cloud Service
- Configuration Settings for Hosting the Service
- Cloud Publishing Settings
- TestClientGUI “Tester to Service” Windows Form Application
- Connected Service “Proxy Reference”
- Simple Program to test the service @ http endpoint
Windows Azure Cloud Service Project Template
A Windows Azure Cloud Service project template (a project that creates a scalable service that runs on Windows Azure) was selected in Visual Studio. In template wizard, the WCF Service Web Role was selected for WCF services and named “PositiveAffirmationsWCFServiceWebRole”. The template process completed with the creation of two separate projects: the “WCF Service Web Role” and the “Cloud Service Project” (MyAzureWCFServiceDemo).
Positive Affirmations “PositiveAffirmationsWCFServiceWebRole” Service Library
The “WCF Service Web Role” project looks very similar to the “WCF Service Application” template, but customized for Cloud. There is a service interface “IPositiveAffirmationService” and a class “PositiveAffirmationService” that implements the service interface. The code is available on GitHub [here].
IPositiveAffirmationService (Interface for Service)
The ServiceContract for the Simple Greeting service has only one OperationContract: a method called “AffirmMe” that accepts a string representing a name. The “AffirmMe” returns string to the caller that includes a positive affirmation plus the name. The code is available on GitHub [here].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/// <summary> /// IPositiveAffirmationService /// A WCF Service that allows a user to retrieve a /// positive affirmation response in the form of a /// string. They may also send their name for a /// personalized response /// </summary> [ServiceContract] public interface IPositiveAffirmationService { /// <summary> /// AffirmMe /// </summary> /// <param name="name">name of the person (optional)</param> /// <returns>a positive affirmation (string)</returns> [OperationContract] string AffirmMe(string name); } |
PositiveAffirmationService (Code that Implements the Service Interface)
The service implementation code details the “AffirmMe” method. It returns a simple string containing a positive affirmation plus the name that the caller sent. A list of approximately 30 positive affirmations are loaded into memory when the service spins up with a random number generator. When the service is called, it randomly returns an item from the list of positive affirmations along with a personalized message to return to the caller.
After my first attempt of publishing the service and accessing the service, I realized that I was receiving ASP.NET incompatibility errors in both the web browser and my proxy connection via the Client Tester application. The solution was to include a class level attribute:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
The code is available on GitHub [here].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
/// <summary> /// Implements the AffirmationService Interface /// </summary> [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class PositiveAffirmationService : IPositiveAffirmationService { // List to hold my positive affirmations private List<string> myAffirmations; // Random number for picking which one to return private Random random; // Random number private int number; public PositiveAffirmationService() { // Initialize the in-memory lists Initialize(); } // end of constructor /// <summary> /// AffirmMe /// Implements the Interface /// Returns a positive affirmation /// </summary> /// <param name="name">name of the person (string) with default "Stranger"</param> /// <returns>a positive affirmation (string)</returns> public string AffirmMe(string name) { // Make Sure Initialized if (myAffirmations == null) { // Load the in-memory list Initialize(); } // Get random number number = random.Next(myAffirmations.Count); // Return the list item return name + ", say this: " + myAffirmations[number]; } // end of method /// <summary> /// Initalize() /// Load the list of positive affirmations into memory /// Initialize the random number /// </summary> public void Initialize() { // Initialize the List myAffirmations = new List<string>() { "I am love. I am purpose. ...", "I don't sweat the small stuff. (@gabbybernstein) ...", "I can. ...", "I am adventurous. ...", "I feed my spirit. ...", "I am in charge of how I feel and today I am choosing happiness. ...", "I am my own superhero. ...", "I will not compare myself to strangers on the Internet", "Every now and then, there are days when you just need a little pick-me-up. ...", "I'm allowed to take up space.", "My past is not a reflection of my future.", "I am smart enough to make my own decisions.", "I'm in control of how I react to others.", "I choose peace.", "I'm courageous and stand up for myself.", "I will succeed today.", "I create a safe and secure space for myself wherever I am.", "I give myself permission to do what is right for me.", "I am confident in my ability to [fill in the blank].", "I use my time and talents to help others [fill in the blank].", "What I love about myself is my ability to [fill in the blank].", "I feel proud of myself when I [fill in the blank].", "I give myself space to grow and learn.", "I allow myself to be who I am without judgment.", "I listen to my intuition and trust my inner guide.", "I accept my emotions and let them serve their purpose.", "I give myself the care and attention that I deserve.", "My drive and ambition allow me to achieve my goals.", "I share my talents with the world by [fill in the blank].", "I am good at helping others to [fill in the blank].", "I am always headed in the right direction.", "I trust that I am on the right path.", "I am creatively inspired by the world around me.", "My mind is full of brilliant ideas.", "I put my energy into things that matter to me.", "I trust myself to make the right decision.", "I am becoming closer to my true self every day.", "I am grateful to have people in my life who [fill in the blank].", "I am learning valuable lessons from myself every day.", "I am at peace with who I am as a person.", "I make a difference in the world by simply existing in it." }; // Set the Random number random = new Random(); } // end of method |
MyAzureWCFServiceDemo “Service Host” Azure Cloud Service Project
Instead of hosting with IIS Express, the Positive Affirmations Service is hosted and managed via Azure Cloud Services with specific ports, configuration, endpoints, etc. to allow access via http protocol (suited for Internet traffic). The cloud service project is published to and managed via a Windows Azure Portal that starts, stops, and monitors the service. The project includes publishing and configuration settings for the cloud service. After the solution is built without errors, it can be deployed to the cloud using Visual Studio Build à Publish. Note: The cloud project must be selected for the Publish option to be visible under the Build menu options. The user will then be prompted to login to their Azure account and confirm the publishing settings.
The code is available on GitHub [here].
HttpClient “Tester to Service” Windows Form Application (TestClientGUI)
The HttpClient “tester to service” is a simple windows form application project in the same solution that connects to the “Positive Affirmations Service” by use of a proxy generated by SVCUTIL. The client program will use this proxy and the http protocol to test the OperationContract or method available in the ServiceContract and return the results to the user on the GUI.
The code is available on GitHub [here].
Connected Service “Proxy Reference” MyAffirmServiceRef
I used the simple “Add Service Reference” wizard to create a Service Reference to the client tester project. The existing “Positive Affirmations” Azure Cloud Service had to be already published and running for the SVCUTIL wizard to be able to connect to the metadata exchange via the base address provided and add a service reference to my client. Note: the base address provided was specific to the http protocol. For the http client, the metadata would be available after entering this address into the wizard:
http://kathleenwest.cloudapp.net/PositiveAffirmationService.svc
The client was able to then recognize the Positive Affirmations Azure Cloud Service and build the service reference using the exposed meta-exchange data “WSDL”. The auto-generated code is available on GitHub [here].
Note: There is no warranty that this service is still currently running and available. This service was created for a demo project only under a student Azure subscription that has an expiration date.
Main Form Program
The main form program in the client “tester” windows form application creates a proxy using the service reference that was previously created using the wizard, to connect to the simple positive affirmations service. Since there is one service endpoint (http), the proxy does not need to specify the specific endpoint name on the service as it relates to the protocol.
PositiveAffirmationServiceClient proxy = new PositiveAffirmationServiceClient();
Good to Know – Endpoints and Client Implementations
The service could also be implemented using the https protocol, and it in that case, the endpoint would have to be specified in the constructor above. The endpoint name for the specific endpoint differentiates the specific protocol (ex: https). The default endpoint name is available in the app configuration file on the service host, so the client has to be aware of the endpoints. They can also learn this from the metadata exchange or WSDL on the service. The correct endpoint name for the protocol must be specified in the creation of the proxy to use that protocol if it not the default.
Testing the Service
After the proxy is setup, the tester windows form application loads the GUI and allows the user to enter their name. After they click the button “AffirmMe”, the program then makes a request to the service (instancing the proxy) passing their name as a string parameter. After it receives the response from the service, it outputs the result as a string to the text box on the form. If it cannot connect to the service or fails, it will say so in the text box on the GUI. The result is a personalized positive affirmation to the user who requested the service. The code is available on GitHub [here].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
private void btnAffirmMe_Click(object sender, EventArgs e) { string response = ""; // Try to Connect to the service try { PositiveAffirmationServiceClient proxy = new PositiveAffirmationServiceClient(); // Check to See if Name is Available if (!String.IsNullOrEmpty(txtName.Text)) { // Call the Service with No parameter for Name response = proxy.AffirmMe(txtName.Text); } else { // Call Service without the Name Parameter response = proxy.AffirmMe("Stranger"); } } catch (Exception) { response = "Sorry, but the service is unavailable."; } // Load the affirmation txtMessage.Text = response; } // end of method |
Azure Cloud Demo
The following are screen captures taken from my Azure Portal that manages the resource for the service.
Client “Tester” Demo
Code
The entire project code repository is available on GitHub [here].