Exemplo n.º 1
0
        private MobileCore(IPlatformInjector injector, Options options)
        {
            Logger = options.Logger ?? injector?.CreateLogger() ?? new NullLogger();

            if (injector != null && options.ConfigFileName != null)
            {
                try
                {
                    using (var stream = injector.GetBundledFileStream(options.ConfigFileName))
                    {
                        mobileConfiguration = MobileCoreConfiguration.Parse(stream);
                    }
                }
                catch (System.Exception e)
                {
                    throw new InitializationException($"{options.ConfigFileName} could not be loaded", e);
                }
            }
            else
            {
                if (options.ConfigJson != null)
                {
                    try
                    {
                        mobileConfiguration = MobileCoreConfiguration.Parse(options.ConfigJson);
                    }
                    catch (System.Exception e)
                    {
                        throw new InitializationException("invalid JSON configuration file", e);
                    }
                }
                else
                {
                    throw new InitializationException("Must provide either filename or JSON configuration in Init() options");
                }
            }
            if (options.HttpServiceModule == null)
            {
                HttpClientHandler httpClientHandler = new HttpClientHandler();
                httpClientHandler.AllowAutoRedirect = options.HttpAllowAutoRedirect;

                HttpClient httpClient = new HttpClient(httpClientHandler);
                httpClient.Timeout = TimeSpan.FromSeconds(DEFAULT_TIMEOUT);
                var httpServiceModule = new SystemNetHttpServiceModule(httpClient);
                var configuration     = GetFirstServiceConfigurationByType(httpServiceModule.Type);
                if (configuration == null)
                {
                    configuration = ServiceConfiguration.Builder.Build();
                }
                httpServiceModule.Configure(this, configuration);
                HttpLayer = httpServiceModule;
            }
            else
            {
                HttpLayer = options.HttpServiceModule;
            }
        }
        private MobileCore(IPlatformInjector injector, Options options)
        {
            Contract.Requires(options != null);
            Logger = options.Logger ?? injector?.CreateLogger() ?? new NullLogger();

            if (injector != null && options.ConfigFileName != null)
            {
                var filename = $"{injector.DefaultResources}.{options.ConfigFileName}";
                try
                {
                    using (var stream = injector.ExecutingAssembly.GetManifestResourceStream(filename))
                    {
                        servicesConfig = MobileCoreJsonParser.Parse(stream);
                    }
                }
                catch (System.Exception e)
                {
                    throw new InitializationException($"{filename} could not be loaded", e);
                }
            }
            else
            {
                if (options.ConfigJson != null)
                {
                    try
                    {
                        MobileCoreJsonParser.Parse(options.ConfigJson);
                    }
                    catch (System.Exception e)
                    {
                        throw new InitializationException("invalid JSON configuration file", e);
                    }
                }
                else
                {
                    throw new InitializationException("Must provide either filename or JSON configuration in Init() options");
                }
            }

            if (options.HttpServiceModule == null)
            {
                HttpClient httpClient = new HttpClient();
                httpClient.Timeout = TimeSpan.FromSeconds(DEFAULT_TIMEOUT);
                var httpServiceModule = new SystemNetHttpServiceModule(httpClient);
                var configuration     = GetServiceConfiguration(httpServiceModule.Type);
                if (configuration == null)
                {
                    configuration = ServiceConfiguration.Builder.Build();
                }
                httpServiceModule.Configure(this, configuration);
                HttpLayer = httpServiceModule;
            }
            else
            {
                HttpLayer = options.HttpServiceModule;
            }
        }