Archive for June, 2005

Four Irritating Lines of Code

At many places in the framework, you’ll see properties of types named for the enumerations or types they receive. I was indifferent to that until just now.

I just wrote these four lines of code. It took much longer than it needed to.

BinaryFormatter f = new BinaryFormatter();
f.FilterLevel = TypeFilterLevel.Low;
f.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
f.AssemblyFormat = FormatterAssemblyStyle.Simple;

Consider the last three lines. None of the enumeration names are even close to the corresponding property on the formatter.

For all three of these, I typed out something like this:

f.FilterLevel = FilterLevel.

[No Intellisense, there’s a problem]

[Backspace repeatedly]

f.

[Intellisense shows up; oh, it’s “TypeFilterLevel”]

f.FilterLevel = TypeFilterLevel.Low;

Maybe the usability studies didn’t make it to System.Runtime.

Annoy Your Friends and Colleagues with Global Flags

Step 1

Open gflags.exe. It comes with the Debugging Tools for Windows.

Step 2

On the Image File tab, enter the name of a popular executable. Suggestions:

  • devenv.exe [developers]
  • outlook.exe [human resources]
  • excel.exe [bankers and finance dweebs]
  • iexplore.exe / firefox.exe [catch-all]
  • Step 3

    Press tab to refresh the form. Check “Debugger,” and enter sol.exe in the textbox. Press ok.

    Step 4

    Bask in the hilarity of your sophisticated prank.

    gflags.exe

WTF

This Daily WTF reminded me of a guy I used to work with who would use this pattern in every single function he wrote.

Public Const cSTRINGFOO As String = "foo"

Public Function Foo() As Boolean
    Dim ret As Boolean = False
    Try
        Console.WriteLine(cSTRINGFOO)
        ret = True
    Catch ex As Exception
        ret = False
        Throw New Exception("", ex)
    End Try
    Foo = ret
    Exit Function
End Function

I’m not sure what he was expecting to happen here, but there were some clues:

  • Every function he wrote returned true or false to indicate that it had successfully completed without an exception.
  • Every exception was wrapped in another, except where he forgot and swallowed the exceptions.
  • He would always use constants for string literals, even if he only used the literal once (like above), and even if the constant was much longer than the literal.

Mind you, I discovered this self-imposed standard a while after he quit, so I never really got to the bottom of it.

Bizarro TypeInitializationException in System.Runtime.Remoting

We’ve seen this exception on two occasions in three months in a production environment. I’m going to throw it up here since there are no Google hits on it so far, at least that I can find.

System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. ---> 

System.TypeInitializationException: The type initializer for
"System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory" threw an exception. --->
System.MissingFieldException: Field not found: ?.s_webServicesFactoryType.

   at System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory..cctor()

   --- End of inner exception stack trace ---

   --- End of inner exception stack trace ---

   at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly)

   at System.Activator.CreateInstance(Type type, Boolean nonPublic)

   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)

   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)

   at System.Web.Configuration.HandlerMapping.Create()

   at System.Web.Configuration.HandlerFactoryCache..ctor(HandlerMapping mapping)

   at System.Web.HttpApplication.GetFactory(HandlerMapping mapping)

   at System.Web.HttpApplication.MapHttpHandler(HttpContext context,
String requestType, String path, String pathTranslated, Boolean useAppConfig)

   at System.Web.MapHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()

   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

This is a type initializer (.cctor) in System.Runtime.Remoting throwing up because one of the private fields cannot be found. Here’s the type in Reflector, proof that something is definitely rotten in the state of Denmark.

The s_webServicesFactoryType static field

The type initializer is just trying to set this field to null.

HttpRemotingHandlerFactory static initializer

Here are some more of the details:

  • The servers were different, but running the same application. Their windows patch levels would have been the same.
  • w3wp is set to recycle on these servers. This exception will happen constantly (thousands of times) until the recycle happens forcibly or on its own, then it goes away.

So, I think this is definitely a JIT or assembly loader bug related to some obscure timing issues. If I can get the native image or a full process dump while this is happening, presumably this could be tracked down.