在WPF应用程序中调试Silverlight

4
我正在开发一个包含WebBrowser控件的WPF应用程序,该控件加载Silverlight应用程序。我希望能够从Visual Studio(F5)启动应用程序,并使调试器附加到Silverlight代码上。然而,我一直没有成功。
目前我最好的做法是在不附加的情况下启动应用程序,然后在它运行时手动附加到进程中,将Silverlight指定为要调试的代码类型,这样就可以工作了。(当我让WebBrowser控件加载Silverlight应用程序时,它会触发我的Silverlight代码中的断点)。我编写了一些宏来自动化这个启动/附加过程,但仍然不是最好的。
我尝试将WPF应用程序指定为运行Silverlight应用程序时的外部程序,但Visual Studio会附加到进程并想要调试托管的.NET代码。
有什么想法吗?理想情况下,我真的希望能够附加到进程并调试托管的.NET和Silverlight代码,但我认为这是不可能的。我真的希望在启动时自动附加到Silverlight代码,以便轻松调试Silverlight应用程序中的所有问题,包括加载时发生的问题。

您可以在Microsoft Connect上报告此问题或功能,我认为这只是因为它假定它是WPF应用程序进行调试,如果不是任何浏览器。 - Akash Kava
3个回答

1
感谢Brandorf和fatty的想法。Brandorf的想法几乎让我达到了我想要的目标,但是需要我的SL应用程序能够独立运行。我真的只想有一个应用程序,它既是wpf又是silverlight,并且SL端可以进行调试。
在我提出这个问题很久之后(我忘记我在这里提出了这个问题),我实际上拼凑出了一个解决方案,我非常满意。我在我的应用程序的WPF/.NET端使用Visual Studio自动化来查找所有正在运行的Visual Studio实例,找出哪一个生成了我的exe文件(因为它通常位于vcproj/sln文件夹下面的文件夹中),然后使用Visual Studio自动化使该VS附加到应用程序上,调试Silverlight代码。完成这些操作后,我加载我的Silverlight内容。
它的效果非常好。每次运行时,您都会得到一个应用程序,它会去找一个调试器来附加到自己身上(所以您可能只想在调试版本中使用此代码,或者以某种方式将其关闭)。因此,您只需在Visual Studio中使用Ctrl-F5(不带调试启动)启动应用程序,以便在需要调试Silverlight端时进行调试。
以下是我的代码:
#if DEBUG

using System;
using System.Collections.Generic;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;

namespace Launcher
{
    //The core methods in this class to find all running instances of VS are
    //taken/inspired from
    //http://www.codeproject.com/KB/cs/automatingvisualstudio.aspx
    class DebuggingAutomation
    {
        [DllImport("ole32.dll")]
        private static extern int GetRunningObjectTable(int reserved,
                                  out UCOMIRunningObjectTable prot);

        [DllImport("ole32.dll")]
        private static extern int CreateBindCtx(int reserved,
                                      out UCOMIBindCtx ppbc);
        ///<summary>
        ///Get a snapshot of the running object table (ROT).
        ///</summary>
        ///<returns>
        ///A hashtable mapping the name of the object
        ///in the ROT to the corresponding object
        ///</returns>
        private static Hashtable GetRunningObjectTable()
        {
            Hashtable result = new Hashtable();

            int numFetched;
            UCOMIRunningObjectTable runningObjectTable;
            UCOMIEnumMoniker monikerEnumerator;
            UCOMIMoniker[] monikers = new UCOMIMoniker[1];

            GetRunningObjectTable(0, out runningObjectTable);
            runningObjectTable.EnumRunning(out monikerEnumerator);
            monikerEnumerator.Reset();

            while (monikerEnumerator.Next(1, monikers, out numFetched) == 0)
            {
                UCOMIBindCtx ctx;
                CreateBindCtx(0, out ctx);

                string runningObjectName;
                monikers[0].GetDisplayName(ctx, null, out runningObjectName);

                object runningObjectVal;
                runningObjectTable.GetObject(monikers[0], out runningObjectVal);

                result[runningObjectName] = runningObjectVal;
            }

            return result;
        }

        /// <summary>
        /// Get a table of the currently running instances of the Visual Studio .NET IDE.
        /// </summary>
        /// <param name="openSolutionsOnly">
        /// Only return instances that have opened a solution
        /// </param>
        /// <returns>
        /// A list of the ides (as DTE objects) present in
        /// in the running object table to the corresponding DTE object
        /// </returns>
        private static List<EnvDTE.DTE> GetIDEInstances(bool openSolutionsOnly)
        {
            var runningIDEInstances = new List<EnvDTE.DTE>();
            Hashtable runningObjects = GetRunningObjectTable();

            IDictionaryEnumerator rotEnumerator = runningObjects.GetEnumerator();
            while (rotEnumerator.MoveNext())
            {
                string candidateName = (string)rotEnumerator.Key;
                if (!candidateName.StartsWith("!VisualStudio.DTE"))
                    continue;

                EnvDTE.DTE ide = rotEnumerator.Value as EnvDTE.DTE;
                if (ide == null)
                    continue;

                if (openSolutionsOnly)
                {
                    try
                    {
                        string solutionFile = ide.Solution.FullName;
                        if (!String.IsNullOrEmpty(solutionFile))
                        {
                            runningIDEInstances.Add(ide);
                        }
                    }
                    catch { }
                }
                else
                {
                    runningIDEInstances.Add(ide);
                }
            }
            return runningIDEInstances;
        }

        internal static void AttachDebuggerIfPossible()
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                //Probably debugging host (Desktop .NET side), so don't try to attach to silverlight side
                return;
            }
            var ides = GetIDEInstances(true);
            var fullPathToAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var potentials = new List<EnvDTE.DTE>();
            foreach (var ide in ides)
            {
                var solutionPath = ide.Solution.FullName;
                var topLevelSolutionDir = Path.GetDirectoryName(solutionPath);
                var assemblyName = fullPathToAssembly;
                if (assemblyName.StartsWith(topLevelSolutionDir, StringComparison.OrdinalIgnoreCase))
                {
                    potentials.Add(ide);
                }
            }

            EnvDTE.DTE chosenIde = null;
            //If you have multiple ides open that can match your exe, you can come up with a scheme to pick a particular one
            //(eg, put a file like solution.sln.pickme next to the solution whose ide you want to debug). If this is not a
            //concern, just pick the first match.
            if (potentials.Count > 0)
            {
                chosenIde = potentials[0];
            }
            var dbg = chosenIde != null ? (EnvDTE80.Debugger2)chosenIde.Debugger : null;

            if (dbg != null)
            {
                var trans = dbg.Transports.Item("Default");

                var proc = (EnvDTE80.Process2)dbg.GetProcesses(trans, System.Environment.MachineName).Item(Path.GetFileName(fullPathToAssembly));
                var engines = new EnvDTE80.Engine[1];
                engines[0] = trans.Engines.Item("Silverlight");

                proc.Attach2(engines);
            }
        }
    }
}
#endif 

0

这有点冒险,但是假设您的Silverlight应用程序能够独立运行,您可以在解决方案设置下,将Visual Studio设置为同时启动两个应用程序,然后您应该会连接到它们两个。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接