Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration system throws `PlatformNotSupportedException` on Xamarin Android. #1151

Closed
borrrden opened this issue Sep 25, 2019 · 13 comments
Closed
Labels
bug

Comments

@borrrden
Copy link

@borrrden borrrden commented Sep 25, 2019

Description

Configuration system throws PlatformNotSupportedException on Xamarin Android.

Complete minimal example reproducing the issue

3.Should().BeGreaterThan(4); // Probably any failing test?

Expected behavior:

The normal assertion failure process

Actual behavior:

The following Exception is thrown:

System.PlatformNotSupportedException : Operation is not supported on this platform.
 		  at System.Configuration.ConfigurationManager.PrepareConfigSystem () [0x0001a] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 
 		  at System.Configuration.ConfigurationManager.GetSection (System.String sectionName) [0x0000a] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 
 		  at System.Configuration.ConfigurationManager.get_AppSettings () [0x00000] in <4cd8a957e7664f4a9e27ce43dd1afeb7>:0 
		  at FluentAssertions.Common.AppSettingsConfigurationStore.GetSetting (System.String name) [0x00000] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Common.Configuration.get_ValueFormatterAssembly () [0x00008] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Common.Configuration.DetermineFormatterDetectionMode () [0x00000] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Common.Configuration.get_ValueFormatterDetectionMode () [0x0001e] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Formatting.AttributeBasedFormatter.get_IsScanningEnabled () [0x00005] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Formatting.AttributeBasedFormatter.CanHandle (System.Object value) [0x00000] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Formatting.Formatter+<>c__DisplayClass7_0.<Format>b__0 (FluentAssertions.Formatting.IValueFormatter f) [0x00000] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at System.Linq.Enumerable.TryGetFirst[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate, System.Boolean& found) [0x0003f] in <15cecf952ca44136ad5b83afa233f6f3>:0 
 		  at System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) [0x00000] in <15cecf952ca44136ad5b83afa233f6f3>:0 
 		  at FluentAssertions.Formatting.Formatter.Format (System.Object value, FluentAssertions.Formatting.FormattingContext context, FluentAssertions.Formatting.FormatChild formatChild) [0x00012] in <d52b982f9eb24747b8fd904dff0631fe>:0 
		  at FluentAssertions.Formatting.Formatter.ToString (System.Object value, System.Boolean useLineBreaks) [0x00054] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Execution.MessageBuilder.<FormatArgumentPlaceholders>b__6_0 (System.Object a) [0x00000] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at System.Linq.Enumerable+SelectArrayIterator`2[TSource,TResult].ToArray () [0x00012] in <15cecf952ca44136ad5b83afa233f6f3>:0 
		  at System.Linq.Enumerable.ToArray[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x0001f] in <15cecf952ca44136ad5b83afa233f6f3>:0 
 		  at FluentAssertions.Execution.MessageBuilder.FormatArgumentPlaceholders (System.String failureMessage, System.Object[] failureArgs) [0x00012] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Execution.MessageBuilder.Build (System.String message, System.Object[] messageArgs, System.String reason, FluentAssertions.Execution.ContextDataItems contextData, System.String identifier, System.String fallbackIdentifier) [0x00036] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Execution.AssertionScope+<>c__DisplayClass30_0.<FailWith>b__0 () [0x00055] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Execution.AssertionScope.FailWith (System.Func`1[TResult] failReasonFunc) [0x0001a] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Execution.AssertionScope.FailWith (System.Func`1[TResult] failReasonFunc) [0x00014] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Execution.AssertionScope.FailWith (System.String message, System.Object[] args) [0x00014] in <d52b982f9eb24747b8fd904dff0631fe>:0 
 		  at FluentAssertions.Numeric.NumericAssertions`1[T].BeGreaterThan (T expected, System.String because, System.Object[] becauseArgs) [0x0004c] in <d52b982f9eb24747b8fd904dff0631fe>:0 

Versions

FluentAssertions 5.9.0
Xamarin.Android SDK 9.1.7.0 (HEAD/ba9da7a76)
Emulator 27.3.9-4899998 API 27 (x86)

@dennisdoomen dennisdoomen added the bug label Sep 25, 2019
@dennisdoomen

This comment has been minimized.

Copy link
Member

@dennisdoomen dennisdoomen commented Sep 25, 2019

Which platform-variant of FA is it picking up? You can determine that from the folder name that points to the DLL from your project.

@borrrden

This comment has been minimized.

Copy link
Author

@borrrden borrrden commented Sep 25, 2019

I'm not quite sure what that means, but dotPeek tells me it is the .NET Standard 2.0 variant.

@borrrden

This comment has been minimized.

Copy link
Author

@borrrden borrrden commented Sep 25, 2019

It looks like ConfigurationManager is not supported on Xamarin or UWP

The recommendation from the Xamarin Forms team is to use Xamarin.Essentials.Preferences instead.

@dennisdoomen

This comment has been minimized.

Copy link
Member

@dennisdoomen dennisdoomen commented Sep 25, 2019

Yeah, strangely it does compile, so something must have changed. We'll need to fix that.

@borrrden

This comment has been minimized.

Copy link
Author

@borrrden borrrden commented Sep 25, 2019

I imagine it compiles because of the odd Microsoft policy that it is acceptable for things to be part of the .NET Standard, but then throw not supported exceptions on platforms that are not applicable....

@jnyrup

This comment has been minimized.

Copy link
Collaborator

@jnyrup jnyrup commented Oct 25, 2019

Some different possibilities.

  1. Wrap calls to Configuration.AppSettings in try/catch.
    • Easiest to implement, but still leaves Xamarin users without the ability to set settings.
  2. Somehow detect that Configuration.AppSettings is unavailable and avoid invoking it.
    • Much like 1) but avoids the exception control flow
  3. Add target frameworks for xamarin/uwp and use the suggested Preferences.
    • Adds more target frameworks to support...
  4. Remove references to System.Configuration and expose the three settings as properties on e.g. a static Settings class.
    • pro: Fewer dependencies
    • pro: Will support Xamarin
    • con: Breaking change
@dennisdoomen

This comment has been minimized.

Copy link
Member

@dennisdoomen dennisdoomen commented Oct 26, 2019

Or we keep the current Configuration abstraction, but introduce an in-memory decorator that intercepts any exceptions while accessing the AppSettingsConfigurationStore and which falls back to local state,

@SteveBush

This comment has been minimized.

Copy link

@SteveBush SteveBush commented Nov 5, 2019

A variation on jnyrup suggestions would be in the set the ConfigurationStore to a NullConfigurationStore(); if accessing a setting throws an exception.

pros:

  • ConfigurationStore is available to platforms that support it.
  • Exception handling only on assignment of ConfigurationStore
  • Unblocks Android users :)

cons:

  • Unsupported platforms cannot set settings

#if NETSTANDARD1_3
    Reflector = new NullReflector();
    ConfigurationStore = new NullConfigurationStore();
#elif NETSTANDARD1_6
    Reflector = new NetStandardReflector();
    ConfigurationStore = new NullConfigurationStore();
#else
    Reflector = new FullFrameworkReflector();
    ConfigurationStore = new AppSettingsConfigurationStore();

// try to use the ConfigurationStore, catch any exceptions 
// and set ConfigurationStore to NullConfigurationStore(); on exception.  

#endif
@dennisdoomen

This comment has been minimized.

Copy link
Member

@dennisdoomen dennisdoomen commented Nov 5, 2019

That's exactly what I proposed as well ;-)

@jnyrup jnyrup added this to the 6.0 milestone Nov 18, 2019
@akamud

This comment has been minimized.

Copy link

@akamud akamud commented Dec 26, 2019

The fix is exactly what I did for my project.

@dennisdoomen

This comment has been minimized.

Copy link
Member

@dennisdoomen dennisdoomen commented Dec 26, 2019

@jnyrup why was it moved to 6.0? You're envisioning a breaking change?

@jnyrup

This comment has been minimized.

Copy link
Collaborator

@jnyrup jnyrup commented Dec 26, 2019

I put this in the 6.0 milestone to make sure we at latest fix it there.
I haven't really thought about whether it will introduce any breaking changes.

jnyrup added a commit to jnyrup/fluentassertions that referenced this issue Dec 29, 2019
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 fluentassertions#1207 and fluentassertions#1151
jnyrup added a commit to jnyrup/fluentassertions that referenced this issue Dec 29, 2019
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 fluentassertions#1207 and fluentassertions#1151
jnyrup added a commit to jnyrup/fluentassertions that referenced this issue Dec 29, 2019
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 fluentassertions#1207 and fluentassertions#1151
@jnyrup

This comment has been minimized.

Copy link
Collaborator

@jnyrup jnyrup commented Jan 2, 2020

Fixed in #1210, which is included in 5.10.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.