Click here to Skip to main content
15,395,065 members
Articles / Database Development / SQL Server / SQL Server 2008R2
Tip/Trick
Posted 26 Oct 2014

Stats

7.6K views
6 bookmarked

SOAP message in WCF services.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Oct 2014CPOL5 min read
Describe about the SOAP message in WCF with example.

Introduction

In this tip we will elaborate about the SOAP message in WCF communication. SOAP stands for simple object access protocol. In WCF the main thing is that the communication between the server and client. The communication takes place by messages with some transport layer. The main need of calling a service is to do the data transfer between the server and client. Then the data is transferred in the form of messages. In WCF, the message is  a SOAP type.  So lets observe the SOAP message in a  WCF services. This tip only for newbie.

Background

 Before starting reading on this tip the reader should have some basic knowledage on WCF . In this tip we  will explain/observe on the SOAP message with some examples.

   The main intension to call a service is to send or retrive some data from the service method. In the service the data is transferred in the form of message . So message is not but a package of data which is  transferred in SOAP format or  that is you can say also SOAP message.

  SOAP message is not but a XML data which is transferred in XML format. The XML format has some Header and Body parts . So when ever the client is calling to WCF service method the  in Request  the data is transferred in XML format. So when ever the server give some responses to the client the data is transferred in XML format.

Image 1

 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 data transferred. 

We will check the contain of the XML format later in this tip .

Using the code

 To observe the SOAP message in WCF service we will create a WCF service application and check step by step.

 Step-1 :

      In step- 1 we will create WCF service application in our .NET framework. Open the visual studio framework and then select a new project -> then select WCF -> then select WCF service application -> then give the name of the application as "SoapMessage" -> then click on "OK" button.

Image 2

Step-2:

    After the creating the service application you will see the newly created application like this image. In this application you will see there would be one .cs file like IService1.cs  which implements the interface and the    operation contract.

Then the file Service1.svc.cs file define the the operation contract which is definded in IService1.cs file.

Image 3

Step-3:

    In this step we will add the  code base to our application .

  Before adding any operation contract inside the service we need to declare an interface . After that we can declare our operation contract inside the interface.

   So in this example our interface name is "IService1" and our operation contract is "AppendTwoString". The operation contract is nothing but just like an method which is exposed in the service under the servicecontract .  So if we expose the ServiceContract  in service then what ever operation contract belongs to this ServiceContract  will be available in the client. So basically the service contact is encapsulated all the things in a single unit. So lets put the below code inside the your IService1.cs file.

C++
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace SoapMessage
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string AppendTwoString(string firstName, string secondName);
    }
}

Step-4:

    So in Step-3 we  just define the operation contract "AppendTwoString()". So in this step we will  define the operation contract inside Service1.svc.cs file.

   In Service1.svc.cs  file , class Service1 inherits the interface IService1 , so we can define the "AppendTwoString()" operation contract here. So this "AppendTwoString()" we are passing two string parameters as input from the client application and we will append two string and return the resulted string to the requested client application as response. So in request we will get two parameter as input and in response we will return one string value as output.

C++
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace SoapMessage
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string AppendTwoString(string firstName, string secondName)
        {
            return firstName + " " + secondName;
        }
    }
}

Step-5 :

     As we add all the code to the application then its time to check the SOAP message by WCF Test client. So build the application and Run it on your Visual studio framework. After running the WCF test client you will see the screen like thisImage 4

Step-6:

After that  please follow these steps:

  a. Click on the  "AppendTwoString()" method .

  b. Provide some input in the Request "value" column.

  c. Then click on the Invoke button.

  d.  Check the service response in the Response "Value" column.

  You can also check the steps from the Image also.

Image 5

So in this step you just provided some value in the method and got some resulted string value.

Step-7 :

   In step-6 we just given some input string and found the output string in Formatted tab. To check the SOAP message that is XML data , click on the XML Tab and you will see the XML data like this.

Image 6

As we discussed above that the there would be two XML data section like Request and Reponse data section . Both section have Header and Body element.

You can mark that in Request section there is two parameter like  <firstName> and   <secondName> having some value in the Body element. But in the Response XML section also the output of the sevice is coming under the Body element.

So this your SOAP message which helps you to transferred your data in WCF service communication.

I have attached the same demo application in this tip . If you want to check  you can download it.

Hope this tip will clear doubts on the SOAP message and how is it transferred in the WCF services. Please have  some comments if you got some idea after reading this tip . Coming with some new interesting tips on WCF . Happy Coding :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bimaln
Software Developer Mindfire Solutions
India India
Software engineer in Mindfire Solutions.

Comments and Discussions

 
-- There are no messages in this forum --