Assembly.GetEntryAssembly() does not work for web applications.
But... I really need something like that. I work with some deeply-nested code that i
The only way I was able to get this to work consistently for Web Applications (at least in .NET 4.5.1) was to do the Assembly.GetExecutingAssembly() in the Web Application project itself.
If you try to create a utilities project with static methods and do the call there, you will get the assembly information from that assembly instead - for both GetExecutingAssembly() and GetCallingAssembly().
GetExecutingAssembly() is a static method that returns an instance of the Assembly type. The method does not exist on an instance of the Assembly class itself.
So, what I did was created a class that accepts Assembly type in the constructor, and created an instance of this class passing the results from Assembly.GetExecutingAssembly().
public class WebAssemblyInfo
{
Assembly assy;
public WebAssemblyInfo(Assembly assy)
{
this.assy = assy;
}
public string Description { get { return GetWebAssemblyAttribute(a => a.Description); } }
// I'm using someone else's idea below, but I can't remember who it was
private string GetWebAssemblyAttribute(Func value) where T : Attribute
{
T attribute = null;
attribute = (T)Attribute.GetCustomAttribute(this.assy, typeof(T));
if (attribute != null)
return value.Invoke(attribute);
else
return string.Empty;
}
}
}
And to use it
string Description = new WebAssemblyInfo(Assembly.GetExecutingAssembly()).Description;