George V. Reilly

SerializationException: the constructor was not found

I just finished another post at the Cozi Tech Blog, Se­ri­al­iza­tionEx­cep­tion: the con­struc­tor was not found

Building a REST Web Service, day 1

My first project at Cozi is to build a simple REST-style Web Service. Nobody here has done that before.

The first thing that I'm trying to get going is a simple URL rewriter, using an ASP.NET HttpModule.

I'm running Vista as my de­vel­op­ment desktop for the first time. So far, not bad, but there are lots of new quirks to get used to. I've been a good boy so far and I've left the User Access Control stuff enabled, so that I'm not running with ad­min­is­tra­tive privileges by default.

It's my first exposure to IIS 7. I must say that the IIS UI is much improved (a low bar to continue.

Serializing a NameValueCollection

I had a NameVal­ueCol­lec­tion embedded inside a larger object. I needed to serialize the larger object into XML and back. Un­for­tu­nate­ly, NameVal­ueCol­lec­tion is not XML se­ri­al­iz­able. Why I do not know.

A blog comment from Tim Erwin got me started in the right direction. Implement IXmlSe­ri­al­iz­able and do the work by hand in ReadXml and WriteXml.

Tim's im­ple­men­ta­tion turned out to be overly simple. It didn't handle an empty collection well, nor did it leave the XmlReader in a good state.

I used SGen to examine the de­se­ri­al­iza­tion of a List to figure out what else needed to be done.

The following ReadXml seems to work. If I expected to receive XML from continue.

Multilingual

This week, I have written code in C#, C++, Managed C++, C, WiX, NAnt, Ac­tion­Script, VBScript, JScript, cmd batch, NMake, HTML, XSLT, and Ruby. And I will probably get some Python in before the weekend is over.

NVelocity: loading templates from embedded resources

In last week's tip on using the NVelocity template formatting engine, I described what to set to load a template from an absolute path.

Here's the magic necessary to get NVelocity to load a template from an embedded resource:

VelocityEngine engine = new VelocityEngine();
ExtendedProperties properties = new ExtendedProperties();
properties.AddProperty("resource.loader", "assembly");
properties.AddProperty("assembly.resource.loader.class",
    "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader, NVelocity");
properties.AddProperty("assembly.resource.loader.assembly", "StencilFormatter");
engine.Init(properties);

NVelocity templates and absolute paths

We've started using the NVelocity template formatting engine. We were absolutely stymied for an hour, trying to figure out how to get it working with an absolute path to the template file, instead of the relative path shown in the doc­u­men­ta­tion.

The trick is to set file.resource.loader.path. Here's how to load C:\foo\bar\­some­file.vm:

ExtendedProperties props = new ExtendedProperties();
props.AddProperty("file.resource.loader.path", new ArrayList(new string[]{".", "C:\\"}));
velocity.Init(props);

template = velocity.GetTemplate("foo\\bar\\somefile.vm");

Printf %n

In my post about Printf Tricks a couple of years ago, I mentioned that "%n is dangerous and disabled by default in Visual Studio 2005."

I got email today from someone who was porting a large codebase to VS 2005. He was getting an assert from %n and he needed a way to get past it. He intends to fix the uses of %n when he has a chance.

I spent several minutes digging around in MSDN and came up with set_print­f_­coun­t_out­put. Wikipedi­a's Format string attack page led me to Exploiting Format String Vul­ner­a­bil­i­ties, which describes in detail how %n (and %s) may be exploited.

In short, if you continue.

Never Sleep(0) in an Infinite Loop

I ran into a problem installing some COM+ components today. The installer was using Regsvcs.exe to register each COM+ component. I noticed after a while that the installer wasn't making any progress and that my dual-proc system was stuck at 50% CPU uti­liza­tion. I attached a debugger to the offending process, regsvcs, and found that it was stuck in the following infinite loop (dis­as­sem­bly courtesy of Reflector):

internal void System.EnterpriseServices.CatalogSync.Wait()
{
  if (this._set)
  {
    RegistryKey key1
      = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\CLSID");
    while (true)
    {
      int num1 = (int) key1.GetValue("CLBVersion", 0);
 
continue.

Printf Tricks

It may be old-fashioned, but I still find printf (and sprintf and _vsnprintf) incredibly useful, both for printing debug output and for generating formatted strings.

Here are a few lesser-known formats that I use again and again. See MSDN for the full reference.

%04x - 4-digit hex number with leading zeroes

A quick review of some of the basics.

%x prints an int in hexa­dec­i­mal.

%4x prints a hex int, right-justified to 4 places. If it's less than 4 digits, it's preceded by spaces. If it's more than 4 digits, you get the full number.

%04x prints a hex int, right-justified to 4 places. If it's less than 4 digits, it's preceded continue.