I have mostly worked in systems technologies, building things like operating system, hypervisor, networking protocols and other virtualization related technologies. I love building system software due to simplicity of the overall software stack, programming languages (C and assembly) and tools to build this software. During my work on Windows Network Controller, I spent some time building WCF services in C#. I really liked coding in C# as it is a very well designed high level language and makes coding some of things much faster. However, at times I felt lost in the myriad of abstractions, frameworks and tools. Fortunately, for the service I was building, all the framework details were handled by a core framework team, and that allowed me to focus on writing the core business logic for my service without getting bogged down in all the other details. Even though I was able to build my service faster; I continued to feel lost at the magic, framework components performed; to do things like cross machine API invocation, associated security handling etc.
Lately, I started working on creation of a new Azure cloud service. Using Visual Studio to create the service resurfaced that same feeling of being “lost” with auto-generated files and black magic happening underneath. Now don’t take me wrong, I like Visual Studio for writing C# code, it is a great tool, but all the auto-generated code and build files hides details that I feel are necessary for proper understanding of the system. To ensure that I have good understanding of the overall mechanics of a cloud service, I decided to write a simple cloud service from scratch without using Visual Studio.
I am happy that I did, as it allowed me to understand exactly what is involved in building a service and how various frameworks and tools work to create an end to end service. Along the way I found many useful articles that improved my overall understanding of WCF and Azure cloud services.
Anatomy of “our” cloud service
For the sample, I decided to build a WCF service and host that service in a console program as well as in a worker role in Azure cloud service. If you are not familiar with Azure cloud services, you may want to read this series of articles first. You can build a Azure cloud service without WCF, but I decided to add WCF to allow clients to interact remotely with this service. The overall architecture of this service is shown below.
The source code for the service is available at: https://github.com/logicpundit/smplsvc/tree/v1.0. The key components of this service are described below.
Service Contract: A service contract is the interface for a WCF service. It can contain both operation/function contracts and data contract. This interface allows clients to interact with the service either locally or remotely. In our case, we have implemented just one operation, namely Echo, that echoes back whatever message is sent to it. The contract is implemented in WcfSmplSvcCntrct.cs and built as a .net library (LogicPundit.Samples.WcfSvc.Contract.dll).
Service Implementation: This is the actual implementation of the service and it implements the operation (and data) contract as specified in the service contract library. The service implementation is done in WcfSmplSvc.cs and built as a .net library (LogicPundit.Samples.WcfSvc.dll).
Console Service Host: A WCF service requires a host to run the service. I have created one command line host to host the service. This makes it easy to run and test code (at least early in development process). This is implemented in WcfSmplHostCmd.cs and built as an executable (LogicPundit.Samples.WcfSvc.Host.Console.exe).
Azure Cloud Service Host: This is a second host implementation for our service. For this, I decided to host the WCF service in a Azure worker role. This service implements minimum interface for Azure worker role and starts the WCF service. It is implemented in WcfSmplHostAzWrkrRole.cs and built as a .net library (LogicPundit.Samples.WcfSvc.Host.AzureWorkerRole.dll).
Service Configuration: A service configuration file describes the configuration for an Azure cloud service. It allows one to package a service once, but deploy it multiple times by modifying the configuration file. It is implemented in WcfSmplSvc.cscfg.
Service Definition: A service definition defines the service and various configuration knobs and other parameters. It also defines which endpoints (e.g. TCP ports) needs to be opened for the service to function, whether the service should run in elevated mode or not etc. It is implemented in WcfSmplSvc.csdef.
Service Client: This is a command line client that interacts with the service. It can talk to the service irrespective of whether service is hosted in console program or Azure cloud service. It creates a WCF channel to the service and invokes the only operation we support i.e. Echo and prints the output on the screen. It is implemented in WcfSmplSvcClnt.cs and built as an executable (LogicPundit.Samples.WcfSvc.Client.exe).
The build script for these components is cmpl.cmd and is available in the github repository mentioned above. Besides these components, we use a tool called cspack.exe (that is part of Azure SDK) to create a package for Azure cloud service. The package (.cspkg) contains all the binaries and definition file for the service. Once the package is created, you can upload it in the cloud service by going to Azure portal along with service configuration (.cscfg) file.
That is all it takes to build an Azure cloud service from scratch and without using visual studio. I wrote all the code in VIM and compiled it in terminal. I used Azure portal UI to upload the code to my cloud service, though I could have used PowerShell as well.
Now that I have a better understanding of how WCF services and Azure cloud services work, the next step is to start building the real service and along the way add security support and maybe REST API support. There are many articles that helped me understand this as well and those are referenced below.
- WCF the manual way…the right way
- How to create a WCF service without Visual Studio
- Microsoft Azure Cloud Services Introduction
- Source code for this sample on github