WCF Multi-layer Services Development with Entity Framework(Fourth Edition)
上QQ阅读APP看书,第一时间看更新

Debugging a WCF service from a client application

Now that we have a fully working WCF service, let's have a look at the debugging options of this service. We will use our original client, HelloWorldClient, to discuss the debugging options; so, in the following sections, whenever you come across terms such as client, the client program, or the client application, refer to HelloWorldClient.

The first and the most common scenario is to start a client program in the debug mode and then step into our WCF service. Next, we will try this option to debug the service.

Starting the debugging process

Follow these steps to start the debugging process from the client application:

  1. Start Visual Studio as an administrator and open the HelloWorld solution.
  2. Change the client program's configuration file to call HelloWorldService hosted within IIS Express. Open the App.config file inside the HelloWorldClient project and set the address of the endpoint to http://localhost:55859/HostExpressServer/HelloWorldService.svc.

    Note

    Remember that the port number can be different in your environment. You should change it to the port that IIS Express has assigned to your service.

  3. In the Solution Explorer, right-click on the HelloWorldClient project and select Set as Startup Project from the context menu.
  4. Open the Program.cs file inside the HelloWorldClient project and set a breakpoint at the following line:
    var client = new HelloWorldServiceClient();

    Your screen will look as follows:

  5. Now, press F5 or select menu options DEBUG | Start Debugging to start the debugging process.

Debugging the client application

When we start debugging our application, the cursor stops at the breakpoint line, as you can see in the following HelloWorld (Debugging) screenshot. The active line is highlighted, and you can examine the variables just as you can do for any other C# applications:

At this point, the channel between the client and the hosting server (HostExpressServer) has not been created. Press F10 or select menu options DEBUG | Step Over to skip over this line. If you don't have the menu options DEBUG | Step Over, you might have to reset your development environment settings through the menu options TOOLS | Import and Export Settings… (check appropriate options in Import and Export Settings Wizard).

Now, the line following the highlighted line in the preceding screenshot should be active and highlighted. At this point, we have a valid client object, which contains all of the information related to the WCF service such as the channel, the endpoint, the members, and the security credentials.

The following screenshot shows the details of the endpoint of the client variable:

Stepping into the WCF service

Now, press F11 to step into the service code. The cursor now resides on the opening bracket of the GetMessage method of HelloWorldService. You can now examine the variables and their values inside HelloWorldService just as you would for any other program. Keep pressing F10 and you should eventually come back to the client program.

However, if you stay inside the HelloWorldService project for too long, when you come back to the HelloWorldClient project, you will get an exception which says that it has timed out. This is because, by default, the HelloWorldClient will call the HelloWorldService and wait for a response for a maximum time of one minute. You can add an attribute, sendTimeout/receiveTimeout, to the binding and set it to a higher value in the App.config configuration file, depending on your own needs.

You might also have noticed that you don't see the output window of the HelloWorldClient. This is because, in the debug mode, once a console application finishes, the console window is closed. You have to add one line at the end of Program.cs to wait for a keystroke so that you can look at the output before it closes. You can do this by adding the following line of code:

Console.ReadKey();

Note

You can step into the service from the client only if TOOLS own the source code of the service and the service is hosted on the same machine as the client program. In certain environments, you might not be able to step inside the service directly from the client application due to some debugging settings of your Visual Studio or IIS Express. In this case, you can use the techniques we will learn in the Attaching the debugger to a running WCF service process section, later in this chapter, to debug the service.