Scott Hanselman

Useful Visual Studio Extension - Web Essentials from Mads Kristensen

November 08, 2011 Comment on this post [62] Posted in ASP.NET | Tools | VS2010
Sponsored By

Visual Studio 2010 is really extensible and that's allowed many folks on the team to try out new features for Web Development without having to rebuild Visual Studio itself. One of those "playground" extensions is called "Web Essentials" by Mads Kristensen. Mads handles HTML5 and CSS3 tools for our team. You might remember Mads from when we released the Web Standards Update a few months back.

Web Essentials is Mads' playground and it's pretty awesome. It's so awesome that I think you should check it out and then, *ahem*,  leave a comment on this post encouraging Mads and gentle urging his boss(es) to get these features into the next version of Visual Studio.

First, it adds a few nice touches to the CSS Editor in Visual Studio. As you can see below, it adds a little glyph next to each color that shows you the actual color.

A nice visual refresh to the Visual Studio CSS editor

If you hover over the colors, you'll get a preview with more details. You'll also get font previews on mouse hover.

Font Previews are fun

These are just nice little touches that make you smile. They also smooth little issues with my daily workflow. After having them, I miss them when they are gone.

There's also some more functional features, like embedding tiny images in your CSS files as base64. Sounds nuts, but if  your small icons are so small that the HTTP Headers are larger than the image itself this can be an easy way to remove an HTTP request.

Here I'm taking the tiny "Ajax Loader" PNG in my app and turning it into an inline image.

Embed Resource as base64

So those

Look at the tiny images as base64

It even has some evil little helpers help you use well-known CSS hacks to control visibility of rules. Like, if you hate yourself and use IE6. ;)

Enabling evil with IE6 hacks

You can minify just a selection of text if it makes you happy.

image

You can drag and drop images and automatically get a background-image CSS rule, which is a HUGE time saver. Oy. All this and a bunch more little subtle stuff.

The best feature and feature that should cause you to download it NOW is that you can do live CSS editing in Visual Studio with a real-time preview.

Fire up a CSS file and hit Ctrl-Alt-Enter. Then click Settings and hit "Live Update CSS." Bam. As you type it'll get updated in the browser. Love this.

image

Do you like these playgrounds? Do you want some or all of these features in Visual Studio? Sound off below.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Back to Basics: Daylight Savings Time bugs strike again with SetLastModified

November 06, 2011 Comment on this post [32] Posted in ASP.NET | Back to Basics | Learning .NET
Sponsored By

CC BY-NC 2.0 Creative Commons Clock Photo via Flickr ©Thomas Hawk No matter how well you know a topic, or a codebase, it's never to late (or early) to get nailed by a latest bug a over a half-decade old.

DasBlog, the ASP.NET 2 blog engine that powers this blog, is done. It's not dead, but it's done. It's very stable. We had some commits last year, and I committed a bug fix in February, but it's really well understood and very baked. My blog hasn't been down for traffic spike reasons in literally years as DasBlog scales nicely on a single machine.

It was 10:51pm PDT (that's Pacific Daylight Time) and I was writing a blog post about the clocks in my house, given that PST (that's Pacific Standard Time) was switching over soon. I wrote it up in Windows Live Writer, posted it to my blog, then hit Hanselman.com to check it out.

Bam. 404.

What? 404? Nonsense. Refresh.

404.

*heart in chest* Have I been hacked? What's going on? OK, to the logs!

l2    time    2011-11-06T05:36:31    code    1    message    Error:System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: utcDate
at System.Web.HttpCacnhePolicy.UtcSetLastModified(DateTime utcDate)
at System.Web.HttpCachePolicy.SetLastModified(DateTime date)
at newtelligence.DasBlog.Web.Core.SiteUtilities.GetStatusNotModified(DateTime latest) in C:\dev\DasBlog\source\newtelligence.DasBlog.Web.Core\SiteUtilities.cs:line 1253
at newtelligence.DasBlog.Web.Core.SharedBasePage.NotModified(EntryCollection entryCollection) in C:\dev\DasBlog\source\newtelligence.DasBlog.Web.Core\SharedBasePage.cs:line 1182
at newtelligence.DasBlog.Web.Core.SharedBasePage.Page_Load(Object sender, EventArgs e) in C:\dev\DasBlog\source\newtelligence.DasBlog.Web.Core\SharedBasePage.cs:line 1213
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) while processing http://www.hanselman.com/blog/default.aspx.

What's going on? Out of range? What's out of range. Ok, my site is down for the first time in years. I must have messed it up with my clock post. I'll delete that. OK, delete. Whew.

Refresh.

404.

WHAT?!?

