Tuesday, May 01, 2007

Today was not a fun day.  Among the many tasks that I had to accomplish my primary development virtual machine decided to barf on me.  The system would boot but then the moment I attempted to run VS and load a project it would 'blink' off and reboot.  It did this twice and then decided to run a CHKDSK at the third boot.  This found several invalid file chains and ended up 'restoring' several files.

At this point, the OS wouldn't even boot, saying that the C:\WINDOWS\SYSTEM32\CONFIG directory was missing.  Great!  My next step was to run a repair from the Windows XP boot disk.  This ran fine (for a while) until it eventually crashed with an invalid memory address read.

The rest of my day was spent attempting to get my development files off of the VPC so I could move over (a.k.a., rebuild) another.  That, in and of itself, isn't a big deal - I have several base images to work with so I'm not worried.  Getting the files off of a VPC image turned out to be non-trivial.  There may be better ways to do this (and I'm open to thoughts), but here's what I did:

  1. Create a secondary, blank VHD (virtual hard drive).  I had to do this because VPC wouldn't let me mount a non-CD/DVD/ISO drive within the image and I couldn't share a folder between the VM and the host machine.
  2. Boot the VPC to the command prompt (I used an old Win98 disk cause it was laying around).
  3. FDISK to create a partition on the VHD.
  4. Boot to the XP Recovery Console (via the XP boot disk) - a step which we'll soon see was a waste of time.
  5. FORMAT D:\ /FS:NTFS
  6. At this point, I wanted to copy my ~\dev directory to the new VHD.  Well, as it turns out the XP Recovery Console doesn't allow XCOPY (even when referenced directly in the c:\windows\system32 folder) !! Even then, COPY doesn't allow wildcards nor directory copying !!  This completely, utterly baffles me.
  7. I then remembered my best friend in boot-time travails: Windows PE (Pre-installation Environment) Server Edition: boot to WinPE.
  8. Once I booted up to PE it was a cakewalk.  It's a good thing too, because manually copying 2490 files manually (one by one due to lack of wildcards in NTRC) would have been a nightmare.

Well, now I'm back up and running, but what a nightmare.

Tuesday, May 01, 2007 10:33:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, April 26, 2007

The other day I posed a question regarding the processing of data from a web service where the structure (i.e., the names and quantities of the fields) varies.  The fictitious example had us suppose that we wrote a component designed to plug into and consume the services of an application that may be installed across a wide customer base where each customer might customize (albeit indirectly) the results of the web service.  The web service, of course, would purely be an abstraction of the customer's customizations and business model.  For instance, a customer may decide to add a special/custom property to an 'Account' within the system that helps him track some aspect of the Account that isn't out-of-the-box.  This may, as a byproduct, affect the structure of the web service such that the new field is now present.

Part of our solution is to be accepting of such a change in structure in a manner that crosses any and all customer needs.  One customer may customize the heck out of an entity (such as Account) while another may make just a few changes.  Others will leave it alone entirely.  Our product must be able to operate on and affect any and all fields (including custom ones).

We discussed that there are some considerations to make in order to support this environment.

The question posed, then, was "How would you approach this problem?"  How would you design a system whereby you can consume all of the attributes of the entity (e.g., Account) across the board?

From within Visual Studio you can "Add Web Reference".  From the command-line (and my preferred mechanism) you use WSDL.exe.  Regardless of route you take (heck, you can even do it manually - I've done that in the past too), you'll end up with a proxy class with method calls to the web service and classes representing the various structured types that the web service exposes.

This isn't enough when you want to support a structure that will vary from implementation to implementation; the service won't have any inkling of any of the custom fields.

Alright, enough setup.  How would you solve this potential issue?

There are a couple of approaches worth investigating, each with their own varying degrees of complexity and control.  And probably many more that are eluding me and are simpler still.

First of all you can rewrite all of the traffic to/from the web service via a SoapExtension.  This approach may ultimately yield the greatest flexibility, but at the expense of a lot of work.  Essentially, with a SoapExtension you can inject your code into the web service pipeline and read, alter, or even rewrite completely all of the traffic to and from the web service before it is ultimately returned to you (or delegated on to another SoapExtension in the chain).  In fact, when I was first experimenting with this issue of dynamically structured data this was my first approach.  I have the history documented here if you're interested in reading more about it.

A second approach takes a different angle on this idea.  It involves defining a "bucket" into which all fields not formally defined within your proxy class are grouped.  To achieve this we must alter the generated proxy class by adding a custom field (note, don't re-gen the proxy class once you do this or you'll lose your changes).

Recall the original example:

[XmlType(Namespace="http://schemas.devstone.com/svc")]
public abstract class customer : BusinessObj {
   public string id;
   public string name;
}

Let's add our "bucket" and we'll call it "custom":

[XmlType(Namespace="http://schemas.devstone.com/svc")]
public abstract class customer : BusinessObj {
   public string id;
   public string name;
   [XmlAnyElement()] public XmlElement[] custom;
}

This very small change is what works the magic for us.  The XmlAnyElementAttribute identifies that the "custom" member will represent any XML elements within the results from the web service that don't have a corresponding member in the object.  That's pretty cool.

Ok, this is only part of the problem.  This gives us the ability reference potentially anything that the web service may throw our way, but how do we make use of this in our application?

I like to define a wrapper class for the object that encapsulates the business object:

public sealed class DynamicObj {
   public DynamicObj(BusinessObj busObj) {
      _busObj = busObj;
      _busType = busObj.GetType();  // cache the type for repetitive use
   }

   BusinessObj _busObj;
   Type _busType;

   // expose the business object directly so we can continue to interact with the web service with
   // the actual object (not the wrapped one)

   public BusinessObj Object {
      get { return _busObj; }
   }

   // provide a indexer mechanism to read/write field values
   public object this[string fieldName] {
      get { return getFieldValue(fieldName); }
      set { /* I'll leave this as an exercise for you, the reader - it's not difficult at all */ }
   }

   // returns the value associated with the specified field name
   public object getFieldValue(string fieldName) {
      FieldInfo field = _busType.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance);
      if ( null != field )
         return field.GetValue(_busObj);
      else {
         XmlElement customField = getCustomField(fieldName);
         return ( null != customField ? customField.InnerText : null );
      }
   }

   // returns simply the number of fields defined by the business object
   public int FieldCount {
      get {
         FieldInfo[] fields = getDefinedFields();
         XmlElement[] elems = getCustomFields();
         return fields.Length + elems.Length - 1;  // subtract 1 to account for the presence of the 'custom' field
      }
   }

   // returns an array consisting of all of the fields within the business object
   // NOTE: depending on usage, we might want to cache the result of this method so
   // we're not constantly regenerating it.
   public string[] GetFieldNames() {
      string[] ret = new string[FieldCount];
      int i = 0;
      foreach ( FieldInfo field in getDefinedFields() )
         if ( "custom" != field.Name ) ret[i++] = field.Name;
      foreach ( XmlElement elem in getCustomFields() )
         ret[i++] = elem.Name;
      return ret;
   }

   // returns an array of all fields formally defined on the business object
   private FieldInfo[] getDefinedFields() {
      return _busType.GetFields(BindingFlags.Public | BindingFlags.Instance);
   }

   // returns a list of all fields found within the 'custom' field.
   private XmlElement[] getCustomFields() {
      FieldInfo customField = _busType.GetField("custom", BindingFlags.Public | BindingFlags.Instance);
      ifnull == customField )
         throw new NotImplementedException("The BusinessObj does not support the 'custom' field.");
      else
         return customField.GetValue(_busObj) as XmlElement[];
   }

   private XmlElement getCustomField(string fieldName) {
      foreach ( XmlElement elem in getCustomFields() ) {
         if ( fieldName == elem.Name )
            return elem;
      }
      return null;
   }
}

