Debugging the HelloWorld WCF service

Now that we have a fully-working WCF service, let us have a look at the debugging options of this service.

Debugging from the client application

The first and most common scenario is to debug from the client program. This means that you start a client program in debug mode, and then step into your WCF service.

Starting the debugging process

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

  1. Change the client program's web configuration file to call the HelloWorldService hosted within the ASP.NET Development Server. Open the file app.config inside the HelloWorldClient project, and set the address of the endpoint to this address:
    http://localhost:8080/HostDevServer/HelloWorldService.svc
    
  2. In the Solution Explorer, right-click on the HelloWorldClient project, and select Set as Startup Project from the context menu.
  3. Open the Program.cs file inside the HelloWorldClient project, and set a breakpoint at the following line:
    HelloWorldServiceClient client = new HelloWorldServiceClient();
    
    Starting the debugging process
  4. You can set a breakpoint by clicking on the gray area of the left of the line (the little ball in the diagram above), pressing F9 while the cursor is on the line, or selecting the menu item Debug | Toggle Breakpoint. You should ensure that the breakpoint line is highlighted, and if you hover your mouse over the red breakpoint dot, an information line will pop up.
  5. Now press F5, or select menu option Debug | Start Debugging, to start the debugging process.

As soon as you press F5, you will notice a little window pop up in the lower-right corner of the screen, as shown in the following image:

Starting the debugging process

This is because the client program HelloWorldClient is referencing HelloWorldService, which is hosted in the ASP.NET Development Server, and you have the project property Always Start When Debugging set to True.

Starting the debugging process

Note that this setting is for the WCF hosting project, not for the client or WCF service project. This is very useful when debugging, because you don't need to start it explicitly. However, sometimes, it might be annoying, especially when you have several hosting projects within the same solution. In this case, you can turn it off by setting it to False. However, you then have to start the Service prior to debugging the client application. Otherwise, you will get an exception. We will discuss more about this, later in this chapter.

Debugging on the client application

The cursor should have stopped on the breakpoint line, as you can see in the following HelloWorld (Debugging) image. The active line is highlighted, and you can examine the variables just as you do for any other C# applications.

Debugging on the client application

At this point, the channel between the client and the hosting server (HostDevServer) hasn't been created. Press F10, or select menu option Debug | Step Over to skip over this line. If you don't have the menu option Debug | Step Over, you may have to reset your development environment settings via menu option Tools | Import and Export Settingsā€¦ (select General Development Settings from the Import and Export Settings Wizard, and check all of the available options).

Now, the following line of source code 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 Locals image shows the details of the Endpoint local variable.

Debugging on the client application

Enabling debugging of the WCF service

Let us press F11 to step into the WCF service. But instead of stepping in, we will receive an error message, as shown in the following image stating that the service debugging is not enabled:

Enabling debugging of the WCF service

This is because we haven't enabled debugging for the HostDevServer application. Click the OK button to dismiss this dialog, and then press Shift+F5, or select menu option Debug | Stop Debugging, to return to the development mode.

To enable the debugging of HostDevServer, open the web.config file inside the HostDevServer project and add following nodes to this file:

<system.web>
<compilation debug="true"/>
</system.web>

Above system.web, nodes should be added as child nodes of the root node <configuration>, and the content of the web.config file should now be like this:

<?xml version="1.0"?>
debugging from client application, HelloWorld WCF servicedebugging, enabling<configuration>
<appSettings>
<add key="HTTPBaseAddress" value=""/>
</appSettings>
<system.serviceModel>
<services>
<service name="MyWCFServices.HelloWorldService" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="wsHttpBinding" contract="MyWCFServices.IHelloWorldService"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>

Stepping into the WCF service

Now, press F5 to start debugging again. Press F10 to skip the first line, and then press F11 to step into the service code. The cursor now resides on the opening bracket of the GetMessage method of the HelloWorldService. You can now examine the variables inside HelloWorldService, just as you would for any other programs. Keep pressing F10, and you should eventually come back to the client program.

Stepping into the WCF servicedebugging from client application, HelloWorld WCF servicedebugging, enabling

However, if you stay inside the HelloWorldService for too long, when you come back to HelloWorldClient, you will get an exception window saying that it has timed out. This is because, by default, the HelloWorldClient will call HelloWorldService, and wait for a response for a maximum time of one minute. You can change this to a longer value in the configuration file web.config, depending on your own needs.

Stepping into the WCF servicedebugging from client application, HelloWorld WCF servicedebugging, enabling

You may also have noticed that you don't see the output window of the HelloWorldClient. This is because, in debug mode, once a console application finishes, the console window is closed. You can add one line to 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();

Debugging only the WCF service