Logs, same error, now the file is a meg and growing, as this messages is happening of hundreds of times a minute. OK, to the code!

UtcSetLastModified is used for setting cache-specific HTTP headers and for controlling the ASP.NET page output cache. It lets me tell HTTP that something hasn't been modified since a certain time. I've got a utility that figures out which post was the last modified or most recently had comments modified, then I tell the home page, then the browser, so everyone can decide if there is fresh content or not.

public DateTime GetLatestModifedEntryDateTime(IBlogDataService dataService, EntryCollection entries)
{
//figure out if send a 304 Not Modified or not...
return latest //the lastTime anything interesting happened.
}

In the BasePage we ask ourselves, can we avoid work and give a 304?

//Can we get away with an "if-not-modified" header?
if (SiteUtilities.GetStatusNotModified(SiteUtilities.GetLatestModifedEntryDateTime(dataService, entryCollection)))
{
//snip
}

However, note that I'm have to call SetLastModified though. Seems that UtcSetLastModified is private. (Why?) When I call SetLastModified it does this:

public void SetLastModified(DateTime date)
{
DateTime utcDate = DateTimeUtil.ConvertToUniversalTime(date);
this.UtcSetLastModified(utcDate);
}

Um, OK. Lame. So that means I have to work in local time. I retrieve dates and convert them ToLocalTime().

At this point, you might say, Oh, I get it, he's called ToLocalTime() too many times and double converted his times. That's what I thought. However, after .NET 2 that is possible.

The value returned by the conversion is a DateTime whose Kind property always returns Local. Consequently, a valid result is returned even if ToLocalTime is applied repeatedly to the same DateTime.

But. We originally wrote DasBlog in .NET 1.1 first and MOVED it to .NET 2 some years later. I suspect that I'm actually counting on some incorrect behavior deep in own our (Clemens Vasters and mine) TimeZone and Data Access code that worked with that latent incorrect behavior (overconverting DateTimes to local time) and now that's not happening. And hasn't been happening for four years.

Hopefully you can see where this is going.

It seems a comment came in around 5:36am GMT or 10:36pm PDT which is 1:36am EST. That become the new Last Modified Date. At some point we an hour was added in conversion as PDT wasn't PST yet but EDT was EST.

Your brain exploded yet? Hate Daylight Saving Time? Ya, me too.

Anyway, that DateTime became 2:36am EST rather than 1:36am. Problem is, 2:36am EST is/was the future as 6:46 GMT hadn't happened yet.

A sloppy 5 year old bug that has been happening for an hour each year that was likely always there but counted on 10 year old framework code that was fixed 7 years ago. Got Unit Tests for DST? I don't.

My server is in the future, but actually not as far in the future as it usually is. My server in on the East Coast and it was 1:51am. However, the reasons my posts sometimes look like they are from the future, is I store everything in the neutral UTC/GMT zone, so it was 5:51am the next day on my file system.

Moral of the story?

I need to confirm that my server is on GMT time and that none of my storage code is affected my Daylight Saving Time.

Phrased differently, don't use DateTime.Now for ANY date calculations or to store anything. Use DateTime.UTCNow and be aware that some methods will freak out if you send them future dates, as they should. Avoid doing ANYTHING in local time until that last second when you show the DateTime to the user.

In my case, in the nine minutes it took to debug this, it resolved itself. The future became the present and the future last modified DateTime became valid. Is there a bug? There sure it, at least, there is for an hour, once a year. Now the real question is, do I fix it and possibly break something that works the other 8759 hours in a year. Hm, that IS still four 9's of uptime. (Ya, I know I need to fix it.)

"My code has no bugs, it runs exactly as it was written." - Some famous programmer

Until next year. ;)

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Got clocks?

November 06, 2011 Comment on this post [34] Posted in Musings
Sponsored By

"Spring forward, fall back"
- The Idiot who invented Daylight Savings Time.

It's time to switch from Daylight Savings Time to Standard Time. I tweeted earlier that I'd gone around the house and updated, by rough count, 18 clocks. Then, I got a bunch of responses like "I've only got one," or "All mine are automatic," or "18 clocks, you're insane."

Turns out the real number is WAY more than 18. I didn't realize.

So, this is for all you folks with your microwave flashing 12:00. I decided to take a moment and really list out all the devices in my house that know the time, separating them by the ones I need to update myself and the ones that automatically update themselves. I realize I could NOT update 50% of them, if not more, and I realize I have a number of gadgets.

Fine, I like clocks. More perhaps than I realized. That said, I really only need the time right on the wristwatch I'm wearing at the time. For you young people, a wrist watch is a non-internet connected device worn often with a leather or metal strap that allowed you to glance as your arm and know the time. It did NOT have Siri, nor did it run Opera Mobile.

