Sunday, May 3, 2015

Different types of WCF bindings



WCF has a couple of built in bindings which are designed to fulfill some specific need. You can also define your own custom binding in WCF to fulfill your need. All built in bindings are defined in the System.ServiceModel Namespace. Here is the list of 10 built in bindings in WCF which we commonly used:
1.   Basic binding
This binding is provided by the BasicHttpBinding class. It is designed to expose a WCF service as an ASMX web service, so that old clients (which are still using ASMX web service) can consume new service. By default, it uses Http protocol for transport and encodes the message in UTF - 8 text for-mat. You can also use Https with this binding.
2.   Web binding
This binding is provided by the WebHttpBinding class. It is designed to expose WCF services as Http requests by using HTTP-GET, HTTP-POST. It is used with REST based services which may give output as an XML or JSON format. This is very much used with social networks for implementing a syndication feed.
3.   Web Service (WS) binding
This binding is provided by the WSHttpBinding class. It is like as Basic binding and uses Http or Https protocols for transport. But this is designed to offer various WS - * specifications such as WS – Reliable Messaging, WS - Transactions, WS - Security and so on which are not supported by Basic binding.
   wsHttpBinding= basicHttpBinding + WS-* specification
4.   WS Dual binding
This binding is provided by the WsDualHttpBinding class. It is like as wsHttpBinding except it sup-ports bi-directional communication means both clients and services can send and receive messages.
5.   TCP binding
This binding is provided by the NetTcpBinding class. It uses TCP protocol for communication be-tween two machines with in intranet (means same network). It encodes the message in binary format. This is faster and more reliable binding as compared to the Http protocol bindings. It is only used when communication is WCF - to – WCF means both client and service should have WCF.
6.   IPC binding
This binding is provided by the NetNamedPipeBinding class. It uses named pipe for Communication between two services on the same machine. This is the most secure and fastest binding among all the bindings.
7.   MSMQ binding
This binding is provided by the NetMsmqBinding class. It uses MSMQ for transport and offers sup-port to disconnected message queued. It provides solutions for disconnected scenarios in which service processes the message at a different time than the client send the messages.
8.   Federated WS binding
This binding is provided by the WSFederationHttpBinding class. It is a specialized form of WS binding and provides support to federated security.
9.   Peer Network binding
This binding is provided by the NetPeerTcpBinding class. It uses TCP protocol but uses peer net-working as transport. In this networking each machine (node) acts as a client and a server to the other nodes. This is used in the file sharing systems like torrent.
10.                     MSMQ Integration binding
This binding is provided by the MsmqIntegrationBinding class. This binding offers support to communicate with existing systems that communicate via MSMQ.
Choosing an Appropriate WCF binding
. Depending upon your requirements, you can choose a binding for your service as shown below in the diagram:


Friday, December 26, 2014

Abstract-Classes

using System;

public abstract class Animal
{
    public abstract string GetName();
    abstract public int Speed { get; }
}



using System;

public class Cheetah : Animal
{
    public override int Speed
    {
        get
        {
            return 100;
        }
    }

    public override string GetName()
    {
        return "cheetah";
    }
}


using System;

public class Turtle : Animal
{
    public override int Speed
    {
        get
        {
            return 1;
        }
    }

    public override string GetName()
    {
        return "turtle";
    }
}


using System;

class AbstractClasses
{
    static void Main()
    {
        Turtle turtle = new Turtle();
        Console.WriteLine("The {0} can go {1} km/h ",
            turtle.GetName(), turtle.Speed);
        Cheetah cheetah = new Cheetah();
        Console.WriteLine("The {0} can go {1} km/h ",
            cheetah.GetName(), cheetah.Speed);

        System.Collections.Generic.List<int> l;
        System.DateTime dt;
    }
}


OUTPUT

The turtle can go 1 km/h

The cheetah can go 100 km/h

Polymorphism

using System;

abstract class Figure
{
    public abstract double CalcSurface();
}


using System;

class Circle : Figure
{
    public double Radius { get; set; }
   
    public override double CalcSurface()
    {
        return Math.PI * this.Radius * this.Radius;
    }
}

using System;

class Rectangle : Figure
{
    public double Width { get; set; }
    public double Height { get; set; }
   
    public override double CalcSurface()
    {
        return this.Width * this.Height;
    }
}

using System;

class Square : Figure
{
    public double Size { get; set; }
   
    public override double CalcSurface()
    {
        return this.Size * this.Size;
    }
}


using System;

