Creating a WCF REST Service in the Cloud

0
180

The TailSpin.Services.Surveys project includes a standard WCF REST Service named SurveysService hosted in Windows Azure that exposes the survey data to the Windows Phone 7 client application. The Windows Azure web role defined in the TailSpin.Services.Surveys. Host.Azure project populates a routing table that includes a route to the Surveys service in its Global.asax.cs file. The following code example shows how the RegisterRoutes method creates the Route Table object.

C#
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private static void RegisterRoutes()
{
var customServiceHostFactory = new
CustomServiceHostFactory(ContainerLocator.Container);

RouteTable.Routes.Add(new ServiceRoute(“Survey”,
customServiceHostFactory, typeof(SurveysService)));

}
}

This SurveysService class implements the WCF REST service endpoints. The following code example shows the GetSurveys method in the SurveysService class that exposes the surveys data stored in Windows Azure storage.

C#
public SurveyDto[] GetSurveys(string lastSyncUtcDate)
{
DateTime fromDate;
if (!string.IsNullOrEmpty(lastSyncUtcDate))
{
if (DateTime.TryParse(lastSyncUtcDate, out fromDate))
{
fromDate = DateTime.SpecifyKind(
fromDate, DateTimeKind.Utc);
}
else
{
throw new FormatException(“lastSyncUtcDate is in an incorrect
format. The format should be: yyyy-MM-ddTHH:mm:ss”);
}
}
else
{
fromDate = new DateTime(
1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);
}
var username = Thread.CurrentPrincipal.Identity.Name;
return this.filteringService
.GetSurveysForUser(username, fromDate)
.Select(s => new SurveyDto
{
SlugName = s.SlugName,
Title = s.Title,
Tenant = s.Tenant,
Length = 5 * s.Questions.Count,
IconUrl = this.GetIconUrlForTenant(s.Tenant),
CreatedOn = s.CreatedOn,
Questions = s.Questions.Select(q =>
new QuestionDto
{
PossibleAnswers = q.PossibleAnswers,
Text = q.Text,
Type = Enum.GetName(typeof(QuestionType), q.Type)
}).ToList()
}).ToArray();
}

This example shows how the Surveys service returns survey definitions to the mobile client in an array of SurveyDto objects that represent surveys added to the service after a specified date. It also demonstrates applying a filter to the request. In the current version of the application, the filter returns a list of surveys from the phone user’s preferred list of tenants. For more information about how Tailspin implemented the filtering behavior, see the section

To enable the mobile client to upload survey answers, the Surveys Service class provides two methods: one for uploading individual images and sound clips, and one for uploading complete survey answers. The following code example shows how the AddMediaAnswer method saves an image or a sound clip to Windows Azure BLOB storage and returns a URI that points to the BLOB.

C#
public string AddMediaAnswer(Stream media, string type)
{
var questionType =
(QuestionType)Enum.Parse(typeof(QuestionType), type);
return this.mediaAnswerStore.SaveMediaAnswer(media,
questionType);
}

The following code example shows the AddSurveyAnswers method that receives an array of SurveyAnswerDto objects that it unpacks and saves in the survey answer store in Windows Azure storage.

C#
public void AddSurveyAnswers(SurveyAnswerDto[] surveyAnswers)
{
foreach (var surveyAnswerDto in surveyAnswers)
{
this.surveyAnswerStore.SaveSurveyAnswer(
new SurveyAnswer
{
Title = surveyAnswerDto.Title,
SlugName = surveyAnswerDto.SlugName,
Tenant = surveyAnswerDto.Tenant,
QuestionAnswers = surveyAnswerDto.QuestionAnswers
.Select(qa => new QuestionAnswer
{
QuestionText = qa.QuestionText,
PossibleAnswers = qa.PossibleAnswers,
QuestionType = (QuestionType)Enum.Parse(
typeof(QuestionType), qa.QuestionType),
Answer = qa.Answer
}).ToList()
});
}
}