Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Reflection.Assembly.GetExecutingAssembly() always returns 0.0.0.0

I have modified my assemblyinfo.cs with this:

[assembly: AssemblyVersion("1.0.*")]

and on the _layout.cshtml I put this so I can recognize in which build I am testing:

 @System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()

it always prints 0.0.0.0

When I go to the bin folder and check the dll properties, version says 1.0.343.2323 so I dont get it?

like image 484
Luis Valencia Avatar asked Apr 13 '13 21:04

Luis Valencia


People also ask

What is Assembly GetExecutingAssembly ()?

In C#, GetExecutingAssembly() method is the method of Assembly class. This method returns the assembly that contains the code that is currently executing. To use this method we have to use System. Reflection in our program.

What is System reflection Assembly?

Namespace: System.Reflection. Summary. Defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.

How do you access the assembly of a running application?

The recommended way to retrieve an Assembly object that represents the current assembly is to use the Type. Assembly property of a type found in the assembly, as the following example illustrates. To get the assembly that contains the method that called the currently executing code, use GetCallingAssembly.

What is the method to load assembly by name?

Loads an assembly given its AssemblyName. The assembly is loaded into the domain of the caller using the supplied evidence. Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller.


2 Answers

Assembly name shown is of some runtime assembly loaded by IIS, instead use EntryAssembly if entry point for your application resides in the assembly for which you have updated the AssemblyInfo -

Assembly.GetEntryAssembly().GetName().Version.ToString();

In case it's different than the calling assembly you can do this way -

Assembly.GetAssembly(typeof(YourAssembly.AnyClass)).GetName().Version.ToString();
like image 172
Rohit Vats Avatar answered Oct 27 '22 00:10

Rohit Vats


Looks like the executing assembly is not the one you've compiled, but a dynamic assembly created by IIS.

Try using

typeof(Your_Type_From_NonWeb_Assembly).Assembly.GetName().Version.ToString()
like image 28
alex Avatar answered Oct 27 '22 01:10

alex