IronShay

Ironing code, geek t-shirts and even presentations!

NAVIGATION - SEARCH

Local IIS Not Working and the horrifying “The Server Committed a Protocol Violation” Error

The Problem

I was working on a web site on my local computer. When I wanted to run the web site on my local IIS, I suddenly received a 404 Not Found error for everything hosted on my local IIS.
I tried to run iisreset (even twice, of course!) and it didn’t help. I then tried to start debugging from within Visual Studio, just to see what happens – then I received the OMFG error: “The Server Committed a Protocol Violation. Section=ResponseStatusLine”.

What?!?

I turned to my old friend, Google. He (or she?) helped me find Martin Kulov’s post - http://blog.kulov.net/2006/06/server-committed-protocol-violation.html, which suggested that some application had already been using port 80 (the one IIS is using). This results in IIS not being able to load and eventually throwing all of these doomsday errors.

The Solution

I downloaded TCPView to figure out which application is responsible for all the mess. I was surprised (or not) to find out which application it was:

Local IIS Not Working and the horrifying “The Server Committed a Protocol Violation” Error

It was SKYPE!!!! WTF???

Killing Skype and restarting IIS solved the problem and I was able to go back to work.

Note: I’m using Skype 5.10.0.116.

All the best,
Shay.



Video, Slides and Code from my Session at aspConf 2012 – ASP.NET MVC Tips, Tricks and Hidden Gems

Last week I had the honor of taking part in the community-driven, ASP.NET-related, virtual event – aspConf 2012. My session was named ASP.NET MVC Tips, Tricks and Hidden Gems and it was generally about things I found to be important from my ASP.NET MVC experience – some were more basic and some were more hidden, too hidden some would say :)

I had lots of fun doing the session, and hopefully the attendees has fun too :)

A big big thanks to the aspConf crew – Javier Lozano, Jon Galloway, Eric Hexter, and friends – you guys did an AMAZING job! thanks!

Anyway, everything from my session is now on the interwebs – video, slides and code samples:

Video

Can be watched and downloaded on channel9: http://channel9.msdn.com/Events/aspConf/aspConf/ASP-NET-MVC-Tips-Tricks-and-Hidden-Gems

Slides

The slides are available on SlideShare: http://www.slideshare.net/shayfriedman/aspnet-mvc-tips-tricks-and-hidden-gems

 

Code Samples

All code samples from the session are available on my github page: https://github.com/shayfriedman/aspConf-mvc-tips-tricks-and-hidden-gems

That’s it. If you have any questions, let me know!
All the best,
Shay.



Razor Tip #2: Output Text in Code Context

Razor, the new ASP.NET MVC view engine is incredible. I like it. A LOT. Great work Microsoft!

In this series of posts I’m sharing some handy tips and tricks that can enhance your experience with this new view engine. Enjoy!

The Problem

Razor is the place where HTML and C# live together in harmony. This is, in my opinion, one of the things that make razor the great view engine that it is. However, there’s a fly in the ointment. Assume you want to output “Good Morning!” if the hour is between 6AM and 9AM:

Razor Tip #2

But oh no! Razor thinks “Good Morning!” is code!

The Solution

Razor has a special solution for that problem. Three different solutions for the matter of fact:

  • Writing the text inside an HTML element – razor knows to differentiate between C# and HTML so if you wrap the text with an HTML element, everything will work as expected:
    Razor Tip #2
  • Adding the “@:” symbol at the beginning of the line – using this symbol will tell razor that this line is an output line and should not be treated as code:
    Razor Tip #2
  • Using the special <text></text> element – when you want to output several lines within code context, it becomes irritating to use the “@:” symbol… For that you’ve got the special <text></text> element which tell razor that its content is to be outputted as HTML. Notice that the <text> element will not be outputted to the final HTML output.
    Razor Tip #2

All the best,
Shay.



LINQ Tip: Chain Ordering

Assuming you have the next code:

public class Person
{
  public string Name { get; set; }
  public int Age { get; set; }
}

public class Whatever
{
  public void Do()
  {
    var people = new List<Person>
                  {
                     new Person {Name = "Shay Friedman", Age = 27},
                     new Person {Name = "Shawn Doe", Age = 51},
                     new Person {Name = "Elvis Presley", Age = 76}
                  };            
  }
}

