From eaa952f5115955c527dbaf03fa0f8f3df2e427ba Mon Sep 17 00:00:00 2001 From: jnyrup Date: Sun, 29 Dec 2019 15:45:09 +0100 Subject: [PATCH] Intercept Reflection exceptions from `AppSettingsConfigurationStore` Some platforms throws reflection exceptions when trying to use `ConfigurationManager`. This does not fix the underlying problem of not being unable to use `ConfigurationManager`, but returns `null` instead of an exception. Relates to #1207 and #1151 --- .../ConfigurationStoreExceptionInterceptor.cs | 36 +++++++++++++++++++ Src/FluentAssertions/Common/Services.cs | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 Src/FluentAssertions/Common/ConfigurationStoreExceptionInterceptor.cs diff --git a/Src/FluentAssertions/Common/ConfigurationStoreExceptionInterceptor.cs b/Src/FluentAssertions/Common/ConfigurationStoreExceptionInterceptor.cs new file mode 100644 index 0000000000..fe06927a06 --- /dev/null +++ b/Src/FluentAssertions/Common/ConfigurationStoreExceptionInterceptor.cs @@ -0,0 +1,36 @@ +#if !NETSTANDARD1_3 && !NETSTANDARD1_6 + +namespace FluentAssertions.Common +{ + internal class ConfigurationStoreExceptionInterceptor : IConfigurationStore + { + private bool underlyingStoreUnavailable; + + private readonly IConfigurationStore configurationStore; + + public ConfigurationStoreExceptionInterceptor(IConfigurationStore configurationStore) + { + this.configurationStore = configurationStore; + } + + public string GetSetting(string name) + { + if (underlyingStoreUnavailable) + { + return null; + } + + try + { + return configurationStore.GetSetting(name); + } + catch + { + underlyingStoreUnavailable = true; + return null; + } + } + } +} + +#endif diff --git a/Src/FluentAssertions/Common/Services.cs b/Src/FluentAssertions/Common/Services.cs index cfc2f745dc..43351e1dec 100644 --- a/Src/FluentAssertions/Common/Services.cs +++ b/Src/FluentAssertions/Common/Services.cs @@ -48,7 +48,7 @@ public static void ResetToDefaults() ConfigurationStore = new NullConfigurationStore(); #else Reflector = new FullFrameworkReflector(); - ConfigurationStore = new AppSettingsConfigurationStore(); + ConfigurationStore = new ConfigurationStoreExceptionInterceptor(new AppSettingsConfigurationStore()); #endif ThrowException = TestFrameworkProvider.Throw;