0

this is a follow on from this question I asked a while ago:

Assembly.GetExecutingAssembly() performance

this solution seemed perfect. Now I've just gottent around to implementing it and it doesn't work. I get a System.TypeInitializationException thrown, the inner exception is the good old, Object reference not set to an instance of an object. Now I'm not sure why it's not working. My guess is that the static readonly property is instanticating before the Assembly class or something?

Can anybody shed any light on why this is happening, any fixes, other than not use a readonly as this is obvious, would also be welcome though not necessarily expected!

Here's the code:

public class VersionHelper
    {
        private static readonly Version _applicationVersion = Assembly.GetEntryAssembly().GetName().Version;
        public static string GetVersionText()
        {
            return string.Format("Version: {0}-{1}", _applicationVersion, Environment.MachineName.Substring(5));
        }
    }

Called:

protected void Page_Load(object sender, EventArgs e)
{
    lblVersion.Text = VersionHelper.GetVersionText();
}

Just to explain if I do it this way it works:

public class VersionHelper
    {
        public static string GetVersionText()
        {
            Assembly web = Assembly.GetExecutingAssembly();
            AssemblyName webName = web.GetName();
            return string.Format("Version: {0}-{1}", webName.Version, Environment.MachineName.Substring(5));
        }
    }
Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190
  • The why not use the version that works? Getting through all the trouble for just a version number... – lboshuizen Oct 19 '12 at 10:31
  • Have you tried adding a static constructor to `VersionHelper` to enforce initialization checks on access? – mlorbetske Oct 19 '12 at 10:35
  • Nope, adding static makes no difference. I wanted to do it this way for performance reasons. I don't want to reflect the assembly everytime someone does anything on a page! Now tbh, I just want to understand why this doesn't work – Liam Oct 19 '12 at 10:44

2 Answers2

2

The exception doesn't have anything to do with the fact that the property is readonly or not. The problem is that you are calling Assembly.GetEntryAssembly() in a ASP.NET context, and apparently that doesn't work well together.

The other option that you have also does not use this method, it uses Assembly.GetExecutingAssembly. If you change your first sample so it uses Assembly.GetExecutingAssembly, than you will see that it runs fine.

I do not have any real references, but you can check this question, and in particular the comments beneath the question.

It also has a solution on how to get an entry assembly in a ASP.NET context.

Community
  • 1
  • 1
Maarten
  • 22,527
  • 3
  • 47
  • 68
-1

Assembly.GetEntryAssembly is not reliable, specifically it will return NULLif the application started in an unmanaged context instead of a managed context. The result must be null checked..

For example, if an unmanaged application creates an instance of a COM component written in C#, a call to the GetEntryAssembly method from the C# component returns NULL, because the entry point for the process was unmanaged code rather than a managed assembly.

whereas GetExecutingAssembly just gets the assembly that contains the code that is currently executing.

while To get the assembly that contains the method that called the currently executing code, you should use GetCallingAssembly.

Rahul Ranjan
  • 1,028
  • 1
  • 10
  • 27
  • @Liam but GetExecutingAssembly() method is much simpler to use..!! – Rahul Ranjan Oct 19 '12 at 10:50
  • Still missing the point, **why** does it fall over when a readonly property? – Liam Oct 19 '12 at 11:01
  • 1
    @Liam I don't feel it is because of the readonly status..It's because of GetEntryAssembly() method..This method do return NULL sometimes unexpectedly..!! Use GetExecutingAssembly in above case and then it will work undoubtedly..!! – Rahul Ranjan Oct 19 '12 at 11:12
  • Turns out it was GetExecutingAssembly, apologies, but you really did not explain it well. It had nothing to do with managed or unmanged code. It was because it was in a web instance. – Liam Oct 19 '12 at 12:08