In the previous section, we started debugging from the client program, and then stepped into the service program. Sometimes, we may not want to run the client application in debug mode. For example, if the client application is a third-party product we won't have the source code, or the client application may be a BPM product that runs on a different machine. In this case, if we need to, we can run the service in debugging mode and debug only the service.

Starting the WCF Service in debugging mode

To start HelloWorldService in the debug mode, first set HostDevServer as the startup project. Then open HelloWorldService.cs from the HelloWorldService project and set a breakpoint at the line inside the GetMessage method, as shown below.

Starting the WCF Service in debugging mode

Now press F5 to start the service in debugging mode.

Once you press F5, the WCF service will be running in debugging mode, waiting for requests. A browser will open displaying all of the files under the HostDevServer folder. If you go back to Visual Studio IDE, you may find that a new solution folder, Script Documents, has been added to the solution. This folder is the actual content of the web page being displayed in the browser. Because its content is dynamically generated, this folder will only be included in the solution when the HostDevServer is being debugged. Whenever you stop the debugging session, this folder will go away automatically.

Starting the WCF Service in debugging mode

After you press F5 to start a WCF service in debugging mode, you might see an error message warning you that script debugging is disabled, as shown the following Script Debugging Disabled image:

Starting the WCF Service in debugging mode

For this dialog box, you can do as instructed (clear the checkbox from the Internet Explorer under Tools | Advanced | Browsing | Disable Script Debugging), or you can just click the Yes button to continue debugging without enabling script debugging for Internet Explorer, because we will not debug any script from Internet Explorer (our application is not a web application).

Once you clicked the Yes button (or you may never see this message box because you have the correct settings), the service will be started in debugging mode.

Starting the client application in non-debugging mode

Now that we have the WCF service running in debugging mode, we need to start the client application in non-debugging mode so that the debugging process can start from the WCF service side, and not from the client side.

For this example, you can't start the HelloWorldClient program from the same Visual Studio IDE instance. The reason for this is that, once you have started the HelloWorldService in debugging mode, the solution is in running status. You can't start another project from the same solution inside the same Visual Studio instance while the HelloWorldService project is running. Actually, the Set as Startup Project menu option is disabled, making it impossible to set any other project as the startup project. Also, a bunch of other menu options are disabled, meaning that you can't change them while in debugging mode.

There are two ways to start the HelloWorldClient program in non-debugging mode. The first one is to start it in another instance of Visual Studio. While leaving the previous instance of Visual Studio running for the HelloWorldService in debugging mode, start a new Visual Studio instance, and open the HelloWorld solution. Set HelloWorldClient as the startup project, and then press Ctrl+F5 to start it in non-debugging mode. As soon as you press Ctrl+F5, you will see that the previous Visual Studio is active and the cursor has stopped on the breakpoint line. You can now examine all of the variables inside HelloWorldService, as you would do for any other program. Press F10 once and you will be taken to the end of the GetMessage method; press F10 again and you will be taken outside of the HelloWorldService project. Because the HelloWorldClient is now not running in debugging mode, you will see the output window immediately.

Another way to start HelloWorldClient is to start it from Windows Explorer. Go to the D:SOAwithWCFandLINQProjectsHelloWorldHelloWorldClientinDebug directory and double click HelloWorldClient.exe file. You will then get the same result as you did when you started it from inside a new Visual Studio instance.

Starting the WCF service and client applications in debugging mode

What if you start HelloWorldClient in debugging mode, while HelloWorldService is also running in the debugging mode? Suppose you have started HelloWorldService in debugging mode, have set a breakpoint inside the GetMessage method, and have attached a debugger to the HelloWorldService. Now, if you start another Visual Studio instance, open the solution, set HelloWorldClient as the startup project, and press F5 to start HelloWorldClient

also in debugging mode, you will get an exception, as shown in the following image indicating that you attach to the server process:

Starting the WCF service and client applications in debugging mode

The main reason for this is that, by default, HostDevServer has the setting Always Start When Debugging set to True. Because of this setting, when HelloWorldClient is started in debugging mode, it also tries to start the service. However, as we have started it in another Visual Studio instance, it will not start a new one. Instead, it will just re-use the existing one. Now, when the breakpoint inside the HelloWorldService is hit, the second Visual Studio instance will try to attach to the HelloWorldService process, which fails because the first Visual Studio instance has already attached a debugger to it.

To overcome this, just change the setting Always Start When Debugging to False, and control the startup of each project manually, or when you start debugging from the client program, don't start the service in advance. The following Properties image shows the setting of this property:

Starting the WCF service and client applications in debugging mode

Attaching to a WCF service process

The third common scenario for debugging is when attaching to a running WCF service. Suppose that HelloWorldService is hosted and running outside Visual Studio, either in IIS or a managed application such as HostCmdLineApp. The client application is also running outside of Visual Studio. At a certain point, you may want to start debugging the running WCF service. In this case, we can attach to the WCF service process, and start debugging from the middle of a process.

Running the WCF service and client applications in non-debugging mode

To try this scenario, change the app.config file to use the IIS hosting HelloWorldService. This means that we use the following address for the endpoint in the app.config file for the HelloWorldClient project:

http://localhost/HelloWorldService/HelloWorldService.svc

Build the solution, and set a breakpoint inside the GetMessage method of the HelloWorldService project. Then, run the <ltieral>HelloWorldClient</ltieral> in non-debugging mode by pressing Ctrl+F5. You will see there is no way to hit the breakpoint we had previously set inside HelloWorldService. This is because the service is now hosted by the IIS, and it is not under debugging by any debugger.

Debugging the WCF service hosted in IIS

To debug the service hosted by the IIS, we can attach it to the IIS process. Start Visual Studio, select menu option Debug | Attach to Processā€¦. The Attach to Process window should now appear. If you can't see the Debug menu from Visual Studio, just open any project or, create an empty new project.

Debugging the WCF service hosted in IIS

Select process aspnet_wp.exe from the list of available processes, and click the Attach button. You will find this process attached to the debugger. Open the HelloWorldService.cs file and set a breakpoint if you haven't done so already. Now run the HelloWorldClient program in non-debugging mode (use Ctrl+F5) from another Visual Studio instance or from Windows Explorer, and you will see that the breakpoint is now hit.

When you have finished debugging HelloWorldService using this method, you can select menu option Debug | Detach All or Debug | Stop Debugging to exit debugging mode.

You may also have noticed that when you attach to aspnet_wp.exe, the ASP.NET Development Server is also started. We will not use it at all at this time. This is again because the Always Start When Debugging property of HostDevServer is set to True, and as we did earlier, you can turn it off if you feel it is annoying.

Just-In-Time debugger

As you can see, we have to start the HelloWorldService before we can run the client program. The actual step to start the HelloWorldService varies depending on the hosting method that you are using. For example, if you are hosting HelloWorldService in a managed application as we did for HostCmdLineApp, you have to start the application manually. If you are hosting HelloWorldService in the ASP.NET Development Server, you can manually start it from Visual Studio, or set Always Start When Debugging to True. If you are hosting HelloWorldService in IIS, you don't need to do anything (except to make sure that the IIS service has been started). Lastly, if you host HelloWorldService in a Windows service, you should set its startup type to automatic, or you will have to manually start it.

What happens when you run the client program, the service is not started, and it is not set to automatically start when being referenced? For example, if you have hosted HelloWorldService in IIS, and for some reason IIS has been stopped, then what will happen to the client program?

To try this, we need to first stop IIS. There are several ways to stop IIS, and one of them is to open a command line window (via menu option Start | All Programs | Accessories | Command Prompt) and run the following command: Net Stop W3SVC, as shown in following image:

Just-In-Time debugger

Once IIS has been stopped, HelloWorldService is no longer accessible. If you start the HelloWorldClient program now, you will get an error. Depending on the mode in which you are running HelloWorldClient, you will get two different errors.

First, if you start HelloWorldClient in debugging mode (by pressing F5) from Visual Studio, it will stop on the line to call the GetMessage method, showing you an exception. This is because the client program can't connect to the server (the server has actively refused it). As we haven't added any code to handle exceptions, .NET runtime throws an unhandled exception. We will discuss exceptions (WCF Fault Contracts) in one of the following chapters. For now, you have to select menu option Debug | Stop Debugging to stop the client program.

Just-In-Time debugger

If you start HelloWorldClient in non-debugging mode (by pressing Ctrl+F5, or by double-clicking the HelloWorldClient.exe file from Windows Explorer D:SOAwithWCFandLINQProjectsHelloWorldHelloWorldClientinDebug), you will see the Visual Studio Just-In-Time Debugger screen.

Just-In-Time debugger

This is a nice feature of the .NET framework, because even though we have started the program in non-debugging mode, we can still step into the codesif something unexpected happens (however, the executable must be built with debugging information, that is, not a release one). In this case, if you click the Yes button, the Visual Studio window with the HelloWorld solution will be active, and you will see the same image as when you started the debugging process from Visual Studio. So, you know it is due to HelloWorldService. This will be very helpful when you are testing a big application, as you don't need to restart your program in debugging mode and repeat what you've done to reach the same problem spot. Instead, you can start the debugging process right on the spot, and then fix it quickly if it is only a configuration problem.

In the above example, if the client program is started from Windows Explorer, and the HelloWorld solution is not open in any Visual Studio IDE, it may even offer to start a new instance of Visual Studio for debugging. If you have multiple versions of Visual Studio .NET IDEs installed, it will list all of them for you to pick one. It is better to choose Visual Studio 2008 because, if you choose others, you may run into some unexpected problems.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset