Considering a sailing adventure to Mexico? Just look at how engrossed that guy is in the book! Grab a copy of the Unauthorized Guide to Sailing in Mexico, and you too can find yourself sitting on a Mexican dock with an oversized (but very attractive) hat.

Unauthorized Guide to Sailing in Mexico

test
« serializing a delegate / queing web services | Main | Henry David Thoreau just keeps getting better »
Wednesday
Jun062007

5 hours later, and Hello World! saves the day

.net hates me today, as well as my co worker Rob.  A lot of work.

It all started earlier today, when Rob called me over and showed me the following error in an application he has under development. His portion consists of a class library, a custom data access layer, and a service wrapper, using asmx / soap / xml.

System.IndexOutOfRangeException: Index was outside the bounds of the array.    at System.Web.Services.Protocols.HttpServerType..ctor(Type type)    at System.Web.Services.Protocols.HttpServerProtocol.Initialize()    at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

Normally, when Rob asks for help, I sweep in for the massive buzzkill, with my super awesome programming skillz. Not today. Today marks the second bug I have found, going back six years, on the .net framework. The first relates to public properties of master pages, and that if you have one, and you have batch compilation enabled, your site just kind of stops working sometimes. Not so cool.

This one would prove to be even weirder. The really kooky part, and what disturbs me to no end, is that Hello World! saved the day. Let's start with a simple custom type of Gumball:

Public Class Gumball Private _color As GumballColor

Private _size As Integer

Private _name As String

Public Property Color() As GumballColor

Get

Return Me._color

End Get

Set(ByVal value As GumballColor) Me._color = value

End Set

End Property

Public Property Size() As Integer

Get

Return Me._size

End Get

Set(ByVal value As Integer) Me._size = value

End Set

End Property

Public Property Name() As String

Get

Return Me._name

End Get

Set(ByVal value As String) Me._name = value

End Set

End Property

Public Overrides Function Equals(ByVal obj As Object) As Boolean

Dim gb As New Gumball() gb = DirectCast(obj, Gumball)

If gb._color = Me._color And gb._name = Me._name And gb._size = Me._size Then

Return True

Else

Return False

End If

End FunctionEnd Class

 

Public
Enum GumballColor

Green

Red

Blue

Orange

End Enum

So, there's our simple Gumball class. Now, we'll create our service wrapper which allows us to touch and poke a Gumball from across the big mighty Internet. What we do doesn't really matter. The important part here is that a public sub (void) with no arguments, combined with a public method that returns the same type that it takes as an argument, gives the error at the top of this page.

Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols
<WebService(Namespace:="http://tempuri.org/")> _

<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Public Class wsRobInherits System.Web.Services.WebService

 

<WebMethod()> _

Public Function InsertWithArgs(ByVal g As Gumball) As Gumball

Dim g2 As New Gumball()

Return g2

End Function

<WebMethod()> _

Public Sub InsertWithoutArgs() End Sub

End Class

What the methods do doesn't really matter, and what the class does really doesn't matter. Putting a breakpoint anywhere doesn't help, nor does anything end up in the server log. Running it in IIS 6.0, or the VS debugger yields the same results. However, if you put the stock Hello World! back in, it works like a champ.

I tried this, and it yields the same result:

<WebMethod()> _

Public Function SelectWithNoArgs() As GumballDim g As New Gumball()

g.Color = GumballColor.Blue

g.Name = "AwesomeGumball"

g.Size = 5

Return g

End Function

What matters isn't really whether you use Hello World! or SelectWithNoArgs (my example); the InsertWithArgs(g as Gumball) as Gumball now works like a champ. We googl'ed around and tried looking for this error, and found a few people who encountered it, but no one was very helpful in diagnosing / resolving. If anyone has found a fix to this problem, please let me know, as it was annoying and wasted a lot of time.

It's a sad day in programming when you can have a problem, but put

<WebMethod()> _

Public Function HelloWorld() As String

Return "Hello World"

End Function

back in, and it all works again. Very sad. Again, if you have a web service that contains a public method with a return type that's the same as the input argument's, you can't execute a public sub web method (a.k.a. void for my c# homies), unless you add in Hello World, or the SelectWithNoArgs thing that I cranked out.

One thing I'm going to check on is whether or not this is limited to HTTP Post / HTTP Get, since we haven't checked this yet to see if SOAP gets kicked back the same way.

Reader Comments (8)

You Sir, have saved me hours by being so kind as to post your finding concerning this, still occurring bug. Exactly as you've explained putting HelloWorld() anywhere inside the implementation fixes the problem. I'd love to see some microsoft genious try and explain how this is a feature and not a bug. :)

August 12, 2009 | Unregistered CommenterVince

Damn feature... I thought the problem was coming from my new IIS 7, but it seems I was wrong!
Thanks for saving me a lot of time!

October 2, 2009 | Unregistered CommenterVlad

It's now more than a year later, I'm using VS2010 and I just ran into this bug.

What the hell... ?

October 26, 2010 | Unregistered CommenterJay

It's been more than three years since I found that bug. It's related (I'm guessing) to the auto-magicalness of how .net handles the converting a class to a web service (the actual beyond-the-IL-implementation).

Crazy stuff man, crazy stuff.

October 26, 2010 | Registered CommenterEric

Today I'm planning on creating a new project and copypasting my source into that one.

Here's to hoping it'll work. Fingers crossed!

October 27, 2010 | Unregistered CommenterJay

Thank you very much for this. I thought I was going crazy.

April 25, 2013 | Unregistered CommenterPaul

Wow, solved! Thanks, Eric!
I found this problem two weeks ago and till today tried to solve it several times. My service (asmx) works fine under several configurations from oldest win2003 (IIS6 .NET2 32bit) to win2008 (IIS7 .NET4 32/64 bit). But it fails as mentioned in this article on new server with win2008sp1. Neither IIS detailed diagnostics (tons of info!) nor debug and configurations comparison pointed to solution.
Let me add full error message with stack to help search this info for guys like me (to add this to index for search engines)

System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

February 19, 2014 | Unregistered CommenterSergey

I must say this very ridiculous solution worked very well for me too.

I tried to remove "helloworld -method" later, and the error made a return with vengeance.

The error occured when I updated some old code and compiled a new version of a project on a server-machine. It has never been a problem before.

I have never stumbled on this error before and is still baffled.

You get +1 for this solution. :)

March 12, 2014 | Unregistered CommenterJohanTheCoder
Editor Permission Required
We're disabling comments for the time being. To contact us directly, please use the form on the "contact" section of this website. Thanks everyone.