JavaScript appears to be disabled. We recommend you enable JavaScript while visiting this site.

(All original content on this site is licensed under the Creative Commons License Attribution-Noncommercial-No Derivative Works 3.0.)

Windows 7 Home Premium changes for IIS 7.5 access of Users directories

Not feeling much like debugging it too much, I made a couple of modifications to my IIS installation to allow access to files contained within my user directory.

Background

I turned the Internet Information Services feature on on my Windows 7 Home Premium machine before using the Web Platform Installer to add additional functionality. However, I kept running into a permissions issue, as the sites I setup were located in my Users directory (C:\Users\James).

Possible options

A couple of possible options exist, including giving the user that the application pool runs as full access to the appropriate directory. However, to some extent this circumvents security on my laptop.

Another option is to use Windows authentication. To this end it needs to be enabled by checking the feature of Internet Information Services > World Wide Web Services > Security > Basic Authentication.

Additional setup and result

Additionally default rights must be given to the local IIS_IUSRS group, so that it can access the Web.config file.

Now when I browse to the site I'm prompted for credentials, which is the same information I use to login to my machine.

Putting the content into a directory with the appropriate permissions (%SystemDrive%\inetpub\wwwroot) may be better, but this works just fine for the limited testing I need to perform.

Tags: ,

Categories: tutorials/guides

(All original content on this site is licensed under the Creative Commons License Attribution-Noncommercial-No Derivative Works 3.0.)

Visual Studio 2010 complaints about ELMAH on a 64-bit machine - fixed

My laptop has quickly become my development machine for smaller projects, especially after I purchased a low-end desktop to host the Subversion repositories.

I recently added ELMAH support to one of my sites, but since I run 64-bit, and have been using the built-in Cassini for quick development, I started getting a message in Visual Studio 2010 saying "ASP.NET runtime error: Could not load file or assembly 'System.Data.SQLite' or one of its dependencies. An attempt was made to load a program with an incorrect format."

While my low-end desktop has Server 2008 R2 installed on it for research and development purposes (thank you MSDN subscription), having a laptop means I won't always have this resource available. So, since Stack Overflow states that this is an issue with Cassini, and I'm running Windows 7 Home Premium, it was time to install IIS 7 on the machine. After a minimal click in the Control Panel and with the hep of Microsoft Web Platform Installer, my IIS instance was setup correctly.

Unfortunately, that requires that Visual Studio 2010 run as administrator (I was afraid I was going to have to update each of my pinned solutions; thankfully you need to (and can only) update this on the main application shortcut), and my MVC 2 project was failing horribly.

Instead of trying to debug this further I kept searching and found another post on Stack Overflow. After updating my 'shared' directory to contain a folder with all of the ELMAH files and another with just the necessary ones, I'm happy to report the errors (warnings) are gone.

My one site is still a mash of the old and new, so I had to manually remove the items from the Bin directory to verify, but things seem just fine.

Tags: , ,

Categories: tutorials/guides

(All original content on this site is licensed under the Creative Commons License Attribution-Noncommercial-No Derivative Works 3.0.)

ASP.NET charts example: Odin Sphere: Part 3 - Creating the chart

In part one of this series we covered what we'd be doing, and what data model we'd be using.

In part two of this series we used LINQ to XML to query the XML file with the data we want to display.

This time we'll be doing the heavy lifting of actually creating the chart and displaying it to the user. For ease, I'll be implementing very basic caching.

Preliminary requirement

Before you can use the charting functionality you need to have a reference to System.Web.DataVisualization. We can then use this in our handler as below.

using System.Web.UI.DataVisualization.Charting;

Next we can do the heavy lifting of creating the basics of the chart:

// Create a new chart, and set the basic properties of it.
Chart hpChart = new Chart();
hpChart.Width = 800;
hpChart.Height = 500;
hpChart.Titles.Add("Odin Sphere HP leveling");
hpChart.Palette = ChartColorPalette.Bright;
hpChart.Legends.Add("Main");
hpChart.Legends[0].LegendStyle = LegendStyle.Row;
hpChart.Legends[0].Docking = Docking.Bottom;
// Create a new area for the main chart to display within.
ChartArea mainArea = new ChartArea("Main chart");
// Set the properties for the x-axis.
mainArea.AxisX.Name = "Level";
mainArea.AxisX.Title = "Level";
mainArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.DimGray;
mainArea.AxisX.MinorGrid.Enabled = true;
mainArea.AxisX.MinorGrid.LineColor = System.Drawing.Color.LightGray;
// Set the properties for the y-axis.
mainArea.AxisY.Name = "Hit points";
mainArea.AxisY.Title = "Hit points";
mainArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.DimGray;
mainArea.AxisY.MinorGrid.Enabled = true;
mainArea.AxisY.MinorGrid.LineColor = System.Drawing.Color.LightGray;
mainArea.AxisY.MinorGrid.Interval = 50;
hpChart.ChartAreas.Add(mainArea);

With our chart created we can now add our data.

foreach (Character character in characterData) {
	// Add a new series of points for each character.
	Series characterSeries = new Series();
	characterSeries.Name = character.Name;
	characterSeries.ChartType = SeriesChartType.Line;
	foreach (HpLevel characterLevel in character.HpLevels) {
		// Add a point for each level recorded.
		characterSeries.Points.AddXY(characterLevel.Level, characterLevel.HitPoints);
	}
	hpChart.Series.Add(characterSeries);
}

