14.01.2015 Views

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

public override bool CanWriteType(Type type)<br />

{<br />

if (type == typeof(TimeCard))<br />

{<br />

return true;<br />

}<br />

}<br />

Next comes the crux of the formatter - the WriteToStream method.<br />

We cast the incoming object into a TimeCard and call the<br />

WriteVCalendar method that writes to the stream buffer.<br />

public override void WriteToStream(Type type, object<br />

value, Stream stream, System.Net.Http.HttpContent<br />

content) {<br />

using (var writer = new StreamWriter(stream))<br />

{<br />

var timeCard = value as TimeCard;<br />

if (timeCard == null)<br />

{<br />

throw new InvalidOperationException(“Cannot serialize<br />

type”);<br />

}<br />

WriteVCalendar(timeCard, writer);<br />

}<br />

stream.Close();<br />

}<br />

As we can see below, VCalendar items are simply formatted text<br />

and we use the data in our TimeCard to create the appropriate<br />

string for the VCalendar.<br />

private void WriteVCalendar(TimeCard contactModel,<br />

StreamWriter writer) {<br />

var buffer = new StringBuilder();<br />

buffer.AppendLine(“BEGIN:VCALENDAR”);<br />

buffer.AppendLine(“VERSION:2.0”);<br />

buffer.AppendLine(“PRODID:-//DNC/DEMOCAL//NONSGML v1.0//<br />

EN”);<br />

buffer.AppendLine(“BEGIN:VEVENT”);<br />

buffer.AppendLine(“UID:suprotimagarwal@example.com”);<br />

buffer.AppendFormat(“STATUS:{0}\r\n”, contactModel.<br />

Status);<br />

buffer.AppendFormat(“DTSTART:{0}Z\r\n”, (contactModel.<br />

StartDate.ToFileTimeUtc().ToString()));<br />

buffer.AppendFormat(“DTEND:{0}Z\r\n”, (contactModel.<br />

EndDate.ToFileTime().ToString()));<br />

buffer.AppendFormat(“SUMMARY:{0}\r\n”, contactModel.<br />

Summary);<br />

buffer.AppendFormat(“DESCRIPTION:{0}\r\n”, contactModel.<br />

Description);<br />

buffer.AppendLine(“END:VEVENT”);<br />

buffer.AppendLine(“END:VCALENDAR”);<br />

writer.Write(buffer);<br />

}<br />

Associating Custom Formatter<br />

in our <strong>Web</strong>API Application<br />

This is a single line of configuration in code. In the<br />

App_Start\<strong>Web</strong>ApiConfig.cs, we add the following in the last line<br />

of the Register method<br />

config.Formatters.Add(new VCalendarMediaTypeFormatter());<br />

If you don’t have the <strong>Web</strong>ApiConfig.cs, you can use<br />

GlobalConfiguration.Configuration.Formatters.Add(new<br />

VCalendarMediaTypeFormatter());<br />

That’s it we are done.<br />

Testing the Custom Formatter<br />

Out<br />

Our Formatter is all set, how can we test this Well we can test<br />

it using Fiddler by creating a new Request as follows. In the<br />

Composer Tab, we set the Request Type to a GET put the following<br />

headers<br />

Host: localhost:54882<br />

User-Agent: Fiddler<br />

Accept: text/calendar<br />

Accept-Encoding: text<br />

Content-Type: text/calendar<br />

Connection: keep-alive<br />

We post it to the URL<br />

http://localhost:54882/api/TimeCardId=1<br />

70 | DNCmagazine www.dotnetcurry.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!