Skip to content

Xamarin .NET Standard 2.0.3 ConfigurationManager throws PlatformNotSupported exception in NHibernateLogger #2105

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

Closed
CSmith1111 opened this issue Apr 5, 2019 · 16 comments · Fixed by #2116

Comments

@CSmith1111
Copy link

CSmith1111 commented Apr 5, 2019

i have a xamarin .net standard 2.0.3 project and try to use nhibernate with sqlite but get the error "System.PlatformNotSupportedException"

The error comes within this code from a .net standard 2.0.3 project:

    var cfg = new Configuration().DataBaseIntegration(db =>
      {
        db.Driver<SQLite20Driver>();
        db.Dialect<SQLiteDialect>();
        db.ConnectionString = "Data Source=test.db;Version=3;New=True";
      });

With just a (xunit) testproject and the nhibernate project this code works fine, but not if i want to start this code within an andorid system.

@hazzik
Copy link
Member

hazzik commented Apr 6, 2019

You'll need to write own driver for SQLite.

@CSmith1111
Copy link
Author

So SQLite is supported for Windows, Linux, MacOS and other .NET Standard platforms but not for Xamarin?

@hazzik
Copy link
Member

hazzik commented Apr 8, 2019

If we're talking about System.Data.SQLite (this is what SQLiteDriver uses) then it supports only Windows, MacOS and Linux. Other platforms are not supported. The SQLite ADO.NET provider which supports Xamarin Android is Mono.Data.Sqlite, which is not supported by NHibernate by default.

You can write own provider for it. Or use this one: https://stackoverflow.com/a/7645822/259946

@CSmith1111
Copy link
Author

CSmith1111 commented Apr 8, 2019

i tried it like you mention it, but the error got thrown even befor nhibernate wants to load the driver.
is there something special to make your linked code work? i also tried to change the nhibernate.cfg.xml but same problem.

      var cfg = new Configuration(); //here comes the error
      cfg.DataBaseIntegration(db =>
      {
        db.Driver<MonoSQLiteDriver>();
        db.Dialect<SQLiteDialect>();
        db.ConnectionString = "Data Source=test.db;Version=3;New=True";
      });

If i create a new dll with just the code from stackoverflow and add <add key="connection.driver_class" value="NHibernate.Driver.ReflectionBasedDriver, SQLDriver.dll" /> to my nhibernate.cfg.xml i get this error:

 NHibernate.HibernateException : Could not create the driver from SQLDriver.MonoSqLiteDriver, SQLDriver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
---- System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
-------- NHibernate.HibernateException : The DbCommand and DbConnection implementation in the assembly Mono.Data.Sqlite could not be found. Ensure that the assembly Mono.Data.Sqlite is located in the application directory or in the Global Assembly Cache. If the assembly is in the GAC, use <qualifyAssembly/> element in the application configuration file to specify the full name of the assembly.

is there any kind of tutorial for this or a fork?

also there comes this error for mono.data.sqlite if i want to use it with .net standard

Error	NU1202	Package Mono.Data.Sqlite 1.0.61 is not compatible with monoandroid90 (MonoAndroid,Version=v9.0). Package Mono.Data.Sqlite 1.0.61 supports: net40 (.NETFramework,Version=v4.0)

@bahusoid

This comment has been minimized.

@CSmith1111

This comment has been minimized.

@hazzik
Copy link
Member

hazzik commented Apr 8, 2019

You don't actualy need any packages. Mono.Data.Sqlite assembly is already part of Mono/Xamarin. You just need to add a reference to the project like this:

<Reference Include="Mono.Data.Sqlite" />

This needed to be included into the App project which targeds Xamarin, not netstandard2.0.

@CSmith1111
Copy link
Author

CSmith1111 commented Apr 9, 2019

NHibernateSQLiteTest.zip
This is my sample project, a basic cross-platform project with nhibernate and all of your hints.
I can't get it work.
Can you please take a look and say me whats the problem?

If i move the Ref to Mono.Data.SQLite from NHibernateSQLiteTest to NHibernateSQLiteTest.Android there is no diffrent

@hazzik
Copy link
Member

hazzik commented Apr 9, 2019

Ok, this is the ConfigurationManager throws PlatformNotSupported exception (in NHibernateLogger)


