Monday, 21 May 2018

SOAP


1) What are Hostings are there?When to use?

SOAP
  • SOAP stands for Simple Object Access Protocol
  • SOAP is an application communication protocol
  • SOAP is a format for sending and receiving messages
  • SOAP is platform independent
  • SOAP is based on XML
  • SOAP is a W3C recommendation

Why SOAP?

It is important for web applications to be able to communicate over the Internet.
The best way to communicate between applications is over HTTP, because HTTP is supported by all Internet browsers and servers. SOAP was created to accomplish this.

A SOAP message is an ordinary XML document containing the following elements:
  • An Envelope element that identifies the XML document as a SOAP message.
  • A Header element that contains header information.
  • A Body element that contains call and response information.
  • A Fault element containing errors and status information.

Syntax Rules

Here are some important syntax rules:
  • A SOAP message MUST be encoded using XML
  • A SOAP message MUST use the SOAP Envelope namespace
  • A SOAP message MUST use the SOAP Encoding namespace

Syntax

soap:encodingStyle="URI"

The SOAP Header Element

The optional SOAP Header element contains application-specific information (like authentication, payment, etc) about the SOAP message.
If the Header element is present, it must be the first child element of the Envelope element.
Note: All immediate child elements of the Header element must be namespace-qualified.<?xml version="1.0"?>


<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"
>


<soap:Header>
  <m:Trans xmlns:m="https://www.w3schools.com/transaction/"
  soap:mustUnderstand="1"
>
234
  </m:Trans>
</soap:Header>
...
...
</soap:Envelope

----------

2) How to Return a DataTable From WCF Service

[ServiceContract]public interface IService1{
    [OperationContract]
    Employee GetEmployee();
}
Then add DataContract and DataMember:
[DataContract]public class Employee{
    [DataMember]
    public DataTable EmployeeTable
    {
        get;
        set;
    }
}

In this article I am leaving binding to the default, wsHttpBinding.

Step 3: Add the following code in the Service1.svc.cs file inside Service1 class:
public class Service1 : IService1{
    string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter sda;
    DataTable dt;
    Employee emp = new Employee(); 
    public Employee GetEmployee()
    {             
        using (con = new SqlConnection(ConString))
        {
            cmd = new SqlCommand("SELECT EmployeeID, FirstName, LastName FROM Employees", con);
            sda = new SqlDataAdapter(cmd);
            dt = new DataTable("Paging");
            sda.Fill(dt);
            emp.EmployeeTable = dt;
            return emp;
        }        
    }
}
We have created our WCF service that will return Employees data as a DataTable. Now its time to consume this service in a Console application.

Step 4: Add a new Console Application named EmployeeServiceClient by right-clicking on the Solution Explorer and selecting Add -> New Project.

Step 5: Add a Service Reference to the WCF service in the Console Application using Add Service Reference dialog box.

Right-click on the Console Application and select Add Service Reference:

wcf2.gif

Step 6: Write following code in the Main function of the Console Application:
static void Main(string[] args)
{
    ServiceReference1.Service1Client MyClient = 
        new ServiceReference1.Service1Client();
    ServiceReference1.Employee emp = 
        new ServiceReference1.Employee();
    emp = MyClient.GetEmployee();
    DataTable dt = new DataTable();
    dt = emp.EmployeeTable;
    Console.WriteLine(" EmpID".PadRight(10) 
        +"FirstName".PadRight(10)
        +"LastName".PadRight(10));
    Console.WriteLine("---------------------------------------");
    for (int i = 1; i < dt.Rows.Count; i++)
    {
        Console.WriteLine(dt.Rows[i][0].ToString().PadRight(10)+ 
            dt.Rows[i][1].ToString().PadRight(10) + 
            dt.Rows[i][2].ToString().PadRight(10));
    } 
    Console.WriteLine(dt.Rows.Count.ToString());
    Console.ReadKey();
}

3) Accessing WCF Service Without Creating Proxy

Normally 
  1. We create a WCF service 
  2. Expose metadata endpoint 
  3. Add service reference at client side to create the proxy.
  4. Using the proxy calls the service operation contracts. 
Normally we call the service as 

1.gif
 
Let us assume we want to call the service using channel without creating proxy or adding the service reference.  We need to follow the below steps 

Create the client.  We are creating a console client to consume the service with channel or without creating proxy.  So follow the below steps 
  1. Do not add the service reference. 
  2. Add the reference of System.ServiceModel.
  3. Add the reference of class library created in step1. 
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ContractDll;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelFactory<MyServiceContract> factory = null;
            try
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress("http://localhost:4684/Service1.svc");
                factory = new ChannelFactory<MyServiceContract>(binding, address);
                MyServiceContract channel = factory.CreateChannel();
                string resturnmessage = channel.GetData(9);
                Console.WriteLine(resturnmessage);
                Console.ReadKey(true);
            }
            catch (CommunicationException)
            {
                if (factory != null)
                    factory.Abort();
            }
            catch (TimeoutException)
            {
                if (factory != null)
                    factory.Abort();
            }
            catch (Exception ex)
            {
                if (factory != null)
                    factory.Abort();
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("Proxy closed");
            Console.ReadKey(true);
        }
    }
}



No comments:

Post a Comment