Friday, 23 December 2016

WCF SOAP AND REST


WCF
1. It is also based on SOAP and return data in XML form.
2. It is the evolution of the web service(ASMX) and support various protocols like 3. TCP, HTTP, HTTPS, Named Pipes, MSMQ.
4. The main issue with WCF is, its tedious and extensive configuration.
5. It is not open source but can be consumed by any client that understands xml.
6. It can be hosted with in the applicaion or on IIS or using window service.

WCF Rest
1. To use WCF as WCF Rest service you have to enable webHttpBindings.
2. It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
3. To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
4. Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified
5. It support XML, JSON and ATOM data format.

Where to use:-

1. In my opinion, SOAP services with SOA architecture are well suited in enterprise applications. SOA architecture includes several key factors like service versioning, scalability, deployment and maintaining of services.
2. REST services are best suited in Internet-facing applications, which can take advantage of HTTP caching and other features.

// SOME OF THE MORE FEATURES ARE GIVEN BELOW



iffernce between normal WCF service and RESTful WCF Service
There is no more difference between WCF service and REST service only main difference is that How Client accesses our Service.
Normal WCF service runs on the SOAP format but when we create REST service then client can access your service in different architecture style like JSON

REST uses4 HTTP methods to insert/delete/update/retrieve information which is below:
GET - Retrive a specific representation of a resource
PUT - Creates or updates a resource with the supplied representation
DELETE - Deletes the specified resource
POST - Submits data to be processed by the identified resource

Where to Use RESTful WCF Service There are some scenario’s where we can use RESTful service like
1) Less Overhead because there is no use of SOAP object
2) Testing is very easy
3) Standardization is very nice

Creation of RESTful WCF Service Step1.
Create a new WCF project in VS 2008/20010

Step 2:
Delete a Service Model node from Web.config

Step 3.
Build the Solution then Right click on the svc file and Click on View Markup button
And add following code to the markup

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

Step 4
Add following references to the WCF Service Application project, if you are using VS2010
Microsoft.Http.dll
Microsoft.Http.Extension.dll
System.ServiceModel.Web.dll


Step 5

  [ServiceContract]
    public interface IService1
    {

        [OperationContract(Name = " AddTwoNumber ")]
        [WebInvoke(UriTemplate = "/", Method = "POST")]
         int AddTwoNumber(int i, int j);

       

        [OperationContract(Name = " SubTwoNumber")]
        [WebGet(UriTemplate = "/")]
        int SubTwoNumber(int i, int j);

      
    }

Add your code in your service like here I have created 2 methods like AddTwonumber which takes two parameter I and j
In the above code we use
WebInvoke :- means invoke web method

Webinvoke takes Two parameters like
1. Method Type there are 4 types of method
a.Post C.Invoke
b.Get d.Delete
2. UriTemplate = "/ADD” tells what would be URI for this particular method


Like this we can create RESTful service.

More differences:
Web Service is an abstract term encompassing a large variety of data providers for distributed systems. Perhaps you are referring to ASMX web services, which can still be found in the wild but aren't really widely used in new development these days.

WCF Service is Microsoft's implementation of SOAP. There are others implementations or you could roll your own (not recommended).

SOAP is a kind of stateful, session-based, message-based web service. It's good if your service is designed as a set of complex actions.

REST is a stateless, sessionless, resource-based web service. It's good if your service is designed to access data and perform simple CRUD operations on it. SOAP and REST are mutually exclusive. A service cannot be both. There are ways to manipulate vanilla WCF to make is RESTful but these techniques are becoming deprecated. If you want to implement a RESTful web service there are two main choices in the Microsoft world: WCF Data Services and ASP.NET Web API.



property

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class property
    {
    }
    class Student
   {
      private string code = "N.A";
      private string name = "not known";
      private int age = 0;
     
      // Declare a Code property of type string:
      public string Code
      {
         get
         {
            return code;
         }
         set
         {
            code = value;
         }
      }
     
      // Declare a Name property of type string:
      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }
     
      // Declare a Age property of type int:
      public int Age
      {
         get
         {
            return age;
         }
         set
         {
            age = value;
         }
      }
      public override string ToString()
      {
         return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
      }
   }
  
   class ExampleDemo
   {
      public static void MainProperty()
      {
     
         // Create a new Student object:
         Student s = new Student();
        
         // Setting code, name and the age of the student
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info: {0}", s);
        
         //let us increase age
         s.Age += 1;
         Console.WriteLine("Student Info: {0}", s);
         Console.ReadKey();
      }
   }
}

