Practicle usage of Ngen  
Author Message
Amit Bansal





PostPosted: Common Language Runtime, Practicle usage of Ngen Top

I am searching some good practicle examples to use the ngen utility in my application.If someone can provide me the link to some code ir just some really helpful articles...........

Actally i am confused about the assembly refrence added to application and how an application resolve that reference.


1) If we have a native image of our assembly then how can we refer to it
2) As we have to installed our assembly in two folders ("assembly" and "public assembly") to make it sharable and to add its ref through IDE then to which assembly it will use during execution

If it uses the assembly in the "Public Assembly" folder then what is the need to install
it in "assembly"(GAC)

3) Which assemblies are really used during the application execution


.NET Development28  
 
 
Pracheeti Nagarkar - MS





PostPosted: Common Language Runtime, Practicle usage of Ngen Top

Amity,

1. You cannot refer to the native image of an assembly directly. As long as your application A.exe has a reference to its dependency B.dll, and a native image for B.dll exists, the Common Language Runtime loader will probe and find the native image and use it at runtime. In order to create a native image for B.dll, there is currently no Visual Studio IDE support that lets you do this. You have to call the ngen.exe tool directly from the command line. To create a native image for A.exe (and all of its dependencies), run the command "C:\Windows\Microsoft.NET\Framework\v2.0.50727\ngen.exe install A.exe".

2. The Public Assembly folder in the Visual Studio IDE contains assemblies that you would like to easily/quickly reference from multiple VS projects. This is not tied to the GAC in any way. The Public Assembly folder is a Visual Studio concept created I imagine to make development easier, while the GAC is a Common Language Runtime concept intended to allow developers to share their assemblies across multiple applications running on the machine. Adding an assembly into the GAC makes it shareable in a different sense - different applications that want to use the same assembly at runtime can now do so, instead of the assembly being stored in a directory "local" to just one application. If you are a library writer and you want your library to be available to multiple applications, you can choose to place it in the GAC. If you also want to make it easier for developers who use Visual Studio to reference your library, you add it to the Public Assembly list.

KB article on how to add an assembly reference to the Public Assembly list in VS - http://support.microsoft.com/default.aspx scid=kb%3ben-us%3b306149

3. At runtime, an assembly can either get used a) from its path location if it is not GAC'd or NGEN'd or b) the GAC if it is installed in the GAC and not NGEN'd , or c) the NGEN cache if there is a native image for the assembly. Note that you can create an NGEN image for the assembly even if the assembly is not GAC'd. In order to tell where the assembly actually got loaded from at runtime, there is a useful tool that lets you see this called FusLogVw.exe. It comes with the .NET Framework SDK (SDK\Bin\Fuslogvw.exe). In the Log Settings, make sure you set "Log All Binds To Disk". When you run your app, and then look at the "Native Images" Log category, there should be an entry for your assembly, that indicates whether native image for the assembly was used or not.

For instance, most apps use mscorlib.dll which has a native image. The corresponding Fusion log entry looks like -



The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from: C:\WINDOWS\Microsoft.NET\Framework\v2.0.61026\mscorwks.dll
Running under executable C:\VBL\CLRPOR\QA\CLR\TestBin\loader\Ngen\Tool\TestApps\App5\app.exe
--- A detailed error log follows.

LOG: Start binding of native image mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
LOG: Start validating native image mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
LOG: Bind to native image succeeded.

The following articles/documentation will be helpful to you -

1. NGEN Command Line help - http://msdn2.microsoft.com/en-us/library/6t9t5wcf.aspx

2. New NGEN features in .NET Framework 2.0 - http://msdn.microsoft.com/msdnmag/issues/05/04/NGen/

3. Performance Benefits of Using NGEN - http://msdn.microsoft.com/msdnmag/issues/06/05/CLRInsideOut/

Hope this helps,

Pracheeti