class Polymorphism
{
    static void Main()
    {
        Figure[] figures = new Figure[] {
            new Square() { Size = 3 },
            new Circle() { Radius = 2 },
            new Rectangle() { Width = 2, Height = 3 },
            new Circle() { Radius = 3.5 },
            new Square() { Size = 2.5 },
            new Square() { Size = 4 },
        };

        foreach (Figure figure in figures)
        {
            Console.WriteLine("Figure = {0} surface = {1:F2}",
                figure.GetType().Name.PadRight(9,' '),
                figure.CalcSurface());
            Console.ReadLine();
        }
    }
}

OUTPUT

Figure = Square    surface = 9.00

Figure = Circle    surface = 12.57

Figure = Rectangle surface = 6.00

Figure = Circle    surface = 38.48

Class-Diagrams

using System;

struct Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y) : this()
    {
        this.X = x;
        this.Y = y;
    }
}

using System;

abstract class Shape
{
    protected Point Position { get; set; }

    public Shape(Point position)
    {
        this.Position = position;
    }
}


using System;

struct Color
{
    public byte RedValue { get; set; }
    public byte GreenValue { get; set; }
    public byte BlueValue { get; set; }

    public Color(byte redValue, byte greenValue, byte blueValue)
        : this()
    {
        this.RedValue = redValue;
        this.GreenValue = greenValue;
        this.BlueValue = blueValue;
    }
}




using System;

interface ISufraceCalculatable
{
    float CalculateSurface();
}




using System;

class Square : Shape, ISufraceCalculatable
{
    private float Size { get; set; }

    public Square(float size, int x, int y)
        : base (new Point(x,y))
    {
        this.Size = size;
    }

    public float CalculateSurface()
    {
        return this.Size * this.Size;
    }
}



using System;

class Rectangle : Shape, ISufraceCalculatable
{
    private float Width { get; set; }
    private float Height { get; set; }

    public Rectangle(float width, float height, int x, int y)
        : base (new Point(x,y))
    {
        this.Width = width;
        this.Height = height;
    }

    public float CalculateSurface()
    {
        return this.Width * this.Height;
    }
}


using System;

class FilledSquare : Square
{
    private Color Color { get; set; }

    public FilledSquare(float size, int x, int y, Color color)
        : base(size, x, y)
    {
        this.Color = color;
    }
}





Inheritance And Accessibility

using System;

public class Creature
{
    protected string Name { get; private set; }

    public Creature(string name)
    {
        this.Name = name;
    }

    private void Talk()
    {
        Console.WriteLine("I am creature ...");
    }

    protected void Walk()
    {
        Console.WriteLine("Walking, walking, walking ....");
    }
}


using System;

public class Mammal : Creature
{
    public int Age { get; set; }

    public Mammal(string name, int age) : base(name)
    {
        this.Age = age;
    }

    public void Sleep()
    {
        Console.WriteLine("Shhh! {0} is sleeping!", this.Name);
    }
}



using System;

public class Dog : Mammal
{
    public string Breed { get; private set; }

    public Dog(string name, int age, string breed) : base(name, age)
    {
        this.Breed = breed;
    }

    public void WagTail()
    {
        Console.WriteLine("{0} is {1} wagging his {2}-aged tail ...",
            this.Name, this.Breed, this.Age);
        //this.Walk(); // This will successfully compile - Walk() is protected
        //this.Talk(); // This will not compile - Talk() is private
        //this.Name = "Doggy"; // This will not compile - Name.set is private
    }
}


using System;

class InheritanceAndAccessibility
{
    static void Main()
    {
        Dog joe = new Dog("Sharo", 6, "labrador");
        joe.Sleep();
        Console.WriteLine("Breed: " + joe.Breed);
        Console.ReadLine();
        joe.WagTail();

     }
}

OUTPUT

Shhh! Sharo is sleeping!
Breed: labrador

Simple-Inheritance

using System;

public class Mammal
{
    public int Age { get; set; }

    public Mammal(int age)
    {
        this.Age = age;
    }
   
    public void Sleep()
    {
        Console.WriteLine("Shhh! I'm sleeping!");
    }
}


using System;

public class Dog : Mammal
{
    public string Breed { get; set; }

    public Dog(int age, string breed) : base(age)
    {
        this.Breed = breed;
    }

    public void WagTail()
    {
        Console.WriteLine("Tail wagging...");
    }
}

using System;

class SimpleInheritance
{
    static void Main()
    {
        Dog joe = new Dog(8, "Labrador");
        joe.Sleep();
        joe.WagTail();
        Console.WriteLine("Joe is {0} years old dog of breed {1}.",
            joe.Age, joe.Breed);
        Console.ReadLine();
    }
}



OUTPUT

Shhh! I'm sleeping!
Tail wagging...
Joe is 8 years old dog of breed Labrador.


Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...