Wednesday, September 29, 2010

WCF Fire and Forget Method Not Returning

Here's something that might trip you up if you're using a WCF Fire and Forget method to exit early from a long-running method.


Scenario: You create an operation contract and set the IsOneWay attribute to true. Then you implement the method and let's say it takes thirty seconds to finish.










Then on your client when you call this method you create your proxy inside a using statement because you want to dispose of the proxy after the call (when the 'using' statement exits if all goes well).













And when you run your code, the code finishes immediately and and all is well.


Oh, wait. Your client is hanging. It's taking...thirty seconds to finish, which is about as long as it would take if it were a request/response method.


It turns out, if you step into the client code, your proxy call is returning immediately. What's taking the thirty seconds to complete is the disposal of the proxy. The client can't exit from the using statement until the proxy is disposed of, and the proxy will not dispose of itself until the fire and forget method completes.


So what are your options?
  1. Reuse the proxy and/or keep the proxy alive indefinitely.
  2. Make your method request/response and call it asynchronously instead

Saturday, January 9, 2010

My Favorite Shortcuts

I remember at the beginning of my career when I was temping as an administrative assistant ("fossoribus orti") for a large telecom. Another, far superior administrative assistant was showing me something on her computer, and she had to switch between programs. I watched as a box popped up in the middle of her screen with icons for all the programs she was working with. Then the box disappeared and she was in another program.

"What did you just do?" I said.

"Alt+Tab. It saves a lot of time."

And so began my quiet, torrid affair with shortcut keys. To this day, one of my favorite simple pleasures is discovering a shortcut key for something I do countless times throughout the day.

Here is a list of my favorite shortcut keys. This list is not exhaustive or well-rounded, but it does highlight the tools with the most wear in my toolbelt. May they bring you lots of productivity during the new year.

All-purpose shortcut keys:
  • Alt+Tab - Use for switching between programs. If you can work your index finger right, Shift+Alt+Tab will cycle through programs in the other direction.
  • [Windows] + R - opens the Run prompt. An excellent shortcut for opening programs. (NOTE: If you open regedit and navigate to the folder HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\, all of the subfolders contain the names of the programs that can be summoned by program name from a command prompt. You can also add a subfolder for a program that doesn't currently have a shortcut. For example, I have multiple versions of Visual Studio on my machine so I created a vs2005.exe registry subfolder and a vs2008.exe registry subfolder with keys pointing to the actual programs.)
  • [Windows] + E - Opens Windows Explorer. I use this shortcut at least twenty times a day.
  • Shift+F10 - Mimics a right-mouse click. I just learned about this shortcut on twitter a couple days ago. Most of my keyboards have a key for this, but the netbook I was playing with a month ago didn't. This would have saved me lots of time had I known about it then.
  • Alt+F4 - Closes an open program. Hitting Alt+F4 twice in rapid succession will open the shutdown screen for your computer, so if you're just closing down programs make sure you pause a half second between uses.
  • Ctrl+F4 - For programs that allow multiple documents or tabs to be open, Ctrl+F4 will close the window or tab with the current focus.
  • Ctrl+Tab - Cycles between tabs or documents in a program. Works for Chrome, IE, Visual Studio, and other programs.
Chrome/Firefox/IE shortcuts
  • Ctrl+T - Creates a new tab. Doesn't always work in Chrome or in IE, depending on the objects that are running on the current page. Sometimes if a media file has the focus it won't open up a new tab.
  • Alt+LeftArrow - Mimics the back button
  • Alt+RightArrow - Mimics the forward button
Microsoft Outlook
  • Ctrl+1 - Switches to the Mail view
  • Ctrl+2 - Switches to the Calendar view
  • Ctrl+Shift+I - Switches to Inbox view, regardless of where you are
  • Ctrl+Shift+M - creates a new email message, even if you're not in Mail view
  • Ctrl+Shift+Q - creates a new meeting, even if you're not in Calendar view
  • Ctrl+D - If you have an email open, deletes the email
  • Ctrl+. - If you have an email open, moves to the next email in the list
  • Ctrl+, - If you have an email open, moves to the previous email in the list
Visual Studio (too many shortcut keys to mention. Here's what I use on a regular basis):

Solution Management
  • Ctrl+Shift+B - Builds your solution
  • Ctrl+Shift+N - Creates a new solution and project
  • Ctrl+Shift+O - Opens an existing solution or project
Outlining (I make heavy use of the #region keyword in my .cs files as
a way to collapse and expand logically related sections of code like
private fields, public properties, interface implementations, etc.
These keys come in very handy when trying to read a big .cs file)
  • Ctrl+M, Ctrl+O - When your cursor is in a .cs file, this combination will collapse all outlining, all the way up to the namespace level
  • Ctrl+M, Ctrl+M - this combination will collapse or expand a single section of code. For example, if your cursor is on the class declaration line, this combination will collapse or expand the class. Same for methods, properties, and regions. Basically, it looks for the next highest collapsible section and collapses it.
Testing
  • Ctrl+R,Ctrl+T: If your cursor is inside a test method, this shortcut will run your test. R -> T. Run -> Test. Makes perfect sense. I think I discovered this in Scott Guthrie's excellent ASP.NET MVC Tutorial.
  • Ctrl+R,Ctrl+A: Runs all of the tests in a project.
  • F9 - sets a breakpoint a the cursor location
  • F10 and F11, the debugging twins - when debugging, F10 steps over a method call, F11 steps into the highlighted method call (if it can). Another handy one to know is Shift+F11. If you step into a method that has a loop and you want to test the first few iterations of the loop and move on, Shift+F11 will step OUT of the current method back to the calling method.
  • F12 - moves to the definition of the item in which the cursor is currently placed