private constructor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class privateConstructor
    {
    //{
    //    Private constructor is a special instance constructor used in a class that contains static member only. If a class has one or more private constructor and no public constructor then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static.
        public static string st = string.Empty;
        public  string st1 = string.Empty;
        public string stw()
        {
            return "sttest";
        }
        public privateConstructor(string st1, string st2)
        {
            Console.WriteLine("Hi"+st1+"and hi"+st1);

        }
        private privateConstructor()
        {
            Console.WriteLine("This is the private cinstructor that can not instiated0");

        }

    }
    class program
    {
        public static void Main5(string[] ar)
        {
            privateConstructor objPrivatecontructor = new privateConstructor("hi p1","p2");
            objPrivatecontructor.st1 = "test";
        }
    }

}

Polymorphism

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class Polymorphism
    {

    }
    ////compie time polymorphis start here
    //public class Class1
    //{
    //    public void NumbersAdd(int a, int b)
    //    {
    //        Console.WriteLine(a + b);
    //    }
    //    public void NumbersAdd(int a, int b, int c)
    //    {
    //        Console.WriteLine(a + b + c);
    //    }
    //}
    //public class Compiletimepolymorphis
    //{
    //    public static void main(string[] args)
    //    {
    //        Class1 objClass1 = new Class1();
    //        objClass1.NumbersAdd(12,5);
    //        objClass1.NumbersAdd(12, 12, 13);
    //    }
    //}

    ///Compile time poly morphis end here
    ///
    ///Run time poly morphis star here
    ///
    public class Bclass
    {
        public virtual void Sample1()
        {
            Console.WriteLine("Base Class");
        }
    }
    // Derived Class
    public class DClass : Bclass
    {
        public override void Sample1()
        {
            Console.WriteLine("Derived Class");
        }
    }
    // Using base and derived class
    class ProgramPloy
    {
        static void MainPlohy(string[] args)
        {
            // calling the overriden method
            DClass objDc = new DClass();
            objDc.Sample1();
            // calling the base class method
            Bclass objBc = new DClass();
            objBc.Sample1();
          
            Console.ReadLine();
        }
    }
}

partial class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class Partialclass
    {
        public partial class student
        {
            public void GetStudentDetails()
            {
                //developer is working on student details module
            }
            //some other function for same modules..
        }

        public partial class student
        {
            public void GetStudentResults()
            {
                //developer is working on student results module
            }

            //some other function for same modules..
        }
    }
}

InterfaceTest

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class InterfaceTest
    {
        interface myclass
        {
            string Animal();
            string Animal1();
  
            // {
            //     string stranimaltext ="Dog has four leg";
            //     return stranimaltext;

            // }
            //public static void Refrencefunction()
            //{
            //}

        }
        class Aceeessabstract : myclass
        {
            public string Animal()
            {
                string stranimaltext = "Cow has four leg";
                return stranimaltext;

            }
            public string Animal1()
            {
                string stranimaltext = "Buffalow1 has four leg";
                return stranimaltext;

            }

        }


        static void Main2(string[] args)
        {
            Aceeessabstract objAb = new Aceeessabstract();
            Console.WriteLine(objAb.Animal());
            Console.WriteLine(objAb.Animal1());
            Console.Read();
        }
    }
}

Delegate

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class Delegate
    {
    }
    public delegate int NumberChanger(int w);
    namespace DelegateAppl
    {
        class TestDelegate
        {
            static int num = 10;
            public static int AddNum(int p)
            {
                num += p;
                return num;
            }

            public static int MultNum(int q)
            {
                num *= q;
                return num;
            }
            public static int getNum()
            {
                return num;
            }

            static void MainDelegate(string[] args)
            {
                //create delegate instances
                NumberChanger nc1 = new NumberChanger(AddNum);
                NumberChanger nc2 = new NumberChanger(MultNum);

                nc1(25);
                nc2(35);
                Console.WriteLine("Addition of two number is {0}",getNum());
                Console.ReadLine();
            }
        }
    }
}

