Scott Hanselman

Enabling dynamic compression (gzip, deflate) for WCF Data Feeds, OData and other custom services in IIS7

March 30, 2011 Comment on this post [10] Posted in ASP.NET | IIS | OData
Sponsored By

I'm working on a thing that uses an HttpWebRequest to talk to a backend WCF Data Service and it'd be ideal if the traffic was using HTTP Compression (gzip, deflate, etc).

On the client side, it's easy to just add code like this

request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate

or more manually

var request = HttpWebRequest.Create("http://foofoo");
request.Headers["Accept"] = "application/json";
request.Headers["Accept-Encoding"] = "gzip, deflate";

However, you need to make sure this is installed and turned on in IIS7 in you server.

Launch your IIS Manager and go to the Compression module.

Compression Button in IIS Manager

There's check boxes, but it's not installed you may see this yellow alert on the right side.

Compression Alert in IIS Manager

If it's not installed, go to the Server Manager, Roles, Web Server. Under Role Services, check your installed Roles. If Dynamic Compression isn't installed, click Add Roles and install it.

The Dynamic Compression module in IIS manager is installed

You can go back to compression for your site and ensure Dynamic Compression is checked. At this point, Dynamic Compression should be setup, but you really need to be specific about what mimeTypes will be compressed.

Back in IIS Manager, go to the page for the SERVER, not the SITE. Click on Configuration Editor:

The Configuration Editor in IIS Manager

From the dropdown, select system.webServer/httpCompression:

Selecting the httpCompression node in the Configuration Editor in IIS Manager

Then click on Dynamic Types and now that you're in the list editor, think about what types you want compressed. By default */* is False, but you could just turn that on. I chose to be a little more picky and added application/atom+xml, application/json, and application/atom+xml;charset=utf-8 as seen below. It's a little gotcha that application/atom+xml and application/atom+xml;charset=utf-8 are separate entries. Feel free to add what ever mimeTypes you like in here.

Adding MimeTypes graphically in IIS Manager

After you've added them and closed the dialog, be sure to click Apply and Restart your IIS Service to load the new module.

GUIs suck! Command Lines Rule!

If you find all this clicking and screenshots offensive, then do it all from the command line using AppCmd for IIS7. That's lovely also.

appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json',enabled='True']" /commit:apphost
appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/atom%u002bxml',enabled='True']" /commit:apphost
appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/atom%u002bxml;charset=utf-8',enabled='True']" /commit:apphost

Do check your ApplicationHost.config after running this command. See how we had to escape the "+" in "atom+xml" above and made it "atom%u002bxml"? Make sure that + got into your applicationHost.config unescaped. It should look like this in the System.webServer/httpCompression section.





Now, use Fiddler to confirm that compression is turned on by sending a request with the header "Accept-Encoding: gzip, deflate" included. Make sure that Fiddler's "AutoDecode" and Transforms are turned off if you really want to be sure you're looking at the raw stuff.

Confirming in Fiddler that gzip compression is turned on

Turning on Compression is a VERY low effort and VERY high reward thing to do on your servers, presuming they aren't already totally CPU-bound. If you're doing anything with phones or services over low-bandwidth 3G or EDGE networks, it's a total no brainer. Make sure you know what's compressed on your systems and what's not, and if not, why not.

Be explicit and know what your system/sites HTTP Headers are doing. Compression is step 0 in service optimization. I think I mentioned this in 2004. :)

Enjoy!

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

Modifying the default code generation/scaffolding templates in ASP.NET MVC

March 29, 2011 Comment on this post [13] Posted in ASP.NET | ASP.NET MVC
Sponsored By
image

One of the things people like the most about my ASP.NE MVC 2 Ninja Black Belt Tips video, besides the Bill Cosby sweater, is the tip where I show how to modify the default CodeTemplates that are used in Code Generation in ASP.NET MVC (either version).

Eilon mentioned it on an internal mailing list this week so I thought it'd be worth surfacing again in case you haven't heard of this, Dear Reader.

Soon, we'll see even more powerful, flexible, and fun ways to customize your own Scaffolding in ASP.NET MVC 3, so keep an eye out, 'cause it's coming.

Bring the CodeTemplates local to your project

imageWhen you use the Visual Studio "tooling" (that means dialogs and stuff) to Add View or Add Controller, you're actually executing a T4 template and generating a little bit of code. Where does this start and how can you change it?

Try this. Create a new ASP.NET MVC project.

Now, go to:

C:\Program Files (or x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp (or Visual Basic)\Web\MVC (or 2) 3\CodeTemplates

See all those folders? Turns out everything under CodeTemplates are T4 templates that can be either modified in place or even better, can be brought local to your project. Local CodeTemplates will override the global ones.

Copy that folder (or just subfolders like AddController, or AddView) to your ASP.NET project. I do this by dragging from Explorer directly into the Visual Studio Solution Explorer as seen  here:

image

You might get some errors when you drop the folder. Ignore them and instead, select the Templates using Ctrl-Click, then right click and select Properties. (You can delete any templates you won't be using.)

image

Now, see where it says Custom Tool? Clear that string out completely. You're tell Visual Studio that you don't want these T4 Templates to be run during a build. They will only be called manually by the Tooling dialogs like Add View and Add Controller.

You change these templates, or in the case of Add View you can make your own. For example here I've made a new "My Awesome List.tt" as a copy of List.tt and it shows up in the Add View dialog.

image

Now I can not only totally customize what gets generated from the Add Controller and Add View dialogs, I can add custom templates to the drop down. Most importantly, these changes live in the CodeTemplates folder of my project and are carried along with my project in Source Control so others on my team or company can use them as well.

Have you customized yours? Enjoy.

Related Links

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

Good Exception Management Rules of Thumb - Back to Basics Edition

March 24, 2011 Comment on this post [58] Posted in Back to Basics | Learning .NET
Sponsored By

Almost five years ago I posted this "Good Exception Management Rules of Thumb" and had some interesting and useful comments. As with all lists of rules of thumbs, they may no longer be valid. What do you think?

Cori Drew commented to me in an email that she often sees code like this in the wild (changed to protect the innocent 3rd parties). This kind of code inevitably shows up in a file called Utils.cs, which may be your first clue there's trouble.

public void HandleException(String strMessage)
{
//Log to trace file only if app settings are true
if (Properties.Settings.Default.TraceLogging == true)
{
try
{
TraceSource objTrace = new TraceSource("YadaYadaTraceSource");
objTrace.TraceEvent(TraceEventType.Information, 5, strMessage.ToUpper());
objTrace.Flush();
objTrace.Close();
}
catch
{
//do nothing if there was an error
}
}
throw new Exception(Environment.NewLine + strMessage);
}

What's wrong with this code, Dear Reader?

There's a number of things that we can learn from, so have at it in the comments in nice lists bulleted with asterisk, won't you? You can comment on lines, or the larger strategy or both. I'll update the post with a roll-up once you come up for breath.

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

This Developer's Life 1.1.5 - Revolt

March 24, 2011 Comment on this post [1] Posted in Podcast
Sponsored By

15-revolution In this special episode of This Developer's Life we explore the Egyptian revolution. We talk to Remon Zakaria about his experience writing software for a day job and overthrowing the government at night. Big thanks to Ahmed Remy and Reem Ahmed for their help.

Download Here

We wish we could have used everyone's story - there were so many.

  • Remon Zakaria works at DashSoft, a software consultancy in downtown Cairo specializing in turnkey solutions for offshore companies.

You can download the MP3 here (42 minutes) and visit our site at http://thisdeveloperslife.com.

Please consider subscribing with iTunes, or Zune. If you enjoy it, take a moment and please do REVIEW our show on iTunes.

Or if you have a BitTorrent client and would like to help save us bandwidth money, as well as the bragging rights of downloading legal torrents via RSS, get our Torrent Feed at ClearBits.

The bandwidth and other costs for this week's show were picked up DevExpress and CodeRush! Visit them and thank them on Twitter.

DX_Slogan_350

Announcing our listener contest...This Developer's Life - Crowdsourced

Oh yes. We want to hear your stories. Record your best developer stories and send them to us and if we think they rock, we'll include them in the next episode of This Developer's Life.

What we need from you:

  • Your story. We don't want interviews, we want stories. Tell us about your passion, or something crazy that happened at work while solving some technical problem.
  • Keep your audio clean. Use a decent microphone or at least make sure you don't "overdrive" your microphone by talking to close or two loudly. Don't record while mowing the lawn and don't record in a giant echo chamber.
  • Be passionate. Talk to us like you're talking to a friend.
  • Don't worry about editing or music. Just share. We'll handle the Lady Gaga mashups.
  • Note we may move your audio around or change the order of stuff to make it more listenable or interesting or both.
  • Change the names of companies and people to protect the innocent (or guilty)
  • Know that by giving us your audio you're releasing it the Creative Commons and that we may or may not use it for a future show.

Send us a link to your audio file and what you're talking about and we'll do the rest. See you next time!

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

Microsoft "Video Kinect" Chat Review - Video Chat on the Big Screen, The Good and The Bad

March 22, 2011 Comment on this post [11] Posted in Gaming | Remote Work | Reviews
Sponsored By

Video Kinect Call I use Video Chat all the time. Like ALL the time. If you check out the Remote Work section of my blog you'll find dozens of posts about optimizing your video chat experience. Living in rural Oregon and wrking remotely for Microsoft, as well as my job as a community-focused open source individual means that I'm skyping or video calling people much of the week.

I have a Cisco Umi for work, and I use HiDef video cameras and Skype for talking to folks during the day. However, I'm always trying to find out the best way to talk to The Wife and Kids when I'm on the road. I wrote an article a while back called Skyping the Wife: Foolproof Video Conferencing with Your Family While on the Road where I setup auto-answer for Skype so my wife wouldn't have to do anything. However, Skype seems to have removed or hidden the auto-answer feature lately as they are constantly moving their features and options around. Plus, whenever I call home with Skype my wife has to drag out the laptop and with its camera's small field of view I usually just end up seeing the tops of the boy's heads. It's a hassle.

A few weeks ago I saw on Twitter that my Xbox 360 with Kinect supports Video Chat using an app called Video Kinect. Apparently this is already installed when you setup your Kinect so you probably have it already!

Video Kinect is effectively a Windows Live Messenger client. You can chat other Kinect/Xbox Live folks, but you can also chat or receive calls from anyone on your Live Messenger list of friends. One you log in to Messenger (be sure to save your password) you will see avatars for Xbox folks and Live Messenger icons (no faces, sadly) for Windows people.

Video Kinect Friends List

You can call from any of these screens, using your controller or your hands via Kinect.

Video Kinect contact list

First thing you should do, IMHO, is turn off the AutoZoom feature. It uses a Digital Zoom to artificially pan/zoom to the face of the person speaking. It's very disjointed, inconsistent but more importantly, it doesn't allow you to really appreciate the wide field of view that the Kinect camera gives you. It's massive and you can see the whole room...this reason alone is why I think Video Kinect will be THE way I talk to the family when travelling. Especially when the kids are running around.

Video Kinect options

The resolution of the Kinect is 640x480 which is pretty darn good. It looks fabulous from my laptop on the receiving side. See the pic below.

Video call with Video Connect and Live Messenger

The audio is excellent as well, surprisingly so. I can hear and see everything that's going on in the TV room which is fantastic with active kids.

The Good

The Kinect has a great webcam. Good resolution and easy to see.

It's on the big screen and using the big stereo. The kids, ahem, connect more with the large system than the laptop. Daddy's actual size on the flat screen.

The Bad

Either Video Kinect, the codec, or the camera sucks for quick quick action. I see only blurs when the kids are running around. Not sure if this is hardware or software, but it's pointed and reproducible. It's not a deal breaker, but it's clear that they've optimized for the "sit and chat" scenario, not the "watch the kids go insane" scenario. Surprisingly the latter is the 80% in my 80/20 world.

There's no option for FullScreen with PIP. Your local image is the same size as the remote one. At leas make this an option. It's weird.

Answering a call ON the Xbox is ridiculously hard. My wife was unable to do it and it takes too many button pushes. You have to press the center Guide button, then down to select the call notification from an Inbox that comes in like a game invite. "Friend wants to Video Kinect with you..." And all this must happen in (it seems) less than 30 seconds. I called and called and the wife just couldn't do it. This means that all our calls have to be originated by her. When she calls me, I just click Answer in Windows Live Messenger. She also found it too hard to log in, so I just keep it logged in at home as me and I log in as her when remote. I asked some friends and over half said they do the same. It's a common scenario that they (the Powers That Be) aren't optimizing for.

The Wish List

  • Fullscreen
  • Easier or auto-answer
  • Easier to launch Kinect
  • Better framerate
  • Skype support

Still, it's pretty sweet, and since I don't have Skype TV and my Umi is in my office, for me Video Kinect plus Live Messenger is the best solution for travelers calling kids back home today.

Related Links

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.