Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 Automatic Attach To Process

I am using visual studio 2010, my application has a multiu layer architect,

MainUI, WCFService, BLL and 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). 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.

Thanks

like image 833
Manvinder Avatar asked Jan 27 '12 07:01

Manvinder


4 Answers

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.

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.

like image 125
Rob Smyth Avatar answered Sep 17 '22 15:09

Rob Smyth


Sample howto start a process and attach it to Visual Studio 2010 with EnvDTE(Version is relevant).

//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...
    }
}
like image 32
Manuel Avatar answered Sep 17 '22 15:09

Manuel


Try using System.Diagnostics.Debugger.Break() in the code. If a debugger is not attached, then running that code will ask to attach a debugger and you can choose the existing instance.

like image 38
akhisp Avatar answered Sep 19 '22 15:09

akhisp


Have you tried System.Diagnostics.Debugger.Launch() in your service you would like the debugger to attach to?
http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx

like image 26
David Anderson Avatar answered Sep 17 '22 15:09

David Anderson