Bizarro TypeInitializationException in System.Runtime.Remoting
June 7th, 2005

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.

Reflector view of HttpRemotingHandlerFactory

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

Reflector disassembly of HttpRemotingHandlerFactory type constructor

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.


Conversations in the Park
May 29th, 2005

Hoboken appears to be overrun with evangelicals. Last week on my way to the train there was a gentleman standing quietly with a sign that read:

CATHOLICISM IS THE ONLY TRUE RELIGION

ALL NONBELIEVERS WILL SUFFER HELLFIRE

It certainly made me glad that his god only exists in his head.

So I went down to the park today to get some reading done, but it wasn’t in the cards. I ended up spending most of the time speaking to two groups of people who were very interested in “saving” me.

Part I — The Young Brazilian Girls

This one was very confusing. It mostly went like this.

“Don’t you believe in the Bible?”

“I believe that it exists. What do you mean, exactly?”

“Why do think there’s all this evil in the world?”

“What?”

“Don’t you believe in the devil?”

“Why did god create him, again?”

Except I feel like we covered that material three or four times. These girls were pretty attractive, but not prepared to debate. That’s a shame, from their point of view. I have no doubts that they would be a real “double-threat” if they could draw from even a few Aristotelian ideas, or throw out some quotes from Thomas Aquinas.

They also kept asking me if I remembered this scene or that scene from The Passion of the Christ. I’ve never seen the Passion of the Christ. Is it a good sign or a bad sign that serious Christian thought has devolved to the point of being dictated by Mel Gibson?

That went on for about a half an hour, until one of them made the “talk to the hand” motion at me and walked away (I am not making this up). I thought I was conversing in a reasonably calm way, by my standards.

After that, I tried moving to a quieter park. The one right on the waterfront is thunderdome, anyway.

Part II — The Jersey Guy

This encounter was much more interesting. This was with a very soft spoken gentleman wearing a tie. “I’m not a Mormon or a Jehovah’s witness,” was the first thing he said. He appeared to be trying to start his own religion. Bravo, good for him.

He spent some of his allotted timeslot complaining about “preachers and priests that drive around in Mercedes,” as he put it. He also stated that “the Bible proves evolution,” which put a pretty interesting spin on the conversation.

Perhaps it was his intention, but I was actually arguing against the absolute provability of a thing like evolution.

“It’s so incredibly likely to be true that you could say that it is proven, but I don’t think any real scientist would say such a thing,” I said. “When you say that the Bible proves it, you’re giving away a very different idea of ‘proof’ than I have.”

He was, (not surprisingly, I guess), impressed with the Bible’s powers of prophecy. He shied away from it, I think detecting that it wouldn’t win me over. We talked about the vague wording, lack of falsifiability, and rationalizing after the fact that is involved in prophecy, and he conceded that I was probably right about that.

In the end, we agreed that I had no answers for him, and he none for me. And we decided that recruiting people to follow you is not a good idea in general, because people should think for themselves. All in all, a very pleasant conversation. It could have been one of the scenes in Waking Life.

Part III — Winding Down

I came back and my roommate was drinking wine and eating cheese. “You need to take the wax off of that cheese before you eat it,” he said. Oh, do I? I bit down. Yes, it turns out that I do. But being a contrarian is fun nonetheless.


Console.WriteLine with Wordwrap
May 17th, 2005

Here’s a class I wrote that will write with wordwrap to standard out in a .NET console app. There are a few other nifty options.

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;

namespace McKinley
{
    public sealed class Out
    {
        [StructLayout(LayoutKind.Sequential)]
        private struct COORD
        {
            public short X;
            public short Y;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct CONSOLE_SCREEN_BUFFER_INFO
        {
            public COORD dwSize;
            public COORD dwCursorPosition;
            public short  wAttributes;
            public SMALL_RECT srWindow;
            public COORD dwMaximumWindowSize;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SMALL_RECT
        {
            public short Left;
            public short Top;
            public short Right;
            public short Bottom;
        }

        private const int STD_OUTPUT_HANDLE = -11;

        [DllImport("kernel32.dll")]
        private static extern IntPtr GetConsoleWindow();

        [DllImport("kernel32.dll")]
        private static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput,
            out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

        [DllImport("kernel32.dll")]
        private static extern IntPtr GetStdHandle(int nStdHandle);

        private static CONSOLE_SCREEN_BUFFER_INFO StdOutInfo()
        {
            IntPtr hOut = GetStdHandle(STD_OUTPUT_HANDLE);
            if(hOut != IntPtr.Zero)
            {
                 CONSOLE_SCREEN_BUFFER_INFO info;
                 if(GetConsoleScreenBufferInfo(hOut, out info))
                 {
                     return info;
                 }
            }
            return new CONSOLE_SCREEN_BUFFER_INFO();
        }

        private static Point GetCursorPosition()
        {
            CONSOLE_SCREEN_BUFFER_INFO info = StdOutInfo();
            return new Point(info.dwCursorPosition.X, info.dwCursorPosition.Y);
        }

        private static Point GetConsoleWindowSize()
        {
            CONSOLE_SCREEN_BUFFER_INFO info = StdOutInfo();
            return new Point(info.dwSize.X, info.dwSize.Y);
        }

        /// <summary>
        /// Writes <paramref name="val" /> to standard output with word wrap.
        /// Each line is indented by <paramref name="indent" /> characters,
        /// and is prefixed by the string specified by <paramref name="prefix"/>.
        /// </summary>
        public static void WordWrap(string val, int indent, string prefix)
        {
            int max = (GetConsoleWindowSize()).X;
            string pad = new string(' ', indent)+prefix;

            Regex r = new Regex(@"([\w\.]*(\s)?)");
            Match words = r.Match(val);

            int count = (GetCursorPosition()).X;
            if(count == 0)
            {
                 Console.Write(pad);
            }
            else
            {
                 Console.Write(prefix);
            }
            count = (GetCursorPosition()).X;
            while(words.Success)
            {
                 string word = words.Value;
                 count += word.Length;
                 if(count >= max-1)
                 {
                     Console.WriteLine();
                     Console.Write(pad);
                     count = word.Length + pad.Length;
                 }
                 Console.Write(word);
                 words = words.NextMatch();
            }
        }

        /// <summary>
        /// Writes <paramref name="val"/> to the standard output
        /// with word wrap. Each line is indented by
        /// <paramref name="indent"/> characters.
        /// </summary>
        public static void WordWrap(string val, int indent)
        {
            WordWrap(val, indent, string.Empty);
        }

        /// <summary>
        /// Writes <paramref name="val"/> to the standard output
        /// with word wrap.
        /// </summary>
        public static void WordWrap(string val)
        {
            WordWrap(val,0);
        }

        private Out() { }
    }
}