[Mono-list] Simple app works in .NET 1.1 but not in Mono

Kevin B. db@ke5in.com
Thu, 3 Feb 2005 15:24:03 -0500 (EST)


------=_20050203152403_50348
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

Hi,

Attached is a simple console app that will compile in .NET Framework 1.1
and will run correctly.  It compiles in Mono on fc2 but it "sort of" does
not run correctly.

The code is supposed to download historical data from the yahoo website. 
Under windows it will correctly download all ten items in the array (Item
5 on the list - ATH - is a failure test case).  On my fc2 setup the code
will download only the first two items and then it timeouts for all the
rest.  One of the items (ATH) does not exist on yahoo.  In windows I get a
the expected 404 error but in Mono I still get the timeout.  It does not
matter what I set the time out value to, the Mono version will
consistently time out for the last eight items.   Its very stange that it
works for the first two items.

I'm not sure what to look for at this point.  My guess is that there is
something going on with the WebRequest object.  Maybe I'm not closing
something and the .NET CLR is more forgiving or maybe my fc2 setup is
corrupt?  I reinstalled mono 1.0.5 from the rpms but that did not fix
it....

I'm out of ideas and hope one of you guys can point me the the right
direction.

Thanks in advance for any help.

The attached file will compile and run by doing the following:

mcs Class1.cs
mono Class1.exe

Thanks again,
Kevin
------=_20050203152403_50348
Content-Type: text/x-csharp; name="Class1.cs"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="Class1.cs"

using System;
using System.Threading;
using System.Collections;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;

namespace ConsoleApplication6
{

	
	
	public class YahooData	
	{
		public YahooData(){}
		public void GetHistoricalDays(string Symbol,DateTime StartDay,DateTime EndDay,ref ArrayList DaysTraded)
		{

			string sURL;

			sURL = "http://table.finance.yahoo.com/table.csv?s="
				+Symbol
				+"&d="+Convert.ToString(EndDay.Month-1)
				+"&e="+Convert.ToString(EndDay.Day)
				+"&f="+Convert.ToString(EndDay.Year)
				+"&g=d"
				+"&a="+Convert.ToString(StartDay.Month-1)
				+"&b="+Convert.ToString(StartDay.Day)
				+"&c="+Convert.ToString(StartDay.Year)
				+"&ignore=.csv"; 
			WebRequest wrGETURL;			
			Stream objStream;
			int i = 2;//Globals.WebRetries;
			while(i>-1)
			{
				
				try
				{	
					wrGETURL = WebRequest.Create(sURL);
					wrGETURL.Timeout = 30000;
					//wrGETURL.Proxy = WebProxy.GetDefaultProxy();
					Console.WriteLine("Trying to get: " + Symbol);
					objStream = wrGETURL.GetResponse().GetResponseStream();
					Console.WriteLine( Symbol + ": No exception occured");
					
					//Read in the data 
					StreamReader objReader = new StreamReader(objStream);
					string sLine ="";
					sLine = objReader.ReadLine(); //header
					sLine = objReader.ReadLine(); //first line of data
					while(sLine!=null)
					{
						DaysTraded.Add(sLine);//(new TradeVector(Symbol,sLine));							
						sLine = objReader.ReadLine();
					}
					objReader.Close();
					DaysTraded.Reverse();
					return; //probably not the best way to leave to the try/catch block
					//but outside it there is a retry loop 
				}
				catch(WebException e)
				{ 	
					Console.WriteLine("Problem: " + e.Message); 
					WebErrorHandler(e,i,Symbol);
				}			
				i--;
			}
			return;
		}

		private void WebErrorHandler(WebException e, int i, string Symbol)
		{
			//ProtocolError handles the case when yahoo, for whatever reason,
			//says that the page is "temporarily" unavailable and gives a NotFound.
			//I'm treating it as a "timeout" in the sense that I use the "i" WebRetries
			//Doing it that way makes it easier then adding another another type of retry
			if(e.Status==WebExceptionStatus.ProtocolError)
			{
				HttpWebResponse response = e.Response as HttpWebResponse;
				if (response != null) 
				{
					//if this is an error other than NotFound then quit with an exception.
					if(response.StatusCode != HttpStatusCode.NotFound)throw(e);
					//this catches the last timeout (in this case 404)
					//if this is a NotFound AND i<1 (i.e. retries is less than 1) 
					//then quit with an exception
					if(i<1)
					{	
						System.Exception e1 = new Exception("Either Yahoo is not responding or '"+Symbol +"' not a valid ticker symbol");
						throw(e1);	
					}
					Thread.Sleep(2000); //give yahoo a chance to get things fixed
							

				}
			}
			else
			{
				//this hadles the case of when the yahoo data is "offline" for a while.
				//basically I ignore i WebExceptionStatus.Timeout's 
				if(e.Status!=WebExceptionStatus.Timeout) throw(e);
				if(i<1) throw(e);	//this catches the last timeout 
			}


		}


	}



	public class X
	{	
		public int totalgood = 0;
		public void Update()
		{
		
			YahooData yd = new YahooData();		
			bool good = false;
			DateTime LastDate = new DateTime(2005,1,15);
			Console.WriteLine("Total good should give you '9' at the end.");
			Console.WriteLine("Symbol ATH does not exist.");
			Console.WriteLine("ATH was added for to make the test realistic.");
			Console.WriteLine("-----------");

			//Go through each symbol and query yahoo for any days traded after the last day in the securities table
			string[] symbols = new string[10];
			symbols[0] = "Y";
			symbols[1] = "CAT";
			symbols[2] = "ITW";
			symbols[3] = "MKL";
			symbols[4] = "ATH";
			symbols[5] = "ALX";
			symbols[6] = "CME";
			symbols[7] = "SBZ";
			symbols[8] = "GOOG";
			symbols[9] = "MITSY";

			for(int s=0; s<10;s++)
			{   

				System.Collections.ArrayList al = new ArrayList();
				string symbol = symbols[s];		
		
				try  //query yahoo for data
				{
					good = true;
					Thread.Sleep(1000); //give yahoo a bandwidth break
					yd.GetHistoricalDays(symbol,LastDate, System.DateTime.Now.Date,ref al);
				}
				catch
				{
					//if something happens while getting data from yahoo then
					//mark it as a failed update in the db.
					good = false; 
				}

				if(good) totalgood++;
				Console.WriteLine(symbol + " - " + good + " - " + al.Count);				
			}
			Console.WriteLine("-----------");
			Console.WriteLine("Total Good: " + totalgood.ToString());
	       
		}
	}





	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{

			X x = new X();
			x.Update();
			//Console.WriteLine("Press Enter to quit");
			//Console.ReadLine();
			//
			// TODO: Add code to start application here
			//
		}
	}
}
------=_20050203152403_50348--