예제 #1
0
        /// <summary>
        /// Load up Lynicon modules and initialise the Lynicon data API after having loaded outputs from the current Visual Studio project
        /// which references Lynicon
        /// </summary>
        /// <param name="sendMessage"></param>
        public static void InitialiseDataApi(Action <MessageEventArgs> sendMessage)
        {
            if (DataApiInitialised)
            {
                return;
            }

            EnsureLoaded(sendMessage);

            using (AppConfig.Change(WebConfigPath))
            {
                string lyniconConfigName = DefaultNs + ".LyniconConfig";

                if (sendMessage != null)
                {
                    sendMessage(new MessageEventArgs("Initializing Project Context", "Trying to load type: " + lyniconConfigName));
                }
                Type lyniconConfig = StartupAssembly.GetType(lyniconConfigName);
                if (lyniconConfig == null)
                {
                    sendMessage(new MessageEventArgs(new Exception("Cannot load type LyniconConfig: you may not have built the solution after installing the Lynicon package")));
                }


                if (sendMessage != null)
                {
                    sendMessage(new MessageEventArgs("Initializing Project Context", "Type loaded"));
                }

                var registerModulesMethod   = lyniconConfig.GetMethod("RegisterModules", BindingFlags.Static | BindingFlags.Public);
                var initialiseDataApiMethod = lyniconConfig.GetMethod("InitialiseDataApi", BindingFlags.Static | BindingFlags.Public);
                if ((registerModulesMethod == null || initialiseDataApiMethod == null) && sendMessage != null)
                {
                    sendMessage(new MessageEventArgs(new Exception("Cannot find 'RegisterModules' and 'InitialiseDataApi' methods on LyniconConfig")));
                }

                if (sendMessage != null)
                {
                    sendMessage(new MessageEventArgs("Initializing Project Context", "Initializing Data Api"));
                }
                try
                {
                    registerModulesMethod.Invoke(null, new object[0]);
                    initialiseDataApiMethod.Invoke(null, new object[0]);
                }
                catch (Exception ex)
                {
                    if (sendMessage != null)
                    {
                        sendMessage(new MessageEventArgs(ex));
                    }
                }

                if (sendMessage != null)
                {
                    sendMessage(new MessageEventArgs("Initializing Project Context", "Lynicon initialized"));
                }
            }

            DataApiInitialised = true;
        }
예제 #2
0
        /// <summary>
        /// Set up environment parameters and load assemblies from the output of the currently running
        /// Visual Studio project
        /// </summary>
        /// <param name="sendMessage"></param>
        public static void EnsureLoaded(Action <MessageEventArgs> sendMessage)
        {
            if (ContextLoaded)
            {
                return;
            }

            EnsureDTE(sendMessage);

            if (sendMessage != null)
            {
                sendMessage(new MessageEventArgs("Initializing Project Context", "Loading assembly: " + AssPath));
            }

            System.AppDomain.CurrentDomain.SetData("APPBASE", RootPath);
            System.AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", "bin");
            System.AppDomain.CurrentDomain.SetData("DataDirectory", RootPath + "\\App_Data");

            DllPath = RootPath + "\\bin\\";

            // Use the web config of the website project as config for this appdomain
            using (AppConfig.Change(WebConfigPath))
            {
                StartupAssembly = null;
                try
                {
                    StartupAssembly = Assembly.LoadFrom(AssPath);
                    var x = StartupAssembly.GetType("log4net.Config.Log4NetConfigurationSectionHandler");
                }
                catch (Exception ex)
                {
                    if (sendMessage != null)
                    {
                        if (ex is FileNotFoundException && ex.Message.StartsWith("Could not load"))
                        {
                            sendMessage(new MessageEventArgs(new Exception("Couldn't load the project assembly: you may not have compiled it", ex)));
                        }
                        else
                        {
                            sendMessage(new MessageEventArgs(ex));
                        }
                    }
                }

                Assembly efSqlAss = null;
                try
                {
                    // Load this manually as it will generally not be referenced but is referenced by Lynicon
                    efSqlAss = Assembly.LoadFrom(RootPath + "\\bin\\EntityFramework.SqlServer.dll");
                    var x = efSqlAss.GetType("System.Data.Entity.SqlServer.SqlProviderServices");
                }
                catch { }

                if (sendMessage != null)
                {
                    sendMessage(new MessageEventArgs("Initializing Project Context", "Assembly loaded"));
                }
            }

            ContextLoaded = true;
        }