简体   繁体   English

Visual Studio 2010自动附加到进程

[英]Visual Studio 2010 Automatic Attach To Process

I am using visual studio 2010, my application has a multiu layer architect, 我正在使用visual studio 2010,我的应用程序有一个multiu层架构师,

MainUI, WCFService, BLL and DAL MainUI,WCFService,BLL和DAL

My MainUI communicated to WCF and WCF further communicates to BLL and DAL, whenever i need to debug BLL and DAL, i first need to attach WCF as a process in Visual studio(everytime). 我的MainUI与WCF进行通信,WCF进一步与BLL和DAL通信,每当我需要调试BLL和DAL时,我首先需要将WCF作为Visual Studio中的进程附加(每次)。 How could i can save myself from this hassle. 我怎么能从这个麻烦中拯救自己。

How could i set up visual studio in a way that i automatically attach to the service automatically and i could debug my application easily. 我怎么能以自动附加到服务的方式设置visual studio,我可以轻松地调试我的应用程序。

Thanks 谢谢

Configure your solution for multi project start up. 为多项目启动配置解决方案。 I do this for a similar application. 我这样做是为了类似的应用程序。 VS launches the WCF and client automatically and I can set break points in either. VS自动启动WCF和客户端,我可以在其中设置断点。

The start-up order is the order in which you select the projects. 启动顺序是您选择项目的顺序。

Right mouse click on your solution and select 'choose startup projects'. 右键单击您的解决方案,然后选择“选择启动项目”。 Then select multiple startup projects and select the projects. 然后选择多个启动项目并选择项目。

Sample howto start a process and attach it to Visual Studio 2010 with EnvDTE(Version is relevant). 示例如何启动进程并使用EnvDTE将其附加到Visual Studio 2010(版本相关)。

//c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\EnvDTE.dll
using Process = EnvDTE.Process;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + @"\YourProcess.exe";
//Start the process
p.Start();
//Wait for process init
System.Threading.Thread.Sleep(1000);

bool attached = false;
//did not find a better solution for this(since it's not super reliable)
for (int i = 0; i < 5; i++)
{
    if (attached)
    {
        break;
    }
    try
    {
        EnvDTE.DTE dte2 = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
        EnvDTE.Debugger debugger = dte2.Debugger;
        foreach (Process program in debugger.LocalProcesses)
        {
            if (program.Name.Contains("YouProcess.exe"))
            {
                program.Attach();
                attached = true;
            }
        }
    }
    catch (Exception ex)
    {
        //handle execption...
    }
}

Try using System.Diagnostics.Debugger.Break() in the code. 尝试在代码中使用System.Diagnostics.Debugger.Break()。 If a debugger is not attached, then running that code will ask to attach a debugger and you can choose the existing instance. 如果未附加调试器,则运行该代码将要求附加调试器,您可以选择现有实例。

Have you tried System.Diagnostics.Debugger.Launch() in your service you would like the debugger to attach to? 您是否在服务中尝试过System.Diagnostics.Debugger.Launch() ,您希望调试器附加到?
http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx

Here is a detained article that explains how to do this...You can customize this macro. 这是一篇被扣留的文章,解释了如何执行此操作...您可以自定义此宏。

http://sivablogz.wordpress.com/2013/04/08/running-an-application-and-attaching-to-the-process-with-a-macro-in-visual-studio/ http://sivablogz.wordpress.com/2013/04/08/running-an-application-and-attaching-to-the-process-with-a-macro-in-visual-studio/

我个人更喜欢在这个线程中使用Debugger.Launch() ,因为它不需要引用DTE(这是特定于IDE的,必须明确引用到要使用的项目中)

  1. In the properties page for the wcf service project, select the Web tab. 在wcf服务项目的属性页面中,选择“Web”选项卡。
  2. Select 'Start External Program' for the start action, and choose MainUI.exe. 选择“启动外部程序”作为启动操作,然后选择MainUI.exe。
  3. Set the working directory as the folder that MainUI.exe is in (probably a bin folder). 将工作目录设置为MainUI.exe所在的文件夹(可能是bin文件夹)。
  4. Set a break point and press f5 to start debugging. 设置断点并按f5开始调试。

If I understand correctly, Macro may be answer: 如果我理解正确,宏可能会回答:

in Vs: 在Vs:

  1. Tools->Macros->record TemporarilyMacro (Ctrl+shift+r) 工具 - >宏 - >记录TemporarilyMacro(Ctrl + shift + r)
  2. Attach VS to process as usual (ctrl+alt+p) 像往常一样附加VS进行处理(ctrl + alt + p)
  3. Stop recording macro (ctrl+shift+r) 停止录制宏(ctrl + shift + r)
  4. Go to View->Other Windows->Macro Explorer (CTRL+F8) 转到视图 - >其他Windows->宏浏览器(CTRL + F8)
  5. find your Temporarily Macro (somewhere in MyMacros->RecordingModule) and rename it 找到你的临时宏(在MyMacros-> RecordingModule中的某个地方)并重命名它
  6. Now, go to Tools->Options->Keyboard and find your macro (in "Show Command containing write name of you macro) 现在,转到工具 - >选项 - >键盘并找到您的宏(在“显示包含您的宏写入名称的命令”中)
  7. in "Press Shortcut keys" bind it to some key shortcut (i have my macro in CTRL+SHIFT+K ;)) 在“按快捷键”将其绑定到某个键快捷键(我在CTRL + SHIFT + K中有我的宏;))
  8. Push OK 按“确定”
  9. Be Happy 要开心

Have you tried using the WCFSvcHost.EXE that comes with Visual Studio to launch the BLL and DAL service? 您是否尝试使用Visual Studio附带的WCFSvcHost.EXE来启动BLL和DAL服务? There is a help file with it. 有一个帮助文件。 The help file states it best, "Windows Communication Foundation (WCF) Service Host (wcfSvcHost.exe) allows you to launch the Visual Studio debugger (F5) to automatically host and test a service you have implemented. You can then test the service using WCF Test Client (wcfTestClient.exe), or your own client, to find and fix any potential errors." 帮助文件说明最佳,“Windows Communication Foundation(WCF)服务主机(wcfSvcHost.exe)允许您启动Visual Studio调试器(F5)以自动托管和测试您已实现的服务。然后您可以使用WCF测试客户端(wcfTestClient.exe)或您自己的客户端,用于查找和修复任何潜在错误。“ The default installation is C:\\Program Files\\Microsoft Visual Studio 10.0\\Common7\\IDE. 默认安装是C:\\ Program Files \\ Microsoft Visual Studio 10.0 \\ Common7 \\ IDE。 You can configure it to use your MainUI app as the client. 您可以将其配置为使用MainUI应用程序作为客户端。 The help file WcfSvcHost.chm in the same directory has a section for using a custom client under the Scenarios for using ECF Service Host. 同一目录中的帮助文件WcfSvcHost.chm有一个部分,用于在使用ECF服务主机的方案下使用自定义客户端。 If you rather here is the link to help on MS web site: Using WCF Service Host (wcfSvcHost.exe) . 如果您更喜欢这里是MS网站上的帮助链接: 使用WCF服务主机(wcfSvcHost.exe)

If this is for a self-hosted WCF windows service, you need to make your WCF service host configurable to either run in console or as a windows service. 如果这是针对自托管的WCF Windows服务,则需要使WCF服务主机可配置为在控制台中运行或作为Windows服务运行。 When you have run in console turned on, you can start debugging from visual studio. 当您在控制台中运行时,您可以从visual studio开始调试。

Create an app setting called "RunInConsole." 创建一个名为“RunInConsole”的应用程序设置。 In your service host startup method, have the following code: 在服务主机启动方法中,具有以下代码:

public class MyWindowsService : ServiceBase
{
    public static void Main(string[] args)
    {
        // if configuration says to run in the console, run the service in a console app. otherwise, use windows
        // service to host application
        if (ConfigurationManager.AppSettings["RunInConsole"] == "true")
        {
            using (ServiceHost host = new ServiceHost(typeof(MyService)))
            {
                host.Open();
                Console.WriteLine("Press <Enter> to terminate the Host application.");
                Console.ReadLine();
            }
        }
        else
            ServiceBase.Run(new MyWindowsService ());
    }

}

On all environments you deploy to, you'd always have this config setting set to false or else the service will fail to start, but when debugging locally you'd set it to true. 在部署到的所有环境中,您始终将此配置设置设置为false,否则服务将无法启动,但在本地调试时,您将其设置为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM