Beispiel #1
0
        public static void Initialize(bool updateAddinRegistry)
        {
            if (initialized)
            {
                return;
            }

            Counters.RuntimeInitialization.BeginTiming();
            SetupInstrumentation();

            Platform.Initialize();

            mainThread = Thread.CurrentThread;
            // Set a default sync context
            if (SynchronizationContext.Current == null)
            {
                defaultSynchronizationContext = new SynchronizationContext();
                SynchronizationContext.SetSynchronizationContext(defaultSynchronizationContext);
            }
            else
            {
                defaultSynchronizationContext = SynchronizationContext.Current;
            }


            // Hook up the SSL certificate validation codepath
            ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
                if (sslPolicyErrors == SslPolicyErrors.None)
                {
                    return(true);
                }

                if (sender is WebRequest)
                {
                    sender = ((WebRequest)sender).RequestUri.Host;
                }
                return(WebCertificateService.GetIsCertificateTrusted(sender as string, certificate.GetPublicKeyString()));
            };

            AddinManager.AddinLoadError += OnLoadError;
            AddinManager.AddinLoaded    += OnLoad;
            AddinManager.AddinUnloaded  += OnUnload;

            try {
                Counters.RuntimeInitialization.Trace("Initializing Addin Manager");

                string configDir, addinsDir, databaseDir;
                GetAddinRegistryLocation(out configDir, out addinsDir, out databaseDir);
                AddinManager.Initialize(configDir, addinsDir, databaseDir);
                AddinManager.InitializeDefaultLocalizer(new DefaultAddinLocalizer());

                if (updateAddinRegistry)
                {
                    AddinManager.Registry.Update(null);
                }
                setupService = new AddinSetupService(AddinManager.Registry);
                Counters.RuntimeInitialization.Trace("Initialized Addin Manager");

                PropertyService.Initialize();

                WebRequestHelper.Initialize();
                Mono.Addins.Setup.WebRequestHelper.SetRequestHandler(WebRequestHelper.GetResponse);

                //have to do this after the addin service and property service have initialized
                if (UserDataMigrationService.HasSource)
                {
                    Counters.RuntimeInitialization.Trace("Migrating User Data from MD " + UserDataMigrationService.SourceVersion);
                    UserDataMigrationService.StartMigration();
                }

                RegisterAddinRepositories();

                Counters.RuntimeInitialization.Trace("Initializing Assembly Service");
                systemAssemblyService = new SystemAssemblyService();
                systemAssemblyService.Initialize();
                LoadMSBuildLibraries();

                initialized = true;
            } catch (Exception ex) {
                Console.WriteLine(ex);
                AddinManager.AddinLoadError -= OnLoadError;
                AddinManager.AddinLoaded    -= OnLoad;
                AddinManager.AddinUnloaded  -= OnUnload;
            } finally {
                Counters.RuntimeInitialization.EndTiming();
            }
        }
Beispiel #2
0
        public static Task <bool> UpdateDownloadedCacheFile(string url, string cacheFile,
                                                            Func <Stream, bool> validateDownload = null, CancellationToken ct = default(CancellationToken))
        {
            return(WebRequestHelper.GetResponseAsync(
                       () => (HttpWebRequest)WebRequest.Create(url),
                       r => {
                //check to see if the online file has been modified since it was last downloaded
                var localNewsXml = new FileInfo(cacheFile);
                if (localNewsXml.Exists)
                {
                    r.IfModifiedSince = localNewsXml.LastWriteTime;
                }
            },
                       ct
                       ).ContinueWith(t => {
                bool deleteTempFile = true;
                var tempFile = cacheFile + ".temp";

                try {
                    ct.ThrowIfCancellationRequested();

                    if (t.IsFaulted)
                    {
                        var wex = t.Exception.Flatten().InnerException as WebException;
                        if (wex != null)
                        {
                            var resp = wex.Response as HttpWebResponse;
                            if (resp != null && resp.StatusCode == HttpStatusCode.NotModified)
                            {
                                return false;
                            }
                        }
                    }

                    //TODO: limit this size in case open wifi hotspots provide junk data
                    var response = t.Result;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (var fs = File.Create(tempFile))
                            response.GetResponseStream().CopyTo(fs, 2048);
                    }

                    //check the document is valid, might get bad ones from wifi hotspots etc
                    if (validateDownload != null)
                    {
                        ct.ThrowIfCancellationRequested();

                        using (var f = File.OpenRead(tempFile)) {
                            bool validated;
                            try {
                                validated = validateDownload(f);
                            } catch (Exception ex) {
                                throw new Exception("Failed to validate downloaded file", ex);
                            }
                            if (!validated)
                            {
                                throw new Exception("Failed to validate downloaded file");
                            }
                        }
                    }

                    ct.ThrowIfCancellationRequested();

                    SystemRename(tempFile, cacheFile);
                    deleteTempFile = false;
                    return true;
                } finally {
                    if (deleteTempFile)
                    {
                        try {
                            File.Delete(tempFile);
                        } catch (Exception ex) {
                            LoggingService.LogError("Failed to delete temp download file", ex);
                        }
                    }
                }
            }, ct));
        }