none
NGen Questions #1install assembly while it is running - #2 recognize invalid native assemblies RRS feed

  • Question

  • Hi, I would like to use NGen to pre-compile my application (during the installation process). I already read a lot about that. But there are some questions left.

    After I have installed the native image of my assembly in the cache - the image will be executed when I start the app.
    But what happens if the native image becomes invalid for some reason?

    #1: How can my program find out it is not running as native in the assembly cache?

    #2: Is it possible to start "ngen update <myApp>" while <myApp> is running? (Will that work?)

    I hope someone knows the answers to this questions and hopefully that person is so kind to share his knowledge.
    Thanks, Morris
    Thursday, April 16, 2009 1:34 PM

Answers

  • Hello,
    I am not expert on ngen, but I'll try to asnwer what I know:

    #1: Your program should not programatically check this. When you setup new libraries or new version of your applications, then you should run the ngen command (ngen install) during this setup. Ngen will take care of the rest. User (your program) is never supposed to look after it. If you still feel the need to check for ngen images, then please describe your scenario in more details and please explain what are you trying to achieve.

    If you need to just personally verify for yourself that you are using ngen image (and that your setup ran correctly), then check out the list of loaded DLLs. All ngen images have extension .ni before .dll/.exe (e.g. MyLibrary.ni.dll). You can see the list of DLLs under debugger in VS Modules window (however .NET Framework ngen images are filtered out, but it should work for you application according to this blog post), or in windbg debugger (command lm), or in Process Explorer or using listdlls utility.

    #2: Yes, it is possible, but I don't think it will work as you expect. The command 'ngen update' will do nothing unless you ran 'ngen install' before it. Again I might not fully understand your scenario, so please post more details if you feel that you really need to run 'ngen update' excplicitly.

    Thanks,
    -Karel
    • Marked as answer by Zhi-Xin Ye Wednesday, April 22, 2009 11:21 AM
    Thursday, April 16, 2009 4:59 PM
    Moderator

All replies

  • Hello,
    I am not expert on ngen, but I'll try to asnwer what I know:

    #1: Your program should not programatically check this. When you setup new libraries or new version of your applications, then you should run the ngen command (ngen install) during this setup. Ngen will take care of the rest. User (your program) is never supposed to look after it. If you still feel the need to check for ngen images, then please describe your scenario in more details and please explain what are you trying to achieve.

    If you need to just personally verify for yourself that you are using ngen image (and that your setup ran correctly), then check out the list of loaded DLLs. All ngen images have extension .ni before .dll/.exe (e.g. MyLibrary.ni.dll). You can see the list of DLLs under debugger in VS Modules window (however .NET Framework ngen images are filtered out, but it should work for you application according to this blog post), or in windbg debugger (command lm), or in Process Explorer or using listdlls utility.

    #2: Yes, it is possible, but I don't think it will work as you expect. The command 'ngen update' will do nothing unless you ran 'ngen install' before it. Again I might not fully understand your scenario, so please post more details if you feel that you really need to run 'ngen update' excplicitly.

    Thanks,
    -Karel
    • Marked as answer by Zhi-Xin Ye Wednesday, April 22, 2009 11:21 AM
    Thursday, April 16, 2009 4:59 PM
    Moderator
  • Hi, I would like to use NGen to pre-compile my application (during the installation process). I already read a lot about that. But there are some questions left.

    After I have installed the native image of my assembly in the cache - the image will be executed when I start the app.
    But what happens if the native image becomes invalid for some reason?

    #1: How can my program find out it is not running as native in the assembly cache?

    #2: Is it possible to start "ngen update <myApp>" while <myApp> is running? (Will that work?)

    I hope someone knows the answers to this questions and hopefully that person is so kind to share his knowledge.
    Thanks, Morris

    #1:  There is a pretty straightforward way to do this outlined in an answer to this very question on Stack Overflow.

    #2:  Yes.  I developed a PoC that runs "ngen install <myApp>" on the <myApp>'s first run (i.e. while <myApp> is running), and it works well.  If "ngen install <myApp>" has been run previously, running "ngen update <myApp>" should work regardless whether <myApp> is running.


    J0e3gan http://stackoverflow.com/users/1810429/j0e3gan


    • Edited by Joe Egan Tuesday, May 20, 2014 4:28 AM cleanup
    Monday, May 19, 2014 7:36 PM
  • Re: #1 on StackOverflow. That depends on undocumented stuff, so don't be surprised if some future version of .NET will break your code.

    -Karel

    Monday, May 19, 2014 8:25 PM
    Moderator
  • Re: #1 on StackOverflow. That depends on undocumented stuff, so don't be surprised if some future version of .NET will break your code.

    -Karel

    The ".ni." filename format was clearly explained in "MSDN Magazine" when NGen was updated with the release of .NET 2.0; and it is clearly explained elsewhere online.   The format has been stable for nearly 10 years.

    Sure, it could change; but the change would come with similar same attention to it - maybe not in the official NGen MSDN page but somewhere; and evaluating such breaking changes is part of responsibly moving to new releases of the .NET Framework generally.  Although not completely unfounded, the concern just sounds a little overstated to me.

    Even so, executing ngen.exe itself from C# code would do the trick - per the command-line solution in the same SO answer.  Of course, that could change too if "ngen.exe display <myApp>" stops returning 0 when it finds a native image for <myApp> and -1 when it doesn't.


    J0e3gan http://stackoverflow.com/users/1810429/j0e3gan


    • Edited by Joe Egan Tuesday, May 20, 2014 4:29 AM cleanup
    Tuesday, May 20, 2014 3:24 AM