Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug into WCF Service

So we run in an SOA architecture. I have a service that I'm trying to debug into a call that comes from a WinForms app in a different solution.

In this winforms app I have referenced the service on localhost correctly in the app.config, and now I want to start an instance of the WCF service so I can set a breakpoint and step through it.

When I go into the service, I right click the project, go to properties, and under 'Start Action' I choose the .exe file in the services bin/debug/ directory. Then I save, compile, and hit F5 to start an instance of it. I get this error:

enter image description here

What should I be doing?

like image 890
slandau Avatar asked Feb 24 '23 10:02

slandau


1 Answers

you have to host the service in a process and then debug it from there. This could be as simple as writing a console app to host the service, or writing a windows service to host it, or a windows forms app, or hosting it in IIS.

you can host in a console app like so:

static void Main(string[] args)
{
  using (ServiceHost host = new ServiceHost(typeof(YourNamespace.YourServiceInterface)))
  {
    host.AddServiceEndpoint(typeof(
YourNamespace.YourServiceInterface), new NetTcpBinding(), "net.tcp://localhost:9000/YourService");
    host.Open();

    Console.WriteLine("Press <Enter> to terminate the Host 
application.");
    Console.WriteLine();
    Console.ReadLine();
  }
}

this article shows how to host in a windows service. I would recommend adding

Debugger.Launch();

as the first line in the OnStart method so that you can attach the debugger when the service starts. This will help debug any startup issues. Otherwise you can just choose AttachToProcess from the Debug menuand attach to the running windows service.

you need to add using System.Diagnostics to use the Debugger.Launch(); method

like image 150
Sam Holder Avatar answered Feb 25 '23 23:02

Sam Holder