Likes

Developing Windows Azure and Web Services Lab @ 5 Ans 2

To support RSS feeds of flight schedule you will create a new media type that handles the RSS requests, and then register the new media formatter class in the Http Configuration formatters.
The main tasks for this exercise are:
1. Create a new class that derives from MediaTypeFormatter.
2. Implement the necessary code to return an RSS response containing flight schedule information.
3. Register the new media formatter.
4. Use Internet Explorer to view the feed.



Task 1: Creating a New Class that Derives from MediaTypeFormatter
To create a new class that derives from MediaTypeFormatter. you need to perform the following steps:
1. Browse to the location where the Exercise 02.zip file is saved.
2. Extract the files.
3. Press the Windows logo key. The Start screen is displayed.
4. Start typing Visual Studio 2012.
5. Right-click the Visual Studio 2012 tile.The App bar is displayed.
6. Click the Run as administrator button.
7. Select FILE—Open—Project-Solution. The Open Project dialog box is displayed.
8. Browse to the location where the Exercise 02 folder is extracted.
9. Double-click the Exercise 02 folder.
10. Double-click the BlueYonder.Companion folder.
11. Select the BlueYonder.Companion.sin file.
12. Click the Open button. The BlueYonder.Companion - Microsoft Visual Studio (Administrator) window is displayed.
13. Ensure that the Solution Explorer window is opened.
14. Right-click the BlueYonder.Companion.Controllers node, and then select Add—New Folder.
15. Replace the existing text of the folder name with Formatters, and then press the Enter key.
16. Right-click the Formatters folder, and then select Add—Class. The Add New Item -BlueYonder.Companion.Controllers dialog box is displayed.
17. Select and replace the existing text in the Name text box with Atom Formatter.
18. Click the Add button. The AtomFormatter.es file is displayed.
19. Type the highlighted portions of the following code snippet in the AtomFormatter.es file:
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml;
using BlueYonder.Companion.Entities; using System.Net.Http; using System.Net.Http.Formatting; using System.Net.Http.Headers; using System.ServiceModel.Syndication;
namespace BlueYonder.Companion.Controllers.Formatters
{
public class AtomFormatter : MediaTypeFormatter
{
}
}
20. Select FILE— Save All to save the changes.
Task 2: Implementing the Necessary Code to Return RSS Response Containing Flight Schedule Information
To implement the necessary code to return RSS response containing flight schedule information, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Ensure that the AtomFormatter.es file is opened.
3. Type the highlighted portions of the following code snippet in the AtomFormatter.es file:
class AtomFormatter : MediaTypeFormatter
{
private HttpRequestMessage request;
//Add the new media type header to the supported media types, public AtomFormatterQ {
SupportedMediaTypes.Add(new MediaTypeHeaderValue ("application/atom+xml") );
>
//Is used to receive the instance of the HttpRequestMessage class, public AtomFormatter(HttpRequestMessage request)
{
_request - request;
}
//Overrides the CanReadType method, to define it is not supporting deseriali public override bool CanReadType(Type type)
{
return false;
}
//Overrides the CanWriteType method, to define it supports serialization of FlightWithSchedulesDTO class instances.
public override bool CanWriteType(Type type)
{
>
return type == typeof(FlightWithSchedulesDTO);
private SyndicationFeed CreateFeed(FlightWithSchedulesDTO flight)
{
var feed = new SyndicationFeed
{
Title = new TextSyndicationContent(string.Format("Blue Yonder flight {0}", flight.FlightNumber))
};
var items = from s in flight.Schedules select new Syndicationltem
{
Title = new TextSyndicationContent(
String.Format("Flight {0} {1}", flight.FlightNumber, s.Departure.ToString(”MMMM dd, yyyy")))>
Id = flight.FlightNumber,
BaseUri = new Uri(_request.RequestUri, string.Format("{0}/{1}",
_request.RequestUri.AbsolutePath, flight.FlightNumber)),
>;
feed.Items - items; return feed;
}
public override Task WriteToStreamAsync(Type type, object value, System.10 writeStream, RttpContent content, System.Net.TransportContext transportContext)
{
// creating a System.ServiceModel.Syndication.SyndicationFeed var feed - CreateFeed(value as FlightWithSchedulesDTO);
return Task.Run(() =>
{
using (var writer = XmlWriter.Create(writeStream))
{
Atoml0FeedFormatter atomformatter = new Atoml0FeedFormatter(fe< atomformatter.WriteTo(writer);
}
»;
}
//Overrides the base GetPerRequestFormatterlnstance method, to create a nei instance of the AtomFormatter class for each atom request.
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type typ< HttpRequestMessage request,
MediaTypeHeaderValue mediaType)
{
return new AtomFormatter(request);
}
}
}
4. Select FILE— Save All to save the changes.
Task 3: Registering the New Media Formatter
ro register the new media formatter, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Expand the BlueYonder.Companion.Host node.
3. Expand the App_Start folder.
4. Double-click the WebApiConfig.es file. The WebApiConfig.es file is displayed.
5. Type the highlighted portions of the following code snippet in the WebApiConfig.es file:
using System.Web.Http.Routing; using System.Net.Http;
using BlueYonder.Companion.Controllers.Formatters;
namespace BlueYonder.Companion.Host
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Add(new AtomFormatterQ); config.DependencyResolver ■ new BlueYonderResolverQ; config.MessageHandlers.Add(new AtomHandler()>;
6. Select FILE— Save All to save the changes.
Task 4: Using Internet Explorer to View the Feed
To use Internet Explorer to view the feed, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Ensure that the BlueYonder.Companion.Host project is set as startup project.
3. Press the F5 key. The Internet Explorer window is displayed.
4. Append atom/flights/1 to the address in the address bar. and then press the Enter key. The details of a flight are
5. Switch to Microsoft Visual Studio 2012.
6. Press the Shift+F5 keys.
7. Close Microsoft Visual Studio 2012.

No comments: