コード例 #1
0
        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initialization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            bool bQuitting = false;

            base.Initialize();

#if DEBUG
            Log          = new Core.Logger.OutputLogger(this);
            Log.LogLevel = LogLevels.Debug;
#else
            Log          = new Core.Logger.NullLogger();
            Log.LogLevel = LogLevels.Warning;
#endif
            Log.Debug("BIDSHelper Package Initialize Starting");
            string sAddInTypeName = string.Empty;
            try
            {
                StatusBar      = new Core.VsIntegration.StatusBar(this);
                StatusBar.Text = "Loading BIDSHelper (" + this.GetType().Assembly.GetName().Version.ToString() + ")...";
                VsShell        = (IVsShell)this.GetService(typeof(SVsShell));
                DTE2           = this.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE)) as EnvDTE80.DTE2;

                DebuggerService.AdviseDebuggerEvents(this, out debugEventCookie);

                if (SwitchVsixManifest())
                {
                    bQuitting = true;
                    RestartVisualStudio();
                    return;
                }

                System.Collections.Generic.List <Exception> pluginExceptions = new System.Collections.Generic.List <Exception>();
                Type[] types = null;
                try
                {
                    types = Assembly.GetExecutingAssembly().GetTypes();
                }
                catch (ReflectionTypeLoadException loadEx)
                {
                    types = loadEx.Types; //if some types can't be loaded (possibly because SSIS SSDT isn't installed, just SSAS?) then proceed with the types that work
                    pluginExceptions.Add(loadEx);
                    Log.Exception("Problem loading BIDS Helper types list", loadEx);
                }

                foreach (Type t in types)
                {
                    if (//typeof(IBIDSHelperPlugin).IsAssignableFrom(t.GetType())
                        t != null &&
                        t.GetInterfaces().Contains(typeof(IBIDSHelperPlugin)) &&
                        (!object.ReferenceEquals(t, typeof(IBIDSHelperPlugin))) &&
                        (!t.IsAbstract))
                    {
                        sAddInTypeName = t.Name;
                        Log.Verbose(string.Format("Loading Plugin: {0}", sAddInTypeName));

                        BIDSHelperPluginBase feature;
                        Type[] @params = { typeof(BIDSHelperPackage) };
                        System.Reflection.ConstructorInfo con;

                        con = t.GetConstructor(@params);

                        if (con == null)
                        {
                            System.Windows.Forms.MessageBox.Show("Problem loading type " + t.Name + ". No constructor found.");
                            continue;
                        }

                        try
                        {
                            feature = (BIDSHelperPluginBase)con.Invoke(new object[] { this });
                            Plugins.Add(feature.FullName, feature);
                        }
                        catch (Exception ex)
                        {
                            pluginExceptions.Add(new Exception("BIDS Helper plugin constructor failed on " + sAddInTypeName + ": " + ex.Message + "\r\n" + ex.StackTrace, ex));
                            Log.Exception("BIDS Helper plugin constructor failed on " + sAddInTypeName, ex);
                        }
                    }
                }

                if (pluginExceptions.Count > 0)
                {
                    string sException = "";
                    foreach (Exception pluginEx in pluginExceptions)
                    {
                        sException += string.Format("BIDS Helper encountered an error when Visual Studio started:\r\n{0}\r\n{1}"
                                                    , pluginEx.Message
                                                    , pluginEx.StackTrace);

                        Exception innerEx = pluginEx.InnerException;
                        while (innerEx != null)
                        {
                            sException += string.Format("\r\nInner exception:\r\n{0}\r\n{1}"
                                                        , innerEx.Message
                                                        , innerEx.StackTrace);
                            innerEx = innerEx.InnerException;
                        }

                        ReflectionTypeLoadException ex = pluginEx as ReflectionTypeLoadException;
                        if (ex == null)
                        {
                            ex = pluginEx.InnerException as ReflectionTypeLoadException;
                        }
                        if (ex != null)
                        {
                            System.Text.StringBuilder sb = new System.Text.StringBuilder();
                            foreach (Exception exSub in ex.LoaderExceptions)
                            {
                                sb.AppendLine();
                                sb.AppendLine(exSub.Message);
                                System.IO.FileNotFoundException exFileNotFound = exSub as System.IO.FileNotFoundException;
                                if (exFileNotFound != null)
                                {
                                    if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
                                    {
                                        sb.AppendLine("Fusion Log:");
                                        sb.AppendLine(exFileNotFound.FusionLog);
                                    }
                                }
                                sb.AppendLine();
                            }
                            sException += sb.ToString();
                        }
                    }
                    AddInLoadException = new Exception(sException);
                }

#if DENALI
                //handle assembly reference problems when the compiled reference doesn't exist in that version of Visual Studio
                //doesn't appear to be needed for VS2013
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);
#endif
            }
            catch (Exception ex)
            {
                //don't show a popup anymore since this exception is viewable in the Version dialog in the Tools menu
                if (string.IsNullOrEmpty(sAddInTypeName))
                {
                    AddInLoadException = ex;
                    Log.Exception("Problem loading BIDS Helper", ex);
                    //System.Windows.Forms.MessageBox.Show("Problem loading BIDS Helper: " + ex.Message + "\r\n" + ex.StackTrace);
                }
                else
                {
                    AddInLoadException = new Exception("Problem loading BIDS Helper. Problem type was " + sAddInTypeName + ": " + ex.Message + "\r\n" + ex.StackTrace, ex);
                    Log.Exception("Problem loading BIDS Helper. Problem type was " + sAddInTypeName, ex);
                    //System.Windows.Forms.MessageBox.Show("Problem loading BIDS Helper. Problem type was " + sAddInTypeName + ": " + ex.Message + "\r\n" + ex.StackTrace);
                }
            }
            finally
            {
                if (!bQuitting)
                {
                    StatusBar.Clear();
                }
            }
        }
コード例 #2
0
        private void OriginalInitialize()
        {
            bool bQuitting = false;

            base.Initialize();

#if DEBUG
            Log          = new Core.Logger.OutputLogger(this);
            Log.LogLevel = LogLevels.Debug;
#else
            Log          = new Core.Logger.NullLogger();
            Log.LogLevel = LogLevels.Warning;
#endif
            Log.Debug("BIDSHelper Package Initialize Starting");


            //var bidsHelperPath = new System.IO.FileInfo(typeof(BIDSHelperPackage).Assembly.Location);

//#pragma warning disable 0618 //ignore the fact this is an obsolete method
//            AppDomain.CurrentDomain.AppendPrivatePath(bidsHelperPath.DirectoryName);
//            AppDomain.CurrentDomain.SetupInformation.ApplicationBase = bidsHelperPath.DirectoryName;
//#pragma warning restore 0618

            //(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath + ";" ?? "") +

            /*
             * VS2019
             * Microsoft Reporting Services Projects (by Microsoft) v2.5.6 717ad572-c4b7-435c-c166-c2969777f718
             * Microsoft Analysis Services Projects (by Microsoft) v2.8.11 04a86fc2-dbd5-4222-848e-911638e487fe
             *
             * VS2017
             * Microsoft Reporting Services Projects (by Microsoft) v2.5.6 717ad572-c4b7-435c-c166-c2969777f718
             * Microsoft Integration Services Projects (by Microsoft) v2.1 D1B09713-C12E-43CC-9EF4-6562298285AB
             * Microsoft Analysis Services Projects (by Microsoft) v2.8.11 04a86fc2-dbd5-4222-848e-911638e487fe
             *
             */
            try
            {
                System.IServiceProvider serviceProvider = this as System.IServiceProvider;
                Microsoft.VisualStudio.ExtensionManager.IVsExtensionManager em =
                    (Microsoft.VisualStudio.ExtensionManager.IVsExtensionManager)serviceProvider.GetService(
                        typeof(Microsoft.VisualStudio.ExtensionManager.SVsExtensionManager));

                string result = "";
                foreach (Microsoft.VisualStudio.ExtensionManager.IInstalledExtension i in em.GetInstalledExtensions())
                {
                    try
                    {
                        Microsoft.VisualStudio.ExtensionManager.IExtensionHeader h = i.Header;
                        if (h.Name == "Microsoft Reporting Services Projects" || string.Compare(h.Identifier, "717ad572-c4b7-435c-c166-c2969777f718", true) == 0)
                        {
                            SSRSExtensionVersion     = h.Version;
                            SSRSExtensionInstallPath = i.InstallPath;
                            Log.Debug("SSRS extension v" + h.Version + " is installed at " + i.InstallPath);
                        }
                        else if (h.Name == "Microsoft Integration Services Projects" || string.Compare(h.Identifier, "D1B09713-C12E-43CC-9EF4-6562298285AB", true) == 0 || //2.2 (VS2017)
                                 h.Name == "SQL Server Integration Services Projects" || string.Compare(h.Identifier, "851E7A09-7B2B-4F06-A15D-BABFCB26B970", true) == 0 //3.0 (VS2019)
                                 )
                        {
                            SSISExtensionVersion     = h.Version;
                            SSISExtensionInstallPath = i.InstallPath;
                            Log.Debug("SSIS extension v" + h.Version + " is installed at " + i.InstallPath);
                        }
                        else if (h.Name == "Microsoft Analysis Services Projects" || string.Compare(h.Identifier, "04a86fc2-dbd5-4222-848e-911638e487fe", true) == 0)
                        {
                            SSASExtensionVersion     = h.Version;
                            SSASExtensionInstallPath = i.InstallPath;
                            Log.Debug("SSAS extension v" + h.Version + " is installed at " + i.InstallPath);
                        }
                        else if (h.Name == "Microsoft BI Shared Components for Visual Studio" || string.Compare(h.Identifier, "BAB64743-DA65-4501-B3A3-A73171C73D77", true) == 0)
                        {
                            BISharedExtensionInstallPath = i.InstallPath;
                            Log.Debug("BI Shared extension v" + h.Version + " is installed at " + i.InstallPath);
                        }
                        result += h.Name + " (by " + h.Author + ") v" + h.Version + " " + h.Identifier + " " + h.MoreInfoUrl + " " + i.InstallPath + System.Environment.NewLine;
                    }
                    catch { }
                }
                Log.Debug(result);
            }
            catch { }


            string sAddInTypeName = string.Empty;
            try
            {
#if !DENALI
                //given the version numbers seem to be changing frequently, try this approach to increment version numbers of references
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);
#endif

                StatusBar      = new Core.VsIntegration.StatusBar(this);
                StatusBar.Text = "Loading BIDSHelper (" + this.GetType().Assembly.GetName().Version.ToString() + ")...";
                VsShell        = (IVsShell)this.GetService(typeof(SVsShell));
                DTE2           = this.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE)) as EnvDTE80.DTE2;

                DebuggerService.AdviseDebuggerEvents(this, out debugEventCookie);

                //if (SwitchVsixManifest())
                //{
                //    bQuitting = true;
                //    RestartVisualStudio();
                //    return;
                //}

                System.Collections.Generic.List <Exception> pluginExceptions = new System.Collections.Generic.List <Exception>();
                Type[] types = null;
                try
                {
                    types = Assembly.GetExecutingAssembly().GetTypes();
                }
                catch (ReflectionTypeLoadException loadEx)
                {
                    types = loadEx.Types; //if some types can't be loaded (possibly because SSIS SSDT isn't installed, just SSAS?) then proceed with the types that work
                    //pluginExceptions.Add(loadEx); //in testing it appears that this does return the complete list of types so there's no need to display this exception to the user, I don't think since we will individually load plugins below and log exceptions
                    //Log.Exception("Problem loading BIDS Helper types list", loadEx);
                    //Log.Error(FormatLoaderException(loadEx));
                }
                Log.Debug($"Found {types.Length} types");

                //can be used for debugging which types couldn't be loaded above
                //for (int i = 0; i < types.Length; i++)
                //{
                //    if (types[i] == null)
                //        Log.Error("types[" + i + "] is null");
                //    else
                //        Log.Debug("types[" + i + "]\t" + types[i].FullName);
                //}

                foreach (Type t in types)
                {
                    if (//typeof(IBIDSHelperPlugin).IsAssignableFrom(t.GetType())
                        t != null &&
                        t.GetInterfaces().Contains(typeof(IBIDSHelperPlugin)) &&
                        (!object.ReferenceEquals(t, typeof(IBIDSHelperPlugin))) &&
                        (!t.IsAbstract))
                    {
                        sAddInTypeName = t.Name;

                        //new... only load SSIS to test this works
                        //load any not marked
                        var categoryAttribute = t.GetCustomAttributes(typeof(FeatureCategory), true).FirstOrDefault() as FeatureCategory;
                        if (categoryAttribute != null)
                        {
                            if (SSISExtensionVersion == null && categoryAttribute.Category == BIDSFeatureCategories.SSIS)
                            {
                                Log.Verbose(string.Format("Skipping Plugin: {0}", sAddInTypeName));
                                continue;
                            }
                            else if (SSASExtensionVersion == null && (categoryAttribute.Category == BIDSFeatureCategories.SSASMulti || categoryAttribute.Category == BIDSFeatureCategories.SSASTabular))
                            {
                                Log.Verbose(string.Format("Skipping Plugin: {0}", sAddInTypeName));
                                continue;
                            }
                            else if (SSRSExtensionVersion == null && categoryAttribute.Category == BIDSFeatureCategories.SSRS)
                            {
                                Log.Verbose(string.Format("Skipping Plugin: {0}", sAddInTypeName));
                                continue;
                            }
                        }
                        else
                        {
                            Log.Verbose(string.Format("Warning: Plugin FeatureCategory not set: {0}", sAddInTypeName));
                        }


                        Log.Verbose(string.Format("Loading Plugin: {0}", sAddInTypeName));

                        BIDSHelperPluginBase feature;
                        Type[] @params = { typeof(BIDSHelperPackage) };
                        System.Reflection.ConstructorInfo con;

                        con = t.GetConstructor(@params);

                        if (con == null)
                        {
                            System.Windows.Forms.MessageBox.Show("Problem loading type " + t.Name + ". No constructor found.");
                            continue;
                        }

                        try
                        {
                            feature = (BIDSHelperPluginBase)con.Invoke(new object[] { this });
                            Plugins.Add(feature.FullName, feature);
                        }
                        catch (Exception ex)
                        {
                            pluginExceptions.Add(new Exception("BIDS Helper plugin constructor failed on " + sAddInTypeName + ": " + ex.Message + "\r\n" + ex.StackTrace, ex));
                            Log.Exception("BIDS Helper plugin constructor failed on " + sAddInTypeName, ex);
                        }
                    }
                }

                if (pluginExceptions.Count > 0)
                {
                    string sException = "";
                    foreach (Exception pluginEx in pluginExceptions)
                    {
                        sException += FormatLoaderException(pluginEx);
                    }
                    AddInLoadException = new Exception(sException);
                }

#if DENALI
                //handle assembly reference problems when the compiled reference doesn't exist in that version of Visual Studio
                //doesn't appear to be needed for VS2013
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);
#endif
            }
            catch (Exception ex)
            {
                //don't show a popup anymore since this exception is viewable in the Version dialog in the Tools menu
                if (string.IsNullOrEmpty(sAddInTypeName))
                {
                    AddInLoadException = ex;
                    Log.Exception("Problem loading BIDS Helper", ex);
                    //System.Windows.Forms.MessageBox.Show("Problem loading BIDS Helper: " + ex.Message + "\r\n" + ex.StackTrace);
                }
                else
                {
                    AddInLoadException = new Exception("Problem loading BIDS Helper. Problem type was " + sAddInTypeName + ": " + ex.Message + "\r\n" + ex.StackTrace, ex);
                    Log.Exception("Problem loading BIDS Helper. Problem type was " + sAddInTypeName, ex);
                    //System.Windows.Forms.MessageBox.Show("Problem loading BIDS Helper. Problem type was " + sAddInTypeName + ": " + ex.Message + "\r\n" + ex.StackTrace);
                }
            }
            finally
            {
                if (!bQuitting)
                {
                    StatusBar.Clear();
                }
            }
        }