Seattle .NET User Group - July 8th 2009 Meeting - ASP.NET MVC Framework by Brad Wilson

Topic:  ASP.NET MVC.

is a new web development framework from the ASP.NET team at Microsoft. In this talk, Brad Wilson will discuss what the Model-View-Controller pattern is, why the team decided to create a new framework, how it compares to ASP.NET WebForms, some of its primary benefits, and of course lots of time in Visual Studio walking through the uses of MVC.

Speaker: Brad Wilson

Brad Wilson is a senior software developer at Microsoft on the ASP.NET team with more than 15 years of experience writing software for everything from networking stacks to high-speed measuring machines, from startups to large enterprises, and most everything in-between. He is currently working on the MVC team helping to define and deliver the next version of ASP.NET MVC. He is co-creator of the unit testing framework.

Wednesday, July 8th, 2009
5:45 – 6:15 PM – Mixer, group kickoff and speaker introduction
6:15 – 7:45 PM – Presentation
7:45 – 8:00 PM – Spillover time and raffle


Starbucks Support Center
2401 Utah Ave S.
Seattle, WA 98134


For more information visit

The next Seattle.Net User Group meeting June 10th, 2009

 for more information visit website:

Topic:  Windows Azure.

In this session, David Lemphers, Senior Program Manager for Windows Azure, will provide an overview of Windows Azure, including how-to code demos on building cloud based applications using Windows Azure and Visual Studio.

Speaker: Dave Lemphers
David Lemphers is a Windows Azure program manager based in Redmond. Dave spends most of his time working on features and projects for Windows Azure, but also enjoys blogging and building robots in his spare time.
Originally from Australia, Dave spends his free time making vegemite sandwiches and eating meat pies and lamingtons at Cafe 41!

Wednesday, June 10th, 2009
5:45 – 6:15 PM – Mixer, group kickoff and speaker introduction
6:15 – 7:45 PM – Presentation
7:45 – 8:00 PM – Spillover time and raffle


Starbucks Support Center
2401 Utah Ave S.
Seattle, WA 98134
See Map

Our meeting is open to everyone so bring your friends and co-workers. 
If you’re planning to come, please RSVP as soon as possible via email or at the Facebook Group.

Barcamp Seattle June 13th - June 14th 2009

BarCampSeattle is an ad-hoc gathering born from the desire for people to share and learn in an open environment. It is an intense event with discussions, demos, and interaction from attendees. It is an international network of user generated non-traditional social conferences: open, participatory workshop-events, whose content is provided by participants, often focusing on early-stage web applications, and related open source technologies, social protocols, and open data formats. Oh and super fun!!

I will be going to this on Saturday to see how this type of event works. I always enjoy going to these types of things just for the networking opportunities.

To register and see more details visit:

Compress and Decompress using .net framework and built in GZipStream

I recently had a project in which I wanted to compress log files I was transferring between servers. I did not realize till I did some research that the .NET framework has a nice little library built in for creating GZIP files. While I think the maximum recommended size for using this is 4GIG I am well under that.

Here is my Compress / Decompress methods

byte[] startfile = File.ReadAllBytes("e:\\mylog.log");
byte[] comp = null;
byte[] decom = null;


comp = CompressBytes(startfile);

decom = Decompress(comp);


private void Compress(byte[] fileBytes)
using(MemoryStream ms = new MemoryStream())
using(GZipStream gz = new GZipStream(ms,
gz.Write(fileBytes, 0, fileBytes.Length);


private byte[] Decompress(byte[] fileBytes)
int buffer_size = 100;

using(MemoryStream ms = new MemoryStream(fileBytes))
using(GZipStream gz = new GZipStream(ms,
byte[] bufferFooter = null;
int readOffset = 0;
int totalBytes = 0;
byte[] finalBuffer = null;
int uncompLength = 0;
int compressedFileLength = fileBytes.Length;

// Get last 4 bytes (footer) as they contain the
// original length of the compressed bytes

// byte array to hold footer value
bufferFooter = new byte[4];

// Set position of MemoryStream end of stream
// minus the 4 bytes needed
ms.Position = ms.Length - 4;

// Fill the bufferFooter with the last 4 bytes
ms.Read(bufferFooter, 0, 4);

// Set Stream back to 0
ms.Position = 0;

// Convert footer bytes to the length.
uncompLength = BitConverter.ToInt32(bufferFooter, 0);

// Set a temporary buffer to hold the uncompressed
// information. We also make it slightly larger to
// ensure everything will fit. We will later trim
// off the unused bytes.
finalBuffer = new byte[uncompLength + buffer_size];

// Read from stream up to buffer size. Note that return
// value is actual bytes read in case the number filled
// is less than we requested.
int bytesRead = gz.Read(finalBuffer,

// If no bytes returned we are done
if(bytesRead == 0)

readOffset += bytesRead;
totalBytes += bytesRead;

// Trim off unused bytes based
Array.Resize<byte>(ref finalBuffer, totalBytes);

return finalBuffer;

Calling ASMX .net Web Service using jQuery

Just started to pick up jquery recently and was playing with calling a .net web service from my page. Was really easy once I used Firefox Firebug to do my debugging and figure out some of the variable names to use to access my data.

Here is my final code

Javascript in page

<script type="text/javascript">
$(document).ready(function() {
type: "POST",
url: "default.asmx/GetCatalog",
cache: false,
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: handleSuccess,
error: handleError

function handleSuccess(data, status) {
for (var count in data.d) {
' <strong>Book:</strong> ' + data.d[count].BookName +
' <strong>Author:</strong> ' + data.d[count].Author +
                ' <br />');


function handleError(xmlRequest) {
alert(xmlRequest.status + ' \n\r '
+ xmlRequest.statusText + '\n\r'
+ xmlRequest.responseText);

and the div that I write the content to

<b>Books List</b>
<div id="bookTitles">

and my Web Service and Class Catalog

public Catalog[] GetCatalog()
Catalog[] catalog = new Catalog[1];
Catalog cat = new Catalog();
cat.Author = "Jim";
cat.BookName ="His Book";
catalog.SetValue(cat, 0);
return catalog;


public class Catalog
public string Author;
public string BookName;

Replacing text in a stream after writing

Recently I responded to a question asked on StackOverflow. The question was "What is the BEST way to replace text in a File using C# / .NET?"

Just by the question you might assume they were wanting to simply replace text in an existing file and of course you could do that by opening the file and looping over it or doing a Replace of content.

Here is the requirements they gave.

Have a text file that is being written to as part of a very large data extract. The first line of the text file is the number of "accounts" extracted.

Because of the nature of this extract, that number is not known until the very end of the process, but the file can be large (a few hundred megs).

What is the BEST way in C# / .NET to open a file (in this case a simple text file), and replace the data that is in the first "line" of text?

So as you can see they are in the process of writing the file, at the end of the write of the file they now know the information they wish to write at the top of the file. So how might we able to go back and write that information without having to open the file again and write the information at the top of the file?

It turns out it is actually fairly simple. One we want to make sure memory utilization is a factor since these files can be very large we probably do not want to open the whole file and prepend text, or do some kind of search and replace on text content that size. So my suggestion is using a stream. Also by using a Stream we can avoid entirely writing the file, then having to open the file again and write the text. StreamWriter has a BaseStream property that provides you access to the base stream object which then allows you to set the position of the stream back to the top and write out the information.

Here is my answer.

private void WriteUsers()
    string userCountString = null;
    ASCIIEncoding enc = new ASCIIEncoding();
    byte[] userCountBytes = null;
    int userCounter = 0;
    using(StreamWriter sw = File.CreateText("myfile.txt"))
        // Write a blank line and return
        // Note this line will later contain our user count.
        // Write out the records and keep track of the count 
        for(int i = 1; i < 100; i++)
            sw.WriteLine("User" + i);
        // Get the base stream and set the position to 0
        sw.BaseStream.Position = 0;
        userCountString = "User Count: " + userCounter;
        userCountBytes = enc.GetBytes(userCountString);
        sw.BaseStream.Write(userCountBytes, 0, userCountBytes.Length);


Seattle .net dotnet user group

Great new user group that is getting started and it is in Seattle. Was hosted at Starbucks who provided a top notch facility for the meeting. About 25 people showed up and  speaker Charles Sterling  gave us a great presentation walking us through the new testing tools in Microsoft Visual Studio Team Systems 2010. 

Highly recommend this to anyone involved in .NET development and located in or around Seattle.

They also have a facebook group

How do I open a file that is in use in C#?

I had a situation recently in which I needed to display in my application the log file that was being generated by the application which was using log4net. Any time I tried to open the log file I would get an exception because the file was in use. I searched google for the answer as I normally do and found many others trying to find the same exact answer. Some came up with ideas like copying the original file to another temporary file and opening it. Others seemed to reach a dead end. However I soon figured out what my issue was.

When reading my file using StreamReader it appears that while I assumed since I was reading the file it should not be an issue if it was in use since I was only reading it. Well that is were I was making my wrong assumption. It appears that the StreamReader object opens the file and tries to set an exclusive lock on the file while reading the contents. Since in my case another program already has an exclusive lock it would fail.

So I started to look at how I could open the file in truly a read only mode. While I found people talking again about opening a file in use for readonly access it was for some reason not clear to me what the exact solution was.

So what is the solution? System.IO.FileShare

By creating a FileStream object and setting FileShare.ReadWrite I am saying that I want to open the file in a mode that allows other files to Read and Write to/from the file while I have it opened.

Here is an example code snipet of how I read my log file that was currently in use by the logging object log4net. Hope this helps someone else.

private void LoadFile()
        using(FileStream fileStream = new FileStream(
            using(StreamReader streamReader = new StreamReader(fileStream))
                this.textBoxLogs.Text = streamReader.ReadToEnd();
    catch(Exception ex)
        MessageBox.Show("Error loading log file: " + ex.Message);

kick it on

Microsoft patterns & practices Application Architecture Guide 2.0

Was listening to a podcast the other day in which one of the authors of the Microsoft patterns & practices Application Architecture Guide was speaking about this latest version so I decide to take a look. So far I am very impressed as the information seems to do a fairly good job of coupling desgin guidlines for applications with some of the latest technologies.

The guide breaks architecture down into 4 parts.

Part I - Fundamentals

  • Fundamentals of Application Architecture
  • .NET Platform Overview
  • Architecture and Design Guidlines

Part II - Desgin

  • Designing Your Architecture
  • Deployment Patterns
  • Architecture Styles
  • Quality Attributes
  • Communication Guidelines

Part III - Layers

  • Layers and Tiers
  • Presentation Layer Guidelines
  • Business Layer Guidelines
  • Data Access Layer Guidelines
  • Service Layer Guidelines

Part IV - Achetypes

  • Application Archetypes
  • Web Applications
  • Rich Internet Applications (RIA)
  • Services
  • Mobile Applications
  • Office Business Applications (OBA)
  • SharePoint LOB Applications

Also the Appendix offers some great advice using a Matrix apporoach to various technologies.

Data Technology Matrix - Provides matrix of old and new technology options such as ADO.NET, Entity Framework, LINQ to SQL and more.

Check out the website on codeplex

Also here is a link to pocket guides based on the Application Architecture Guide but geared towards Web Architecture, Mobile Architecture, RIA Architecture, Rich Client Architecture, Service Architecture.

Check out pocket guides on codeplex