You are in the "Understand WCF" article series. This series is explaining WCF services. We have learned how to host and consume a simple WCF service and many more.
You can read them here.
In this article we will learn to observe a SOAP message in WCF communication. My previous article 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 standard message format to exchange data between a client and 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 article, 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 "WCF Service Application". Please don't select "WCF Class library" or any other template because we will run this application in the VisualStudio environment.
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)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- namespace TestServive
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- string AddName(String name,string surname);
- }
- }
This ServiceContract is very simple, it just has one methd defined in it. It takes two string parameters and returns one string.
Implementation in Class
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- namespace TestServive
- {
- public class Service1 : IService1
- {
- public string AddName(String name,string surname)
- {
- return name + surname;
- }
- }
- }
We have implemented the "IService" interface in the "Service1" class. And the "AddName()" methid 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..
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.
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.
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.