简体   繁体   中英

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:

在此输入图像描述

What should I be doing?

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

您必须将本地构建的服务作为Windows服务安装,然后附加到该服务。

Is the service running under IIS or self-hosted? Either way once the service is running you can attach to it by going into Debug->Attach Process.

If running under IIS you need to look for the w3wp.exe (IIS 7) process. Note that to attach to this process you will most likely have to run VS as an administrator. If you are running an older version of IIS, the process is called something like aspnet_XXX.

Once you are attached you can put in your breakpoints and debug as needed.

Well, you got that error when you try to run/debug a class library project, so check that your services project is not that kind.

If you want to make it easy (and there's no design infraestructure problem) to run/debug from the VS then make it a WCF service application project which hosts the services in a website like project: https://dl-web.dropbox.com/get/Photos/web/wcfserviceapp.png?w=44e8c6ed

This way you can run the service in its solution and then run the winforms app from its solution, then you can go to menu "Debug/Attach to process..." and look for the service url and port: https://dl-web.dropbox.com/get/Photos/web/debugattachtoprocess.png?w=8c917c28

I hope this helps, if not please elaborate to have a more clear idea of what kind of projects do you have, how is your solution and other usefull information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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