Constructor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class Constructor1
    {
        public Constructor1()
        {
            Console.WriteLine("constructr that have the no parameter");

        }
        //public Constructor1(string st)
        //{
        //    Console.WriteLine("constructr that have the  parameter {0}",st);

        //}
        public Constructor1(string st1, string st2)
        {
            Console.WriteLine("constructr that have the  parameter {0}&{1}", st1,st2);

        }
    
    }
     class ChildClass : Constructor1
    {
        public ChildClass()
            : base()
        {
            Console.WriteLine("Chuild  Class constructor with n parameter ");
        }
        //public ChildClass(string stchild1)
        //    : base(stchild1)
        //{
        //    Console.WriteLine("Child class cnstructr that have one string parameter{0}", stchild1);

        //}
        public ChildClass(string stchild1,string stChild2)
            : base(stchild1,stChild2)
        {
            Console.WriteLine("Child class cnstructr that have one string parameter{0} & {1}", stchild1, stChild2);

        }
    }
    public class Program1
    {
        public static void Maincons(string[] args)
        {
            Console.WriteLine("--------------------------Cnstructor Chaining strted here---------------------------------------");
            ChildClass objConstructor = new ChildClass ();
            ChildClass objCh2 = new ChildClass("Parameter1", "Parameter2");
            Console.ReadLine();
        }
    }
}

ABSTTRACTION

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    public class Bike
    {
        public int mileage = 65;
        public string color = "Black";
        private string formula = "a*b";
        //Its public – so accessible outside class
        public int GetMileage()
        {
            return mileage;
        }

        //Its public – so accessible outside class
        public string GetColor()
        {
            return color;
        }

        //Its private – so not accessible outside class
        private string GetEngineMakeFormula()
        {
            return formula;
        }
    }
    public class hondaBike
    {
        public int mileage = 65;
        public string color = "Black";
        private string formula = "a*b";
        //Its public – so accessible outside class
        public int GetMileage()
        {
            return mileage;
        }

        //Its public – so accessible outside class
        public string GetColor()
        {
            return color;
        }

        //Its private – so not accessible outside class
        private string GetEngineMakeFormula()
        {
            return formula;
        }

        //Its public – so accessible outside class
        public string DisplayMakeFormula()
        {
            //"GetEngineMakeFormula()" is private but accessible and limited to this class only
            return GetEngineMakeFormula();
        }
    }

    public class Programabst
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("------------------------------------Encapsulation started here------------------------------------");
            Bike objBike = new Bike();
            Console.WriteLine("Bike mileage is : " + objBike.GetMileage()); //accessible outside "Bike"
            Console.WriteLine("Bike color is : " + objBike.GetColor()); //accessible outside "Bike"
            //we can't call this method as it is inaccessible outside "Bike"
            //objBike.GetEngineMakeFormula(); //commented because we can't access it


            Console.WriteLine("------------------------------------Encapsulation End here------------------------------------");



            Console.WriteLine("------------------------------------Abstraction Started here------------------------------------");
            hondaBike objhondaBike = new hondaBike();
            Console.WriteLine("Bike mileage is : " + objhondaBike.GetMileage()); //accessible outside "Bike"
            Console.WriteLine("Bike color is : " + objhondaBike.GetColor()); //accessible outside "Bike"
               //we can't call this method as it is inaccessible outside "Bike"
               //objBike.GetEngineMakeFormula(); //commented because we can't access it
            Console.WriteLine("Bike making frmula  is : " + objhondaBike.DisplayMakeFormula()); //accessible outside



            Console.WriteLine("------------------------------------Abstraction End here------------------------------------");
            Console.Read();
        }
    }
}

AbstractInterfaceBoth

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    /// <summary>
    /// An Abstract class doesn't provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
//Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
//We can not declare a member field in an Interface.
//We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.
//An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.
    /*
     * Abstraction:
Abstraction is "To represent the essential feature without representing the back ground details."

Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or object by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in understandable manner.

Real world Example of Abstraction: -
Suppose you have an object Mobile Phone.

Suppose you have 3 mobile phones as following:-

Nokia 1400 (Features:- Calling, SMS)
Nokia 2700 (Features:- Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:-Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (Necessary and Common Information) for the object "Mobile Phone" is make a call to any number and can send SMS."

so that, for mobile phone object you will have abstract class like following:-

     *
     ///  ENCAPSULATION START HERE
     *
     *
     *
     * Encapsulation:
Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.

    class Bag
    {
        book;
        pen;
        ReadBook();
    }


Encapsulation means hiding the internal details of an object, i.e. how an object does something.

Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.

Encapsulation is a technique used to protect the information in an object from the other object.

Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.
So, when you access the property you can validate the data and set it.
S
    
    
    
     */
    /// </summary>
abstract class M1
   {
        public int add(int a, int b)
        {
            return (a + b);
        }
    }
    class M2 :M1
    {
        //public int mul(int a, int b)
        //{
        //    return a * b;
        //}
    }
    class test
    {
        static void Main56(string[] args)
        {
            M2 ob = new M2();
            int result = ob.add(10, 20);
            Console.WriteLine("the result is {0}", result);
            Console.ReadLine();
        }
    }
}