# Tuesday, June 29, 2010

A while back, I was tasked with solving a defect in our system. The defect was “Changes to an image can be reverted only one time.”

As I started to look into the code, I saw something like this:

  1. private Image _originalImage;
  2. private Image _image;
  3.  
  4. public void Show(Image image) {
  5. _image = image;
  6. _originalImage = (Image) image.Clone();
  7. }
  8.  
  9. private void Revert() {
  10. _image = _originalImage;
  11. }
 

Looking at the code, I suspected I knew what was wrong, but I wanted to confirm it. So I ran the app, made modifications to the image, clicked revert, made modifications to the image again, set a break point and clicked revert again. Once Visual Studio broke, I popped over to the immediate window and typed this:


&_image
Which returned: 0x1234abd0
&_originalImage
Which also returned: 0x1234abd0

Now I knew what the issue was, the original image was getting clobbered due to a missed .Clone() on line 10. A simple _image = (Image)_originalImage.Clone(); fixed the issue.

.NET | C#
Tuesday, June 29, 2010 6:45:00 AM (Alaskan Daylight Time, UTC-08:00)
# Monday, June 28, 2010

The other day, Visual Studio 2008 kept crashing due to an issue with one of our user controls. When I opened a WinForm which contained the control, VS would initialize everything, get to an exception and crash. The exception was caused by the user control running code that it should not have in the designer. A quick search turned up this post on stackoverflow

  1. if (LicenseManager.UsageMode == LicenseUsageMode.Runtime) {
  2.   ...
  3. }

This code works in most scenarios, but failed for me. More searching turned up a post by Andre de Cavaignac

  1. public bool IsDesignerHosted {
  2.   get {
  3.     Control ctrl = this;
  4.     while (ctrl != null) {
  5.       if (ctrl.Site == null)
  6.         return false;
  7.       if (ctrl.Site.DesignMode == true)
  8.         return true;
  9.       ctrl = ctrl.Parent;
  10.     }
  11.     return false;
  12.   }
  13. }

I found that mostly worked as well. I ended up having to combine them to detect correctly 100% of the time if the control was designer hosted. I created it as an extension method, and here is the result

  1. /// <summary>
  2. /// Used to detect whether the control is being hosted on the surface of a form at design-time,
  3. /// since the DesignMode property no longer works once the control has been compiled and
  4. /// hosted on the surface of another design-time control.
  5. /// </summary>
  6. public static bool IsDesignerHosted(this Control ctrl) {
  7.   get {
  8.     while (ctrl != null) {
  9.       if (ctrl.Site == null)
  10.         return false;
  11.       if (ctrl.Site.DesignMode)
  12.         return true;
  13.       ctrl = ctrl.Parent;
  14.     }
  15.  
  16.     return (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
  17.   }
  18. }
C# | WinForms
Monday, June 28, 2010 5:45:00 PM (Alaskan Daylight Time, UTC-08:00)
# Sunday, June 27, 2010

After not blogging for over a year, I decided to get back in the swing of things and write more. I clicked on a link and got the dreaded 404. So I decided to investigate.

About 6-8 months ago I upgraded my server last year to Server 2008 and IIS 7. I checked that my blog loaded, but never checked any links. It turns out that IIS 7 be default blogs "+" in URLs because it can pose security issues. Well, all my blog post links have "+" in them. This broke things :(

After some digging, I figured out how to fix it. Add the following to your Web.config file
<configuration>
  <system.webServer>
    <security>
      <requestFiltering allowDoubleEscaping="True"/>
    </security>
  </system.webServer>
</configuration>

Sunday, June 27, 2010 4:02:31 PM (Alaskan Daylight Time, UTC-08:00)