As you can see, it's pretty simple and removes a lot of the complexity of dealing with an object of whose structure you're not aware.  There is a whole lot more you can do with this object than I'm letting on, but it may be a starting point.  There are a few issues that this approach does not address (such as the notion of 'type').  For example, there's no way of knowing whether you should be dealing with an int, a string, a DateTime, etc - but I'll leave that implementation up to you.

What do you think? Plausable? Idiotic? Cool?

I'd like your feedback and input.

Thursday, April 26, 2007 4:12:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Tuesday, April 24, 2007

With .NET, consuming web services is a snap.  Heck, it's not all that difficult without .NET, but it sure makes it even easier.  In general, and for simple cases, all you need to do is add a web reference to the target .asmx, have the proxy generated (via VS.NET or wsdl.exe), and you're off to the races.

Some additional care and consideration should be taken, however, when the data returned from the web service can vary in format and structure.  Allow me illustrate this with an example:

Suppose you are developing an application that must consume some data from a web service.  This web service returns structured data that, via the auto-generated proxy, is represented in a class.  For argument sake, this class is Customer (which derives from BusinessObj) and it has a few properties as identified below:

[XmlInclude(typeof(customer))]
public abstract class BusinessObj { }

[XmlType(Namespace="http://schemas.devstone.com/svc")]
public abstract class customer : BusinessObj {
   public string id;
   public string name;
}

