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 WCF 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.
- using System.ServiceModel;
- namespace wcfService
- {
- [ServiceContract]
- public interface IMathService
- {
- [OperationContract]
- int AddTwoNo(int FirstNo, int Second);
- }
- }
Step 4
Let's make changes to MathService.svc.cs and implement the IMathService interface.
- using System;
- namespace wcfService
- {
- public class MathService : IMathService
- {
- public int AddTwoNo(int FirstNo, int Second)
- {
- return FirstNo + Second;
- }
- }
- }
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
- 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.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";
- }
- <h2>Index</h2>
- @using(Html.BeginForm("Index","Home",FormMethod.Post))
- {
- <div>
- <span>First No :</span> @Html.TextBoxFor(m=>m.FirstNo)
- </div>
- <div style="clear:both; height:1px; width:1px;"></div>
- <div>
- <span>Second No :</span> @Html.TextBoxFor(m=>m.SecondNo)
- </div>
- <div style="clear:both; height:1px; width:1px;"></div>
- <div>
- <input type="submit" value="Add Number" /><br />
- <span>Result : @ViewBag.Result</span>
- </div>
- }
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 project 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.