Since we want to cache the chart, we'll add an informational message.

// Add a new informational title.
Title cacheTitle = new Title("Cached " + DateTime.Now.ToString() + " and based on http://jamesrskemp.com/files/OdinSphere.xml");
cacheTitle.Docking = Docking.Bottom;
hpChart.Titles.Add(cacheTitle);

Next we'll set the rendering type of the chart and add it to the cache.

hpChart.RenderType = RenderType.BinaryStreaming;
// Cache our object for an amount of time
HttpRuntime.Cache.Add("OdinSphereChart", hpChart, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Low, null);

Everything above, as well as the XDocument load from part two, can be wrapped by a check for whether the chart is in the cache.

// Determine whether the chart is already cached.
if (HttpRuntime.Cache["OdinSphereChart"] == null) {
//...
}

And then we can finally output the chart to the user.

// Output the cached chart to the browser.
using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) {
	((Chart)HttpRuntime.Cache["OdinSphereChart"]).SaveImage(stream);
	context.Response.ContentType = "image/png";
	context.Response.BinaryWrite(stream.GetBuffer());
}

Final code

At the end of our exercise, our handler (OdinSphere.ashx) looks something like the following:

<%@ WebHandler Language="C#" Class="OdinSphere" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Web.UI.DataVisualization.Charting;

public class OdinSphere : IHttpHandler {

	/// <summary>
	/// One of the five playable characters in Odin Sphere, for the Playstation 2.
	/// </summary>
	public class Character {
		/// <summary>
		/// Name of the character.
		/// </summary>
		public String Name { get; set; }
		/// <summary>
		/// List of hit point leveling information.
		/// </summary>
		public List<HpLevel> HpLevels { get; set; }
	}

	/// <summary>
	/// Hit point information at a particular level.
	/// </summary>
	public class HpLevel {
		/// <summary>
		/// Level of the character.
		/// </summary>
		public int Level { get; set; }
		/// <summary>
		/// Hit points at a level, for a character.
		/// </summary>
		public int HitPoints { get; set; }
	}

	public void ProcessRequest(HttpContext context) {
		// Determine whether the chart is already cached.
		if (HttpRuntime.Cache["OdinSphereChart"] == null) {
			// Grab the current data.
			XDocument dataFile = XDocument.Load("http://jamesrskemp.com/files/OdinSphere.xml");
			IEnumerable<Character> characterData = from characters in dataFile.Descendants("Character")
												   select new Character {
													   Name = characters.Attribute("name").Value,
													   HpLevels = (from levels in characters.Element("HP").Element("Levels").Descendants("Level")
																   select new HpLevel {
																	   Level = int.Parse(levels.Attribute("id").Value),
																	   HitPoints = int.Parse(levels.Attribute("hitPoints").Value)
																   }
													   ).ToList()
												   };

			// Create a new chart, and set the basic properties of it.
			Chart hpChart = new Chart();
			hpChart.Width = 800;
			hpChart.Height = 500;
			hpChart.Titles.Add("Odin Sphere HP leveling");
			hpChart.Palette = ChartColorPalette.Bright;
			hpChart.Legends.Add("Main");
			hpChart.Legends[0].LegendStyle = LegendStyle.Row;
			hpChart.Legends[0].Docking = Docking.Bottom;
			// Create a new area for the main chart to display within.
			ChartArea mainArea = new ChartArea("Main chart");
			// Set the properties for the x-axis.
			mainArea.AxisX.Name = "Level";
			mainArea.AxisX.Title = "Level";
			mainArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.DimGray;
			mainArea.AxisX.MinorGrid.Enabled = true;
			mainArea.AxisX.MinorGrid.LineColor = System.Drawing.Color.LightGray;
			// Set the properties for the y-axis.
			mainArea.AxisY.Name = "Hit points";
			mainArea.AxisY.Title = "Hit points";
			mainArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.DimGray;
			mainArea.AxisY.MinorGrid.Enabled = true;
			mainArea.AxisY.MinorGrid.LineColor = System.Drawing.Color.LightGray;
			mainArea.AxisY.MinorGrid.Interval = 50;
			hpChart.ChartAreas.Add(mainArea);

			foreach (Character character in characterData) {
				// Add a new series of points for each character.
				Series characterSeries = new Series();
				characterSeries.Name = character.Name;
				characterSeries.ChartType = SeriesChartType.Line;
				foreach (HpLevel characterLevel in character.HpLevels) {
					// Add a point for each level recorded.
					characterSeries.Points.AddXY(characterLevel.Level, characterLevel.HitPoints);
				}
				hpChart.Series.Add(characterSeries);
			}

			// Add a new informational title.
			Title cacheTitle = new Title("Cached " + DateTime.Now.ToString() + " and based on http://jamesrskemp.com/files/OdinSphere.xml");
			cacheTitle.Docking = Docking.Bottom;
			hpChart.Titles.Add(cacheTitle);
			
			hpChart.RenderType = RenderType.BinaryStreaming;
			// Cache our object for an amount of time
			HttpRuntime.Cache.Add("OdinSphereChart", hpChart, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Low, null);
		}

		// Output the cached chart to the browser.
		using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) {
			((Chart)HttpRuntime.Cache["OdinSphereChart"]).SaveImage(stream);
			context.Response.ContentType = "image/png";
			context.Response.BinaryWrite(stream.GetBuffer());
		}
	}
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

You can see this in action online.

Tags: , , ,

Categories: tutorials/guides