Tracing in WCF – Part 8

In this article we learn what is tracing and how we can implement in wcf.

Before reading this article, I highly recommend reading my previous parts:

According to MSDN Tracing enables you to view diagonastic information about a single request. It enables you to follow a service execution path , display diagonastic information at runtime and debug your application.
Lets create a wcf application and name it wcfService and done these steps. This service is performing add two integer number.
Step 1: Delete default Service1.svc and IService.cs.
Step 2: Add new svc file and name it MathService
Step 3: Delete the default service DoWork and Add new service. My IMathService.cs file look like

using System.ServiceModel;
namespace wcfService
{
[ServiceContract]
public interface IMathService
{
[OperationContract]
int AddTwoNo(int FirstNo, int Second);
}
}

Step 4: Lets make changes in MathService.svc.cs. Lets implement IMathService interface.

using System;
namespace wcfService
{
public class MathService : IMathService
{
public int AddTwoNo(int FirstNo, int Second)
{
return FirstNo + Second;
}
}
}

As we know, default binding of wcf services is httpbasicbinding. Lets run the application and test that it works fine or not.

selfHostwcfAppOutput

 

Assign the value of FirstNo, Second 5 and 8 respectively and hit invoke button and see the output its 13. This means my service is working fine.

Now add a new mvc project in solution mvcClient. I am going select template as basic. You can make it as indivisual as well. Now add a service reference. If you are new to add reference please read my article Introduction to wcf.
After adding the reference of my service i am going to add Model,view and controller AddNumber,Index and Home respectively.
In AddNumber.cs

namespace mvcClient.Models
{
public class AddNumber
{
public int FirstNo { get; set; }
public int SecondNo { get; set; }
}
}

In HomeController.cs

using System.Web.Mvc;
namespace mvcClient.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult Index(Models.AddNumber addNumber) {
// MathServiceReference is reference object name which is we are provide when i am trying to add web reference in my project
MathServiceReference.MathServiceClient client = new MathServiceReference.MathServiceClient();
int Result = 0;
Result = client.AddTwoNo(addNumber.FirstNo, addNumber.SecondNo);
ViewBag.Result = Result;

return View();
}
}
}

In Index.cshtml

@model mvcClient.Models.AddNumber

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

Index

@using(Html.BeginForm("Index","Home",FormMethod.Post)) { <div>First No : @Html.TextBoxFor(m=>m.FirstNo)</div> <div style="clear: both; height: 1px; width: 1px;"></div> <div>Second No : @Html.TextBoxFor(m=>m.SecondNo)</div> <div style="clear: both; height: 1px; width: 1px;"></div> <div> <input type="submit" value="Add Number" /> Result : @ViewBag.Result }

After make these changes i am going to run this application and see the output.

Output:

serviceOutput

Service is running successfully.
Now we are going to enabling tracing in our application with following steps.
Step 1: Right click on web.config which is located in mvcClient poject and click on “Edit WCF Configuration” option.

Edit WCF configuration

Step 2: Click on Diagnostics option.

Diagnostics

Step 3: Click on Enable log auto flush link, messageLogging and Enable tracing link.

enable tracing

See after clicking on enable tracing link the configuration edito show the log file location. In my case its “C:/pramod/2013/mvcClient/mvcClient” and file name is we_tracelog.svclog.

Step 4: Expand the Diagnostics and click on Messege Logging and make LogEntireMessage as true and close the editor.

MessageLogging

Step 5: After click on close button there is a message prompt click on Yes.

Close configuration

Lets run our application and set firstno as 4 and second as 8 and see the output. Output is as expected 8.
Lets open the “web_messages.svclog” which is located at “C:\pramod\2013\mvcClient\mvcClient”. Just double click on web_messages.svclog and it’s open.

Step 6: Click on last one activity and select messge tab. We are able to se what parameter i am passing, if i select the second option in description then it shows the result.

trac1

I hope you will enjoy this article.
Happy coding.

Tags: , , ,