Furthermore, this web service is part of a larger application that a company will purchase and deploy internally.  In fact the customer may customize each type of data (e.g., Customer) by adding custom fields and attributes.  Your component is designed as a 3rd-party add-in for this application and may be deployed across this varied environment.

Some customers will leave the base application alone (not customizing it) where other will 'go-to-town' and change the schema entirely to fit their business needs.

The question is this: how can your application operate in such a varied environment, consume the web service, and process all of the customizations that the customer has implemented?

You cannot, for instance simply create your web service proxy against the default schema and expect that to work.  The reason being that once you deploy your component into a customized environment, your proxy will not know about the custom fields and cannot serialize/deserialize them.  You won't get an error, per sé, but any custom fields will be ignored and not made available to your application.

This could be disastrous in the event that you needed to populate or read the custom fields.

How would you approach this problem?

NOTE: I'll post my answer tomorrow (or perhaps tonight) of how I've implemented it in the past to great success.

Tuesday, April 24, 2007 3:50:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [2]  |  Trackback
 Monday, April 16, 2007

Phew! What a crazy, fun weekend!  I'm finally getting caught up.

This past weekend (April 14th, 2007) we had the opportunity to host and participate in the second Code Camp in Utah officially entitled “Utah Spring Code Camp 2007”.  Our goal is to put these on every six months, the next one sometime between Sept and Nov of this year.

Nonetheless, this event was a lot of fun.  There were many in attendance, though not quite as many as we were hoping for.

I had the opportunity speak on .NET Serialization.  It was a variation on a presentation that I had prepared a few months back for the Utah .NET User Group and this time, after rewriting the presentation and code samples from scratch it went MUCH better.  I am quite happy with how it came out (I hope those in attendance gleaned some nuggets of information).

As it turns out one presenter was not able to make it out to the Code Camp (calling in around 10:30 or 11:00 in the morning) so we had to scramble a bit.  I had some ideas up my sleeve based on what I had trimmed from my presentation and we focused the second hour on XML Serialization.  That was a lot of fun! :-)

The third block was a shortened (30 min) presentation in which I discussed an evolution in accessing application configuration files (e.g., web.config, app.exe.config, et al).  Beginning with the most basic access method via AppSettings, we progressively made the transition to ConfigurationSectionHandlers and XML Serialization.  We used this technique to deserialize a block of XML from the configuration file into an object in code that we can access.

All in all, I'm quite happy with the Code Camp, though I wish I could have attended more of the presentations.  I guess that's a drawback from having multiple tracks running simultaneously - you have to pick and choose what you're gonna see.

The code for my presentations can be downloaded here.  Enjoy!

Monday, April 16, 2007 4:00:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [2]  |  Trackback
 Tuesday, April 10, 2007

Today, April 10th, 2007, marks the three year anniversary of my blog.  What an amazing three years!  This marks the 504th post on this here blog.

Looking back over the past year I'm pretty happy with the many things that have been accomplished and the progress that I've made, both as a developer but also as a person in general.  Among other things, I've strived (more of late than usual) to not focus solely on computers, as it were, and more on life in general.  You could say I've ramped up other aspects of my life because I recognize that there's more to life than just sitting behind this monitor and keyboard.

For instance, I've taken up construction and woodworking (long time loves of mine to which I've not dedicated as much time as I would have liked before).  As soon as I clean out my garage I can really go to town. :-)

Also, looking back on my blog, I've made some changes periodically and sporadically.  I've recently added Google Ads and a Google Search.  I hope those aren't visual obtrusive and annoying.  It's been interesting to see how many ad clicks are made and how many searches are performed daily on the blog.

You may have also noticed my download and rating controls.  I get notified each time someone downloads a file and/or rates a blog post or comment.  That's been a lot of fun to see at least one download a day by someone (most of the time I don't know who they are).  I've received but a very small handful of emails regarding issues with the downloads.  Have they been working for everyone?  I would assume so.  I've found it quite interesting to see that the Image Rotator Control is, by far, the most popular download.  I would not have expected that.

Well, enough rambling.

Here's to another fruitful, fun-filled year!  Enjoy!

 

Tuesday, April 10, 2007 6:57:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [2]  |  Trackback
 Monday, April 09, 2007

There's gonna be a fun MSDN event this coming Thursday, April 12th (same day as our Utah .NET User Group meeting - reminders going out shortly) from 1:00 PM - 5:00 PM at Jordan Landing in West Jordan on ASP.NET AJAX and CardSpace.  I'll be there - it should be a great time.

Here's the event link with all the details:  http://msevents.microsoft.com/cui/EventDetail.aspx?culture=en-US&EventID=1032326499

Monday, April 09, 2007 7:07:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  |  Trackback

This past Easter weekend I had my first chance to dabble in an MMORPG (Massively-Multiplayer Online Role-Playing Game) called 'The Lord of the Rings Online - Shadows of Angmar”.  I've long resisted playing these games (though I flirted with the Star Wars Galaxies game for about 2 hrs) for a multitude of reasons, not the least of which is the potential time-sink it would become.

I was initially turned on to the game by a good friend, Eric Tolman, and the screenshots that he took while in-game.  I thought I'd bite the bullet and install it and give it a trial run during a free, open beta they're calling 'World Tour'.  I can honestly say - WOW (and no, not World of Warcraft).  The game is pretty amazing.  I played for a few hours, completing quests, gaining levels, interacting with other players, formed a fellowship, and helped save the town.  It was a lot of fun.  Like I say, though, I don't have much to compare it to since I've never really played the other games like it.

Though I'm not sure I'll ultimately subscribe to the service (when do I really have time to play games?), I can honestly say I'd have fun if I did.  I still have another 14-odd days left in the World Tour beta before the game goes live, and I intend to enjoy them.

Monday, April 09, 2007 3:51:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [2]  |  Trackback
 Thursday, April 05, 2007

As a reminder to everyone, we've having our second Utah Code Camp this next Saturday (April 14th, 2007) at Neumont University.  The last event was a great success and a ton of fun.  Please set aside the date and register.  Here's the official blurb:

What: Utah Spring Code Camp
When:  April 14th 2007 9:00 AM - 5:00 PM
Where: Neumont University
Registration:
http://utahcodecamp.eventbrite.com

The local .NET Users Group and SQL Server Users Group is conducting a “Code Camp” for local software programmers next month at Neumont University.  The code camp is by the community for the community.  Always free and Always for the community.

The Saturday, April 14th event is scheduled from 9:00 AM to 5:00 PM. The conference is free please register at. http://utahcodecamp.eventbrite.com
Lots of Sponsors and Lots of software and Tech Gadgets to giveaway!
You can check out
www.msutahevents.com  for a session schedule and speaker list for the Code Camp.

Thursday, April 05, 2007 6:12:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, April 03, 2007

It was requested to keep this information confidential until 9:00 AM PDT today, but the hour has past. :)

I recently received word that Microsoft has retracted its initial plan of not including the Expression suite of tools (e.g. Expression Web, Expression Blend, etc) with MSDN.  After the barrage of concerned voices and complaints against this decision, Microsoft has decided to make Web and Blend available to all MSDN Premium subscribers (Web is available now to MSDN downloads).

For further reading see Somasegar's WebLog.

Tuesday, April 03, 2007 3:34:00 AM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  |  Trackback