Setting DataSet.ReadXML Timeout Values#

One of the things my website does is take advantage of the nifty and easy to use DataSet.ReadXml Method and its ability to extract RSS data using a simple URL as one of its parameters. 

ds = new DataSet();

ds.ReadXml(URLpath, XmlReadMode.Auto);

However, when I implemented it I didn't consider that it doesn't have a timeout value, thus you have no way to control how long a remote RSS call can be permitetd to take with the exception of relying on default timout values.  When my main site was having an issue extracting one of the remote RSS feeds my attention was drawn to the issue!

The solution (albeit somewhat long winded when the ReadXml method takes a URL) is to implement the actual RSS extraction via some form of webClient call, taking advantage of .NET's nifty capability for web type requests.  My first attempt looked at the actually WebClient class, which worked well at giving me the remote RSS as a stream I could bind the dataset to via ReadXml, but has no timeout implementation.  So while code was simple, still didn't solve my immediate problem.

DataSet ds;

WebClient wc = new WebClient();

Stream stream = wc.OpenRead(URLpath);

StreamReader sReader = new StreamReader(stream);

string mYxml = sReader.ReadToEnd();

ds = new DataSet();

ds.ReadXml(new StringReader(mYxml), XmlReadMode.InferSchema);

So, sniffing about as to which of the methods have an actual timeout value brought me to the WebRequest object, which again takes a URL as its argument, but unlike WebClient allows me to set a timeout value in milliseconds.

WebRequest req = WebRequest.Create(URLpath);

req.Timeout = 5000;

So, what was a one line implementation has turned into something a bit more significant, but ultimately more controllable.

<%@ Page Language="C#" %>

<%@ import Namespace="System.Data" %>

<%@ import Namespace="System.Net" %>

<%@ import Namespace="System.IO" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

private void Page_Load(object sender, System.EventArgs e){

string URLpath = http://www.asp.net/modules/articleRss.aspx;

string mYxml = "";

string err = "";

DataSet ds = new DataSet();

int RecordCount = 0;

try

{

WebRequest req = WebRequest.Create(URLpath);

req.Timeout = 5000;

WebResponse result = req.GetResponse();

Stream ReceiveStream = result.GetResponseStream();

Byte[] read = new Byte[512];

int bytes = ReceiveStream.Read(read, 0, 512);

while (bytes > 0){

Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

mYxml += encode.GetString(read, 0, bytes);

bytes = ReceiveStream.Read(read, 0, 512);

}

}

catch (System.Xml.XmlException caught) { err = caught.Message.ToString(); }

catch (System.Net.HttpListenerException caught) { err = caught.Message.ToString(); }

catch (Exception caught) { err = caught.Message.ToString(); }

if (err == ""){

ds.ReadXml(new StringReader(mYxml), XmlReadMode.InferSchema);

RecordCount = ds.Tables[2].Rows.Count;

if (RecordCount > 0){

GridView1.DataSource = ds.Tables[2];

GridView1.DataBind();

}

}else{

lblHttpContext.Text = err.ToString();

}

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>RSS Timout Example</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView1" runat="server">

</asp:GridView>

<asp:Label ID="lblHttpContext" runat="server" Text="Label"></asp:Label><br />

</form>

</body>

</html>

10/23/2006 12:20:23 PM (GMT Standard Time, UTC+00:00) #    Comments  |  Trackback

 

All content © 2009, John Timney
On this page
This site
Calendar
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Sitemap
Blogroll OPML
Talk to Me

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail