19

I'm working on a large-ish web application built in C# (asp.net). I've got a simple aspx page that serves localized strings to the client browser for use in javascript controls. To get the strings, I do the following:

ResourceManager _resources = new ResourceManager(_pathname, typeof(ARM).Assembly);
ResourceSet rs = _resources.GetResourceSet(culture, false, false);

//loop through rs and write the keys & values out to the client in plaintext

This all works fine, except for the first request to the page immediately after a Clean/Build or a Rebuild (if I simply make some changes, then Build, it works fine). So on the first request I get a null reference exception when I try to iterate the ResourceSet. If I refresh the page after the error, however, it works fine from then on.

Does anyone know why this might be happening?

Cole F
  • 191
  • 1
  • 1
  • 4

1 Answers1

52

Second param "createIfNotExist" of the method GetResourceSet has to be true, that tells ResourceManager to load the ResourceSet if not yet loaded.

ResourceSet rs = _resources.GetResourceSet(culture, true, false);
Aleksandar Mirilovic
  • 1,298
  • 1
  • 11
  • 11
  • 5
    Nice answer, the createIfNotExists put me on the wrong foot. (Perhaps Microsoft should think of renaming the parameter name, loadIfNotPresent) – Blaatz0r Aug 01 '18 at 15:15
  • 1
    We have a #TenYearsChallenge here. In **2009** the second parameter was **createIfNotExist**, In **2019** the second parameter is **createIfNotExist** !!! wasted two complete days on this naming convention ... – Seyed Arman Fatemi Oct 25 '19 at 19:58
  • Argh the pain! Thanks for this - Mircosofts really need to change this paramater name! – John Jul 14 '20 at 11:39
  • Amazing how this name is still an issue in .NET 7, thanks for this answer. The default naming is atrocious! – Axemasta Nov 17 '22 at 22:56