Westwind.Globalization icon indicating copy to clipboard operation
Westwind.Globalization copied to clipboard

No Error in provider.GetObject reported when connection is wrong

Open michael-freidgeim-webjet opened this issue 7 years ago • 2 comments

In our code we running provider.GetObject as here:

  private static readonly ConcurrentDictionary<string, IResourceProvider> ResourceProviderCache = new ConcurrentDictionary<string, IResourceProvider>();
 private static readonly DbResourceProviderFactory ResourceFactory = new DbResourceProviderFactory();
    

   private string GetGlobalResourceStringInternal(string classKey, string resourceKey, CultureInfo culture = null)
        {
            IResourceProvider provider = ResourceProviderCache.GetOrAdd(classKey, ResourceFactory.CreateGlobalResourceProvider(classKey));
            object obj2 = provider.GetObject(resourceKey, culture);
            string str = obj2 as string;
              return str;
        }

When connection string is wrong (refers to non-existing database) we expected some exception throws or at least logged, but provider.GetObject returned back resourceKey and even VS debug output doesn't have any clue. 1.Is any configuration available to force logging/throwing exception?

2.Is it possible for GetObject ,if record is not found, to return null instead of resourceKey ? I found a workaround to explicitly specify culture, but is anything better exists?

  1. Why value changed from null to resourceKey only if culture is not null and not the invariant culture ?

I am running into a similar issue. The DbResourceDataManager may log errors using SetError(), but these errors don't seem to get logged or to be easily accessible from the outside.

torhovland avatar Nov 21 '18 08:11 torhovland

That's more or less on purpose as it matches the behavior of Resources in .NET. If resources are missing or failing, resources return the defaults (ie. no value with Resx - but DbRes will return the value passed in). So silently failing is the right behavior I think.

If you really need to trap this, you can make a call to the DbRes provider (during startup) and check for value that should be there and if that fails you can be sure that the db is not working. Maybe we can add a helper for this explicitly - like DbResourceManager.IsDatabaseWorking() or something along those lines.

RickStrahl avatar May 29 '19 10:05 RickStrahl