1

I use code below to read AssemblyTitle attribute of .NET apps, unfortunately Assembly.GetEntryAssembly() always return Null in ASP.NET app. How to read AssemblyTitle in ASP.NET app?

  public static string Title
  {
      get
      {
          var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
          if (attributes.Length > 0)
          {
              var titleAttribute = (AssemblyTitleAttribute)attributes[0];
              if (titleAttribute.Title.Length > 0)
                  return titleAttribute.Title;
          }
          return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase);
      }
  }
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
Tomas
  • 17,551
  • 43
  • 152
  • 257
  • Using your code (http://www.codekeep.net/snippets/170dc91f-1077-4c7f-ab05-8f82b9d1b682.aspx) I can always get the Assembly Title: http://i.stack.imgur.com/b7qGQ.png What are you doing then? WHere are you running that code? – balexandre Sep 06 '11 at 12:51
  • @Balexandre Try to execute code from ASP.NET app hosted on IIS7 – Tomas Sep 06 '11 at 12:58
  • I'm hosting it on **IIS 7.5 Express** and works fine. – balexandre Sep 06 '11 at 15:33
  • Note: assuming it's the first attribute (`attribute[0]`) is not that great. That part can be written in a safer way as: `assembly.GetCustomAttributes()?.Where(x => x is AssemblyTitleAttribute).Select(x => ((AssemblyTitleAttribute) x).Title).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));` – gregmac Sep 30 '16 at 17:07

2 Answers2

4

You must have a type that you know is defined in the same assembly that contains the AssemblyTitle. Then you can do:

typeof(MyType).Assembly.GetCustomAttributes

Note that (for what I know) there isn't any other bulletproof method.

For example using HttpContext.Current doesn't work if you want to do it not during a web request (so you can do it on response of a user action, but not from a separate thread, or from a static initializer, or from global.asax)

Some similar readings (full of half successes):

GetEntryAssembly for web applications

Using the Web Application version number from an assembly (ASP.NET/C#)

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
2

I use the following in asp.net web app:

if (ApplicationDeployment.IsNetworkDeployed)
    return ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

Edit: Sorry, thats just the version, not the title! I combined your version and mine:

System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false); 

That gets the assembly title attribute just fine. The difference is in GetExecutingAssembly() versus your GetEntryAssembly().

w5l
  • 5,341
  • 1
  • 25
  • 43
  • The problem with Assembly.GetExecutingAssembly() is that if code are in class library and referred in ASP.NET, a title will always be from class library and not from ASP.NET app. – Tomas Sep 06 '11 at 13:38