Observe SOAP Message in WCF Communication

Introduction 

 
This series is explaining WCF services. We have learned how to host and consume a simple WCF service and many more.
 
In this chapter, we will learn to observe a SOAP message in WCF communication. My previous chapter explained that WCF is one example of a Service Oriented Architecture (SOA). 
 
This means that all the information passes in the form of a message. The message is nothing but the Simple Object Access Protocol (SOAP). This is a standard message format to exchange data between a client and a server. Again, the SOAP message is nothing but XML. We know that XML is truly platform-independent and is used to pass data from one end to another end without depending on the technology and platform. For this behavior of XML, the industry has chosen it as a standard for communication.
 

How SOAP messages are passed in WCF?

 
This is the main purpose of this quick chapter, we will observe a SOAP message in a request and response. To understand this we need to create a simple WCF application and run it in a .NET environment (or you can choose your own hosting environment).
 
Step 1: Create a WCF application
 
Open Visual Studio and go to the WCF section. Select the "WCF Service Application". Please don't select the "WCF Class library" or any other template because we will run this application in the VisualStudio environment.
 
WCF Service Application 
 
Step 2: Configure the service
 
Once we open the default template, we will find that one sample configuration already exists and we will modify this configuration as in the following.
 
Code for ServiceContract (Interface)
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;   
  8. namespace TestServive  
  9. {  
  10.     [ServiceContract]  
  11.     public interface IService1  
  12.     {  
  13.         [OperationContract]  
  14.         string AddName(String name,string surname);  
  15.     }  
  16. }  
This ServiceContract is very simple, it just has one method defined in it. It takes two string parameters and returns one string.
 
Implementation in Class
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;   
  8. namespace TestServive  
  9. {  
  10.     public class Service1 : IService1  
  11.     {  
  12.         public string AddName(String name,string surname)  
  13.         {  
  14.             return name + surname;  
  15.         }  
  16.     }  
  17. }  
We have implemented the "IService" interface in the "Service1" class. And the "AddName()" method takes a name and surname then returns the full name by adding them.
 
Step 3: Run the application
 
We have created our service and now we will run it to consume the service and then we will see the structure of the SOAP message.
 
structure of SOAP message 
 
Once we run this application, we will encounter the following screen. Here we need to supply data to invoke the service. In this example, I have provided "Sourav" and "Kayal" for the name and surname. Then press the "invoke" button and you will see the following screen.
 
SOAP message structure 
 
This screen is showing the result string. And we are seeing that the service has concatenated the name and surname and returned the result.
 
Now we will see the SOAP message for this communication. Go to the XML section (in the bottom portion of the output section) and you will find the following screen.
 
SOAP message 
 
So, this is the SOAP message that does the communication.
 

Conclusion

 
In this example, we saw how a SOAP message exchanges data in a WCF application. As we said earlier, the SOAP message is nothing but XML and in the output, we are experiencing that. Remain with this series, we will explain more about WCF.
Next » Data Contracts in WCF