Archive for the ‘Code Samples’ Category

How to access Session from an IHttpHandler

I’ve gotten a lot of use during my time coding with ASP.NET out of inheriting from IHttpHandler. I usually do this to handle dynamically-generated files in various formats (PDF, XLSX, and images to name a few). One thing that always irked me was the inability to access the HttpContext’s SessionState object to get things like connection strings and information about the logged-in user more easily to aid in generating my files; often I would end up encrypting those and cramming them into the query-string, which can present challenges such as replay attacks that I’d then have to find a way to prevent.

Getting started with Amazon AWS via C#

A while back I was putting together a website that, among other things, relied on being able to retrieve products from Amazon’s Product Advertising API. There isn’t a ton of documentation out there on how to do this, and it took me a long time to find information on how to do this in my language of choice, C#. Unfortunately, there isn’t anything as cool as the Zend Framework for .NET.

Connection Timeout Using LINQ DataContext

For a while now, I’ve had a website that was sporadically encountering the following error:

Serializing Data with System.Runtime.Serialization.DataContractSerializer

Last time we discussed Serializing Data with System.Xml.Serialization.XmlSerializer. We will be using the same sample solution for this article.

Serializing Data with System.Xml.Serialization.XmlSerializer

Serializing objects is often a useful way to save the state of an application. Let’s take a look at doing this using the XmlSerializer object.

Version Checking with System.Version

The .Net Framework makes version checking extremely simple. The framework provides a Version class that implements IComparable. Comparing two versions is as easy as the code below:

C# Method to Add an Image to a PDF

I had to write a method to put images onto PDFs generically, so I wrote this handy little method. Just get your PDF and your image into byte arrays and fire the method below. I used PDFSharp to append the image. This method could be used to do things like build a letterhead generator or any other kind of PDF document generation.

Code Sample: ASP.Net Message Control

Relaying information snippets and error messages is one of the most common tasks between web application developers. The most basic control this can be done with is the Literal. That’s great for an internal application or a school project, but in the real world information has to be both visually appealing and convey what the message is about using an icon or other form of illustration.