HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

HttpWeb* and XmlReader Async?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andxmlreaderhttpwebasync

Problem

I've run into the need to deserialize XML that returns from an http request. I've done some light work with XML deserialization in the past but that was against files on disk.

While attempting to write the code I noticed that many of the methods in HttpWeb* and XmlReader had both an async and a regular version. I'd glossed over await/async material in the past but have never done anything with it. This seemed like a good opportunity to kill two birds.

Below is my current attempt at both types of processing. You should be able to copy/paste the code into a C# Console App, add references to System.Runtime.Serialization and System.Xml.Linq, and then run.

I think I'm using everything properly and I'm actually pretty pleased with the code. However, I don't pretend to know enough about what I'm doing here yet to be confident that there aren't pitfalls that I'm missing.

```
using System;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

class Program
{
static void Main() {
string url = @"http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx/GetHolidaysForDateRange?countryCode=UnitedStates&startDate=2000-01-01T00:00:00&endDate=2014-01-01T00:00:00";

// Run Synchronous
var sw1 = new System.Diagnostics.Stopwatch();
sw1.Start();
DoStuff(url);
sw1.Stop();

// Run Asynchronous
var sw2 = new System.Diagnostics.Stopwatch();
sw2.Start();
DoStuffAsync(url).Wait();
sw2.Stop();

Console.WriteLine("Sync Elapsed:{0}", sw1.Elapsed.TotalMilliseconds);
Console.WriteLine("Async Elapsed:{0}", sw2.Elapsed.TotalMilliseconds);

Console.ReadKey();
}

private static void DoStuff(string url) {
var stream = GetHttpResponse(url, @"application/xml");

// Xml Processing

Solution

I understand that this is an educational exercise, but naming is important. One and two letter names are highly discouraged. When Mr. Maintainer has to map letters to meanings it becomes difficult for him to focus on what's actually happening, because he has to constantly remember thate means element and dcs is a DataContractSerializer. I'm not even going to mention how terrible DoStuff and DoStuffAsync are. Oops, I just did. =;)-

It's also standard in C# to put opening braces on a new line instead of using Java style Egyptian braces. A dev who swims in C# on a regular basis may have a hard time reading this code as it is. You were consistent though, so I can't complain a whole lot about it.

I question the use of a switch statement in ProcessXmlStream. Typically, switch is used when there are multiple cases to test. I think an If statement would work just fine there.

Context

StackExchange Code Review Q#58109, answer score: 4

Revisions (0)

No revisions yet.