Tracing in WCF: Part 8

Before reading this article, I suggest you read the previous parts of this series.

According to the MSDN, Tracing enables you to view diagnostic information about a single request. It enables you to follow a service execution path, display diagnostic information at runtime and debug your application.

Let's create a ECF application and name it wcfService. Now here is the procedure to add two integer numbers.

Step 1

Delete the default Service1.svc and IService.cs.

Step 2

Add a new svc file and name it MathService.

Step 3

Delete the default service DoWork and add a new service. My IMathService.cs file will look like the following.

  1. using System.ServiceModel;  
  2. namespace wcfService  
  3. {  
  4.     [ServiceContract]  
  5.     public interface IMathService  
  6.     {  
  7.         [OperationContract]  
  8.         int AddTwoNo(int FirstNo, int Second);     
  9.     }  
  10. }  

Step 4

Let's make changes to MathService.svc.cs and implement the IMathService interface.

  1. using System;  
  2. namespace wcfService  
  3. {  
  4.     public class MathService : IMathService  
  5.     {  
  6.         public int AddTwoNo(int FirstNo, int Second)  
  7.         {  
  8.             return FirstNo + Second;  
  9.         }  
  10.     }  
  11. }  
As we know, the default binding of the WCF service is httpbasicbinding. So, let's run the application and test that is it working correctly.



Assign the value of the first and second numbers as 5 and 8 respectively and hit the Invoke button. The result is 13. That means that the service is working correctly.

Next, add a new MVC project to the solution mvcClient. I will select the template as basic. As per your requirements, it can be made individual.
 
Now, add a service reference. If you are a beginner in adding service references then please read my article on Introduction to WCF.

After adding the reference of my service, I will add a Model, View and Controller that are AddNumber, Index and Home respectively.

In AddNumber.cs
  1. namespace mvcClient.Models  
  2. {  
  3.     public class AddNumber  
  4.     {  
  5.         public int FirstNo { getset; }  
  6.         public int SecondNo { getset; }  
  7.     }  
  8. }  
In HomeController.cs
  1. using System.Web.Mvc;  
  2. namespace mvcClient.Controllers  
  3. {  
  4.     public class HomeController : Controller  
  5.     {  
  6.         [HttpGet]  
  7.         public ActionResult Index()  
  8.         {  
  9.             return View();  
  10.         }  
  11.   
  12.         [HttpPost]  
  13.         public ActionResult Index(Models.AddNumber addNumber) {  
  14.         // MathServiceReference is reference object name which is we are provide when i am trying to add web reference in my project   
  15. MathServiceReference.MathServiceClient client = new MathServiceReference.MathServiceClient();  
  16.             int Result = 0;  
  17.             Result = client.AddTwoNo(addNumber.FirstNo, addNumber.SecondNo);  
  18.             ViewBag.Result = Result;  
  19.               
  20.             return View();  
  21.         }  
  22.     }  
  23. }  
In Index.cshtml
  1. @model mvcClient.Models.AddNumber  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4.     Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
  6. <h2>Index</h2>  
  7. @using(Html.BeginForm("Index","Home",FormMethod.Post))  
  8. {  
  9. <div>  
  10. <span>First No :</span> @Html.TextBoxFor(m=>m.FirstNo)  
  11.     </div>  
  12.     <div style="clear:both; height:1px; width:1px;"></div>  
  13. <div>  
  14. <span>Second No :</span> @Html.TextBoxFor(m=>m.SecondNo)  
  15.     </div>  
  16.     <div style="clear:both; height:1px; width:1px;"></div>  
  17.     <div>  
  18. <input type="submit" value="Add Number" /><br />  
  19.         <span>Result : @ViewBag.Result</span>  
  20.     </div>  
  21. }  

After making those changes, I will run this application to generate the following output.


As shown above, the service is running successfully.

We will now enable tracing in our application with the following procedure.

Step 1

Right-click on web.config that is located in the mvcClient poject and click on the "Edit WCF Configuration" option.



Step 2

Click on the Diagnostics option.



Step 3

Click on the Enable Log Auto Flush link, MessageLogging and Tracing link.



After clicking the enable Tracing link, the configuration editor will show the log file location. In my case it is:

"C:/pramod/2013/mvcClient/mvcClient" and the file name is we_tracelog.svclog.

Step 4

Expand the Diagnostics and click on Message Logging. Now, set LogEntireMessage to true and close the editor.



Step 5

After clicking on the Close button, a message dialog box will will be shown. Just click on Yes.



Let's run our application and set the first to 4 and the second to 8. Now the output is expected to be 8.

Let's open the "web_messages.svclog" file that is located at
"C:\pramod\2013\mvcClient\mvcClient". Just double click on web_messages.svclog to open it.

Step 6

Click on the last activity and select the Message tab. We can now see what parameter we're passing. Now, if we select the second option in the description, the following result will be generated.



I hope you enjoyed the article.
Happy coding. 

Up Next
    Ebook Download
    View all
    Learn
    View all