System.TypeInitializationException: The type initializer for 'NHibernate.NHibernateLogger' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
  at System.Configuration.ClientConfigPaths..ctor (System.String exePath, System.Boolean includeUserConfig) [0x00050] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.ClientConfigPaths.GetPaths (System.String exePath, System.Boolean includeUserConfig) [0x00018] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.ClientConfigurationHost.get_ConfigPaths () [0x0000a] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.ClientConfigurationHost.GetStreamName (System.String configPath) [0x0006d] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp () [0x00000] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp () [0x00006] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.ClientConfigurationSystem..ctor () [0x00051] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.ConfigurationManager.EnsureConfigurationSystem () [0x00024] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
   --- End of inner exception stack trace ---
  at System.Configuration.ConfigurationManager.EnsureConfigurationSystem () [0x00060] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.ConfigurationManager.PrepareConfigSystem () [0x0000a] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.ConfigurationManager.GetSection (System.String sectionName) [0x0000a] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at System.Configuration.ConfigurationManager.get_AppSettings () [0x00000] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0 
  at NHibernate.NHibernateLogger.GetNhibernateLoggerClass () [0x00000] in <6a21298c8bf24583b7926240093786b7>:0 
  at NHibernate.NHibernateLogger..cctor () [0x00000] in <6a21298c8bf24583b7926240093786b7>:0 
   --- End of inner exception stack trace ---
  at NHibernateSQLiteTest.NHibernateHelper.MakeCfg () [0x00001] in /Users/hazzik/Downloads/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateHelper.cs:22 
  at NHibernateSQLiteTest.NHibernateHelper.get_Configuration () [0x0000e] in /Users/hazzik/Downloads/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateHelper.cs:79 
  at NHibernateSQLiteTest.NHibernateHelper.get_SessionFactory () [0x0000e] in /Users/hazzik/Downloads/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateHelper.cs:65 
  at NHibernateSQLiteTest.NHibernateHelper.OpenSession () [0x00001] in /Users/hazzik/Downloads/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateHelper.cs:55 
  at NHibernateSQLiteTest.NHibernateSqLiteDummyRepository.Save (NHibernateSQLiteTest.SQLiteDummy SQLiteDummy) [0x00001] in /Users/hazzik/Downloads/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateSqLiteDummyRepository.cs:20 
  at NHibernateSQLiteTest.MainPage.OnClick (System.Object sender, System.EventArgs e) [0x00019] in /Users/hazzik/Downloads/NHibernateSQLiteTest/NHibernateSQLiteTest/NHibernateSQLiteTest/MainPage.xaml.cs:17 
  at Xamarin.Forms.Button.Xamarin.Forms.Internals.IButtonElement.PropagateUpClicked () [0x00000] in D:\\a\\1\\s\\Xamarin.Forms.Core\\Button.cs:166 
  at Xamarin.Forms.ButtonElement.ElementClicked (Xamarin.Forms.VisualElement visualElement, Xamarin.Forms.Internals.IButtonElement ButtonElementManager) [0x0001f] in D:\\a\\1\\s\\Xamarin.Forms.Core\\ButtonElement.cs:61 
  at Xamarin.Forms.Button.SendClicked () [0x00000] in D:\\a\\1\\s\\Xamarin.Forms.Core\\Button.cs:152 
  at Xamarin.Forms.Platform.Android.ButtonElementManager.OnClick (Xamarin.Forms.VisualElement element, Xamarin.Forms.IButtonController buttonController, Android.Views.View v) [0x00000] in D:\\a\\1\\s\\Xamarin.Forms.Platform.Android\\ButtonElementManager.cs:25 
  at Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer.Android.Views.View.IOnClickListener.OnClick (Android.Views.View v) [0x00000] in D:\\a\\1\\s\\Xamarin.Forms.Platform.Android\\AppCompat\\ButtonRenderer.cs:185 
  at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x00011] in <d9e99b55e96444569d54148253f9c212>:0 
  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.32(intptr,intptr,intptr)

@CSmith1111
Copy link
Author

CSmith1111 commented Apr 9, 2019

yes, that's the problem in this issue.
that's why i should change to mono, but as you can see, its not that easy.

any idea who i can make nhiberate to get working with mono in this kind of project?

@xp-development
Copy link

xp-development commented Apr 10, 2019

Is the ConfigurationManager not usable for Xamarin.Android?
Does that mean it's not possible to use NHibernate for Xamarin.Android?

@hazzik
Copy link
Member

hazzik commented Apr 11, 2019

@bahusoid

This comment has been minimized.

@CSmith1111

This comment has been minimized.

@hazzik

This comment has been minimized.

@hazzik hazzik changed the title Xamarin .NetStandard2.0.3 SQLite: System.PlatformNotSupportedException: Operation is not supported on this platform. Xamarin .NET Standard 2.0.3 ConfigurationManager throws PlatformNotSupported exception (in NHibernateLogger) Oct 16, 2019
@hazzik hazzik changed the title Xamarin .NET Standard 2.0.3 ConfigurationManager throws PlatformNotSupported exception (in NHibernateLogger) Xamarin .NET Standard 2.0.3 ConfigurationManager throws PlatformNotSupported exception in NHibernateLogger Oct 16, 2019
hazzik pushed a commit that referenced this issue Oct 16, 2019
…#2116)

- Introduced abstract class ConfigurationProvider exposed as ConfigurationProvider.Current.

To skip all configuration calls (fix for Xamarin) just set it to null:

    ConfigurationProvider.Current = null;

Fixes #2105
@hazzik
Copy link
Member

hazzik commented Oct 16, 2019

On version 5.3 and above to skip all configuration calls set ConfigurationProvider.Current to null:

ConfigurationProvider.Current = null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants