I have a reporting service that reported the service's version. For normal services I can use Assembly.GetEntryAssembly()
, but this doesn't work for Azure roles. How can I detect the version of the role?
In a shared assembly, I have defined the following attribute:
[AttributeUsage(AttributeTargets.Assembly)]
public sealed class EntryAssemblyAttribute : Attribute
{
}
The same assembly also contains a helper class that determines the entry assembly and its version:
public static class EntryAssemblyHelper
{
public static bool IsEntryAssembly(this Assembly assembly)
{
return assembly.GetCustomAttributes(typeof(EntryAssemblyAttribute), false).Any();
}
public static Assembly GetEntryAssembly()
{
return AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(IsEntryAssembly);
}
public static Version GetEntryAssemblyVersion()
{
return GetEntryAssembly()?.GetName().Version;
}
}
All my root assemblies now use the following line in the AssemblyInfo.cs
:
[assembly: EntryAssembly]
Although it requires some custom code it works well if all the entry assemblies in your system are your own. The major benefit of this solution is that it is unrelated to the bootstrapper, so it works with ASP.NET, Cloud Services and normal applications.