And now you want to order it first by name and then by age using LINQ. If you were to do that:

var orderedPeople = people.OrderBy(p => p.Name).OrderBy(p => p.Age);

You would get incorrect results:

LINQ Tip: Results when using OrderBy after an OrderBy call.

That’s because the second OrderBy call just overrides the first call results. To fix that, use the ThenBy LINQ method:

var orderedPeople = people.OrderBy(p => p.Name).ThenBy(p => p.Age);

And now everything works as expected:

LINQ Tip: Using ThenBy after an OrderBy call.

All the best,
Shay.



Developing Excel Functions in C# for Excel 2010 64-Bit

Lately I needed to write a small “add-in” for Excel that adds a function written in C# (another case of “when your 4 years of office add-in development experience comes back to haunt you”). Before going ahead with that I searched the web for a quick how-to and ran into this old but still relevant blog post by Eric Carter.

I did everything Eric had described, but every time I tried to add the add-in through the Automation Servers dialog I just couldn’t find it in the list. Trying to locate the file through the Browse button resulted in the next window:

The file you selected does not contain a new Automation Server, or you do not hace sufficient privileges to register the Automation Server.

Such a sweet little message that tells you so much and at the same time tells you nothing at all. How can’t you love developing for Office?

After a couple of hours fighting it with everything I knew (run as admin, installing through a setup project, rewriting everything and more) just when I was ready to throw the laptop out the window and wait for Chrome OS to come out, the brilliant mind of Eran Stiller came up with the question – “maybe it’s related to some 64-32 bit stuff”.

Facepalm. Head-to-desk. Facepalm again. Of course it was related!

As you can understand, I have Excel 2010 64bit installed on my machine. And that’s the root of all my problems. Yes, each and every single one of them.
Anyway, To get the addin working on my machine I had to manually register the DLL using the .NET framework’s 64bit regasm utility. If you also have the same problem, follow the next steps to solve it:

  1. Follow all the steps in Eric Carter’s blog post.
  2. Open the project’s properties and uncheck the “Register for COM Interop” checkbox (I actually don’t think it really matters but just in case).
  3. Rebuild the project.
  4. Open cmd (open Start->Run, type “cmd” and hit Enter).
  5. Navigate to %windir%\Microsoft.NET\Framework64\v4.0.30319
  6. Run: regasm /codebase <path_to_dll>\MyAssembly.dll
    Pay attention to the /codebase switch – without it, it won’t work!

That’s it, you’re done. Your command prompt would look similar to that:

regasming the assembly

[Notice that you’ll get a warning here that the assembly is not signed. If you’re writing a real product, make sure you sign it!]

After that, open Excel and add your DLL again (according to the instructions in Eric’s post). After you do that, try using your functions!

I’m adding my sample project here (which works for me) and its output assembly so you can play with it:

Project (5Kb)
Assembly (4Kb)

Let me know if something isn’t clear or doesn’t work for you.

All the best,
Shay.



After installing Process Explorer you get “Windows cannot find “C:\Windows\System32\taskmgr.exe”” when trying to open Task Manager

One of the first steps I do when installing a new computer is downloading process explorer and make it replace the default Windows Task Manager.

However, with my new computer not everything went so smooth and after setting Process Explorer to replace the Task Manager, it screwed something up. After that, every time I tried to load the task manager I received the error: “Windows cannot find “C:\Windows\System32\taskmgr.exe”:

Such things can drive me crazy!

Anyway, I researched a bit and found the problem, which just like any Windows problem sourced in the registry.

So if you have the same problem, follow the next steps to solve it:
Warning: modifying the registry incorrectly can cause serious problems that may require you to reinstall Windows. Handle with care!

  1. Open the registry (regedit is a great tool for that).
  2. Go to: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\taskmgr.exe
  3. There you will find a string value named “Debugger”. This is the source of the problem and it’s probably filled with some unrecognizable string.
  4. If you want the default Task Manager back, set Debugger to a blank value.
  5. If you want to use Process Explorer as your task manager, set Debugger to the path to process explorer. For example, “c:\Utils\ProcessExplorer\procexp64.exe”.

That’s it.

All the best,
Shay.