Most of these "don't matter" but it was the sheer number of time-keeping devices, both direct and indirect, that surprised me.

binaryclock Clocks I need to change manually

  • Six cheap wall clocks. In bedrooms, bathrooms, playroom.
  • Five clock radios. Bedrooms, guest room.
  • Stove
  • Microwave above stove
  • Insulin Pump
  • Two Blood Glucose Meters
  • Cool Digital Binary Clock on my desk (at right)
  • Two car radios with clocks
  • Zoom H4N Recorder (for my podcast)
  • Casio S95 Digital Camera
  • Canon SLR Digital Camera
  • Cordless Phone Base (3 satellite phones)
  • Nine wrist-watches (I collect watches)
  • Rainbird Water Timer

Clocks that are automatically updated

Of course, many of these are random Craigslist tech, but even if you remove those, you've still got a lot of things keeping time around the house. 

I bet you have more things in your life that care about the time than you think. How did we survive the Year 2000 again? Oh, none of these clocks matter, that's right.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Your images are a virus. They are EVERYWHERE on the Internet

November 05, 2011 Comment on this post [13] Posted in Blogging | Musings
Sponsored By

Stuff you love to do, Stuff you're good at, Stuff someone will pay you to do.This is a silly little story of a silly little image. An image whose original conceptual source isn't me. Well, I made the image, I made it in PowerPoint with Smart Art in about 12 seconds over 4 years ago. But per my G+ friend James Saull, I know now that the concept is called "The Hedgehog Principle" or "The Hedgehog Concept" by Jim Collins. You'll find it everywhere but it started in 2001 with the book "Good to Great." I likely absorbed it at some past point and when I got the job I drew three cricles. It's one of those "duh, that's awesome" concepts. It's just a Venn Diagram with three circles with the intersection, the middle bit, being the most awesome ideal part. This isn't about the original of the concept, it's how one image is found everywhere on the internet, spreading like a virus. Once you put an image on the internet, you'll never be able to take it down.

I made the circles for a blog post when I took the gig at Microsoft in July of 2007. I haven't thought about them much since, although I've used them in some presentations a few times. They are unimpressive and rather pink.

Yesterday at lunch I was on Facebook and commented on a friends photo. Her friend "liked" my comment, and I clicked to see who that friend of a friend was. Then just scrolling down, I saw my circles on this stranger's wall. Cool! What an amazing coincidence.

Dream Job on FB

This image was shared from another page within Facebook. I followed the rabbit. The photo had hundreds of likes and many shares.

Dream Job on FB

I then started wondering how far this thing went. Well, it spread long before this funny Facebook coincidence. Kyaw Zaw suggested on Google+ that I put my original image into Google Image Search.

Googling with a custom date range shows the first instance of my image, on my blog in 2007. You can also search for images with images using TinEye Image Search.

I can't see how to reliably hotlink to Google Image Search results, so go to http://images.google.com/, click the little camera icon and paste in the URL to the image, like http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/2dc61d7e4a66_13443/image.png

Googling for Dream Job with Google Image Search

Widening the search dates to all dates, I can see there are 228 different places this image appears, mostly on career and inspirational/aspirational blogs.

Lots of Dream Job on the second page

Image search systems are a fascinating way to see how your images find their way around the web. If you had some intellectual property embedded within your images, presumably you could watermark your images but I suspect intuitively that heavily watermarked images might not spread as freely. There's just no easy way to "protect" (if you wanted to) your images these days. So they spread!

What images of yours have spread around the internet?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Google+ Ripples brings something interesting to the table

November 04, 2011 Comment on this post [14] Posted in Blogging | Musings
Sponsored By

I'm on Twitter. I'm on The Facebook. And, sigh, I'm on the snoozefest that has been Google+. I keep coming back to Twitter though for so many reasons

Twitter, today, is just easier to to use, the "What's happening" text box is always there, sharing is effortless. But the lack of real threading, of discussion, is starting to wear on me. Why do sites like even Storify exist? They are there to try to piece the shatter pieces of your Twitter discussion back together. Seriously, I challenge you to piece together anything two weeks after it happened on Twitter. How many retweets did your awesome Tweet get? 100+. Maybe more. You'll never know.

But, the Google+ mobile apps are a mess, so I've just not found a reason to spend much time on G+, except for the occasional Hangout. Until today.

Ripples. Wow.

Pick a post, like I did here, click the down-chevron and click "View Ripples."

Give the users their data. Give them analytics. Let them see how their data moves around the web and how it happened. Let them understand how the network lives and works with elegant visualizations. While you at it, animate the process. Brilliant.

Your move, Twitter. Now it's getting interesting.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.