Remoting Configuration Loaded Too Little Too Late

Arogl Darthu's Blog

  • Homepage

Sep 21: Remoting Configuration Loaded Too Little Too Late

How strange... The program works perfectly in debug mode, but in release mode I get a TypeInitializationException. Why?

The exception originates at the construction of an object (BatchRunner) that tries to instantiate a remotable object. This remotable object is declared as a private static variable:

#region Private Static Fields
private static RemotingWrapper Wrapper = CreateRemotingWrapper();
#endregion
 

And herein lies the problem. The call to CreateRemotingWrapper() fires when the implicit static constuctor is called. In release mode the compiler makes certain optimizations that it does not make in debug mode. Apparently causing implicit static constructors to execute very, very early.
My console apps main method looks like this:

[STAThread]
static void Main(string[] args)
{
  Console.WriteLine("Reading remoting configuration.");
  RemotingConfiguration.Configure(
    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

  BatchRunner batchRunner = new BatchRunner();
  batchRunner.Execute();

  Console.WriteLine("Program finished.");
  Console.ReadLine();
}
 

One would say that the remoting configuration is read way before the rest of the program executes. This is both true and false... True in debug mode, false in release mode :-(. Because the BatchRunner contains a static field, its implicit static constructor fires before the call to RemotingConfiguration.Configure. This being the case, CreateRemotingWrapper() executes without a proper remoting configuration. Consequently, the check to see if the object obtained by calling Activator.CreateInstance(typeof(RemotingWrapper)) is a TransparentProxy fails and throws a RemotingException (see next code fragment).

#region Private Static Methods
private static RemotingWrapper CreateRemotingWrapper()
{
  object remoteObject = Activator.CreateInstance(typeof(RemotingWrapper));
  if (!RemotingServices.IsTransparentProxy(remoteObject))
  {
    throw new RemotingException(
      "Could not obtain transparent proxy. Check your remoting configuration.");
  }
  else
  {
    return (RemotingWrapper)remoteObject;
  }
}
#endregion
 

Now what I don't quite understand is why the RemotingException does not occur right at the start of my program... Why do I have to wait until the BatchRunner is instantiated, causing a TypeInitializationException as a result of the thrown RemotingException?!
Posted by Twan Jacobs in WTF Comments: (0) Trackbacks: (0)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

No comments


Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 

Calendar

Back February '12
Mo Tu We Th Fr Sa Su
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        

Archives

  • February 2012
  • January 2012
  • December 2011
  • Recent...
  • Older...

Categories

  • XML BizTalk
  • XML Daily Didst
  • XML Infra
  • XML SQL
  • XML WCF
  • XML WTF


All categories

Syndicate This Blog

  • XML RSS 2.0 feed
  • XML RSS 2.0 Comments

Blog Administration

Open login screen

Powered by

Serendipity PHP Weblog
 

Layout by Andreas Viklund | Serendipity template by Carl