/// <summary> /// A factory method for creating an app with a particular <see cref="AppConfiguration"/>. /// </summary> /// <param name="config">The <see cref="AppConfiguration"/>, specifying key parameters for the app behavior.</param> /// <returns>An <see cref="App"/> instance can now be used to login users, call functions, or open synchronized Realms.</returns> public static App Create(AppConfiguration config) { Argument.NotNull(config, nameof(config)); if (config.MetadataPersistenceMode.HasValue) { if (config.MetadataPersistenceMode == MetadataPersistenceMode.Encrypted && config.MetadataEncryptionKey == null) { throw new ArgumentException($"{nameof(AppConfiguration.MetadataEncryptionKey)} must be set when {nameof(AppConfiguration.MetadataPersistenceMode)} is set to {nameof(MetadataPersistenceMode.Encrypted)}."); } if (config.MetadataPersistenceMode != MetadataPersistenceMode.Encrypted && config.MetadataEncryptionKey != null) { throw new ArgumentException($"{nameof(AppConfiguration.MetadataPersistenceMode)} must be set to {nameof(MetadataPersistenceMode.Encrypted)} when {nameof(AppConfiguration.MetadataEncryptionKey)} is set."); } } var nativeConfig = new Native.AppConfiguration { AppId = config.AppId, BaseFilePath = config.BaseFilePath ?? InteropConfig.DefaultStorageFolder, BaseUrl = config.BaseUri?.ToString().TrimEnd('/'), LocalAppName = config.LocalAppName, LocalAppVersion = config.LocalAppVersion, MetadataPersistence = config.MetadataPersistenceMode, default_request_timeout_ms = (ulong?)config.DefaultRequestTimeout?.TotalMilliseconds ?? 0, #pragma warning disable CS0618 // Type or member is obsolete - We still want to support people using it log_level = config.LogLevel != LogLevel.Info ? config.LogLevel : Logger.LogLevel, }; if (config.CustomLogger != null) { var logger = Logger.Function((level, message) => config.CustomLogger(message, level)); logger._logLevel = nativeConfig.log_level; nativeConfig.managed_logger = GCHandle.ToIntPtr(logger.GCHandle); } #pragma warning restore CS0618 // Type or member is obsolete else if (Logger.Default != null) { nativeConfig.managed_logger = GCHandle.ToIntPtr(Logger.Default.GCHandle); } var handle = AppHandle.CreateApp(nativeConfig, config.MetadataEncryptionKey); return(new App(handle)); }
/// <summary> /// A factory method for creating an app with a particular <see cref="AppConfiguration"/>. /// </summary> /// <param name="config">The <see cref="AppConfiguration"/>, specifying key parameters for the app behavior.</param> /// <returns>An <see cref="App"/> instance can now be used to login users, call functions, or open synchronized Realms.</returns> public static App Create(AppConfiguration config) { Argument.NotNull(config, nameof(config)); if (config.MetadataPersistenceMode.HasValue) { if (config.MetadataPersistenceMode == MetadataPersistenceMode.Encrypted && config.MetadataEncryptionKey == null) { throw new ArgumentException($"{nameof(AppConfiguration.MetadataEncryptionKey)} must be set when {nameof(AppConfiguration.MetadataPersistenceMode)} is set to {nameof(MetadataPersistenceMode.Encrypted)}."); } if (config.MetadataPersistenceMode != MetadataPersistenceMode.Encrypted && config.MetadataEncryptionKey != null) { throw new ArgumentException($"{nameof(AppConfiguration.MetadataPersistenceMode)} must be set to {nameof(MetadataPersistenceMode.Encrypted)} when {nameof(AppConfiguration.MetadataEncryptionKey)} is set."); } } var nativeConfig = new Native.AppConfiguration { AppId = config.AppId, BaseFilePath = config.BaseFilePath ?? InteropConfig.DefaultStorageFolder, BaseUrl = config.BaseUri?.ToString().TrimEnd('/'), LocalAppName = config.LocalAppName, LocalAppVersion = config.LocalAppVersion, MetadataPersistence = config.MetadataPersistenceMode, default_request_timeout_ms = (ulong?)config.DefaultRequestTimeout?.TotalMilliseconds ?? 0, log_level = config.LogLevel, }; if (config.CustomLogger != null) { // TODO: should we free this eventually? var logHandle = GCHandle.Alloc(config.CustomLogger); nativeConfig.managed_log_callback = GCHandle.ToIntPtr(logHandle); } var handle = AppHandle.CreateApp(nativeConfig, config.MetadataEncryptionKey); return(new App(handle)); }