16
votes

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

10
Have you considered recording a macro and binding it to a short-cut?Strelok
This is still a manual process to use shortcut everytime i execute the application and wants to debug it, something completely automatic is what i want.MegaMind
are you compiling and running all these projects from within Visual Studio? (including the WCFService)? Are you therefore restarting your WCFService for each compile?wal
If yes to any of these questions then it is probably better to run your WCFService as a Console application so it can be easily started/debugged from within Visual Studio. You can modify the Console App (can expand on this when you respond) so that it can also be run as a windows service easily (see Environment.UserInteractive)wal

10 Answers

16
votes

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.

4
votes

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...
    }
}
1
votes

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.

1
votes

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

0
votes
  1. In the properties page for the wcf service project, select the Web tab.
  2. Select 'Start External Program' for the start action, and choose MainUI.exe.
  3. Set the working directory as the folder that MainUI.exe is in (probably a bin folder).
  4. Set a break point and press f5 to start debugging.
0
votes

If I understand correctly, Macro may be answer:

in Vs:

  1. Tools->Macros->record TemporarilyMacro (Ctrl+shift+r)
  2. Attach VS to process as usual (ctrl+alt+p)
  3. Stop recording macro (ctrl+shift+r)
  4. Go to View->Other Windows->Macro Explorer (CTRL+F8)
  5. find your Temporarily Macro (somewhere in MyMacros->RecordingModule) and rename it
  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 ;))
  8. Push OK
  9. Be Happy
0
votes

Have you tried using the WCFSvcHost.EXE that comes with Visual Studio to launch the BLL and DAL service? 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." The default installation is C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE. You can configure it to use your MainUI app as the client. 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. If you rather here is the link to help on MS web site: Using WCF Service Host (wcfSvcHost.exe).

0
votes

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. When you have run in console turned on, you can start debugging from visual studio.

Create an app setting called "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.

0
votes
0
votes

Personally I prefer to use Debugger.Launch() as suggested here in this thread, because it doesn't need for references to the DTE (that's IDE-specific and must be explicitly referenced into the project to be used)