Sunday, November 3, 2013

REST BASED SERVICES USING WCF

REST stands for Representational state transfer
Very popular for services
Principle: is if there is any resource then everyone has to access that resource. Rest also defines that URL should be friendly.



REST URL should be as must as plane URL

REST is the term coined by Ray Fielding in PH.D to describe an architecture style of networking systems.

There are Rest services & Rest –Like services which uses XML and HTTP.

Rest outlines the philosophy of mapping.
HTTP Verbs to Create, retrieval, update, delete

Rest is not only for services, it is the for Html 5.
Rest architecture says everything should be a resources.

Rest say we will develop a program and give the URL to everyone.
REST is not approved by W3 till now.

Note:

When we talk about the Database as a resource we usually talk in terms of CRUD operations. I.e. Create, Retrieve, Update and Delete. Now, What REST says is that for a remote resource all these operations should be possible and they should be possible using simple HTTP protocols

Actually only the difference is how clients access our SERVICE .Normally, a WCF service will use SOAP, but if you build a REST service, clients will be accessing your service with a different architectural style (calls, serialization like JSON, etc.)

We have to prepare help File for Rest for the client, saying that what are the methods somewhat we have to give the reply to client.

Now the basic CRUD operations are mapped to the HTTP protocols in the following manner:

·         GET: This maps to the R (Retrieve) part of the CRUD operation. This will be used to retrieve the required data (representation of data) from the remote resource.

·         PUT: This maps to the U (Update) part of the CRUD operation. This protocol will update the current representation of the data on the remote server.

·         POST: This maps to the C (Create) part of the CRUD operation. This will create a new entry for the current data that is being sent to the server.

·         DELETE: This maps to the D (Delete) part of the CRUD operation. This will delete the specified data from the remote server.

Now, the list of Employee Details can be retrieved as

To retrieve specific employee details from the Employee Table we have an Id Column in the database. To access the details of particular employee


Since these are GET requests, data can only be retrieved from the server. To perform other operations, if we use the similar URI structure with PUT, POST or DELETE operation, we should be able to create, update and delete the resource from the server. 

WCF Rest supports for REST

        Programming Model
        WebGetAttribute
WebInvokeAttribute
WebServiceHost

Channels & Dispatcher Infrastructure.
WebHttpBinding
WebHttpBehaviour

Utility Classes & Extensibility Points

        Uri Template
       Uri Template Table
        Query String Converter
        WebHttpDispatchOperationSelector

WebGet:
Is the attribute which make our [Operation Contract] as web method enabled to consume as Rest Style & Binding is WebHttpBinding

[WebGet]
[Operation Contract]
Void Add ();

Web InvokeAttribute:àWeb Get +All Others

Now, How to Call this Method


Here Add is the method name

If it returns string, we will get result also as string.

WebServiceHostFactory

To make Rest Enabled all the following things are required

System.ServiceModel.dll
System.ServiceModel.Web.dll

To make URL as Friendly we use uri Template

Example

It is not friendly URL

So friendly URL is


To make URL as friendly:

[WebGet(UriTemplate=”Add/{a}/{b}”)]
[operationContract]
Void Add(int a, int b)

Note: Webhttpbinding does not support SOAP format. what format is Json,pox


In visual studio 2010 we have two ways to create Restful Services:

Start a WCF Service as usual and add Operation Contract as required

Additionally add Rest attribute that is web get /web invoke

System. Service model namespace & system. Service model. Web namespaces to be added.

Go to web.config and add End Points with WebHttpBinding only. 

This binding is enough for consuming and also hosting.


Go to service directive .svc file and add factory=”webservicehostfactory” attribute to this directive.



Difference between Web Services, WCF, WCF Rest

Web Service:
 It is based on SOAP and return data in XML form.
    It support only HTTP protocol.
   It is not open source but can be consumed by any client that understands XML.
It can be hosted only on IIS

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

WCF REST

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


Thursday, October 31, 2013

Alphanumeric auto code generator using web


Default.aspx
 



Default.aspx.cs
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Next_Click(object sender, EventArgs e)
    {
        lblvalue1.Text = NxtKeyCode(txtValue.Text);
    }
    public string NxtKeyCode(string KeyCode)
    {
        byte[] ASCIIValues = ASCIIEncoding.ASCII.GetBytes(KeyCode);
        int StringLength = ASCIIValues.Length;
        bool isAllZed = true;
        bool isAllNine = true;
        //Check if all has ZZZ.... then do nothing just return empty string.
        for (int i = 0; i < StringLength - 1; i++)
        {
            if (ASCIIValues[i] != 90)
            {
                isAllZed = false;
                break;
            }
        }
        if (isAllZed && ASCIIValues[StringLength - 1] == 57)
        {
            ASCIIValues[StringLength - 1] = 64;
        }
        // Check if all has 999... then make it A0
        for (int i = 0; i < StringLength; i++)
        {
            if (ASCIIValues[i] != 57)
            {
                isAllNine = false;
                break;
            }
        }
        if (isAllNine)
        {
            ASCIIValues[StringLength - 1] = 47;
            ASCIIValues[0] = 65;
            for (int i = 1; i < StringLength - 1; i++)
            {
                ASCIIValues[i] = 48;
            }
        }

        for (int i = StringLength; i > 0; i--)
        {
            if (i - StringLength == 0)
            {
                ASCIIValues[i - 1] += 1;
            }
            if (ASCIIValues[i - 1] == 58)
            {
                ASCIIValues[i - 1] = 48;
                if (i - 2 == -1)
                {
                    break;
                }
                ASCIIValues[i - 2] += 1;
            }
            else if (ASCIIValues[i - 1] == 91)
            {
                ASCIIValues[i - 1] = 65;
                if (i - 2 == -1)
                {
                    break;
                }
                ASCIIValues[i - 2] += 1;

            }
            else
            {
                break;
            }
        }
        KeyCode = ASCIIEncoding.ASCII.GetString(ASCIIValues);
        return KeyCode;
    }
}


 
Output:
 
 
 
 
 
 
 
 

Saturday, October 26, 2013

Special Functions in SQL SERVER

Special functions:
db_name():
Returns name of the current database.
select db_name()

user_name()
Returns name of the user
select user_name()

host_name()
Returns name of the server
select host_name()

IDENT_CURRENT(‘tablename’)
This function returns current value of the identity.
select IDENT_CURRENT('login')

IDENT_SEED(‘tablename’)
This function returns the starting value of the identity.
select IDENT_SEED('login')

IDENT_INCR(‘tablename’)
This function returns increment value.

select IDENT_INCR('login')

isnumeric()
isnumeric,isexpression,if expression,isnumber
This function returns 1 otherwise ‘0’
select isnumeric(10)

isdate()
If expression is date, returns 1 otherwise ‘0’
select ISDATE(getdate())

isnull()
this function is used to convert null values.
Syntax: isnull(expr1,expr2)

if expr1 is null, it returns exp2
if expr is not null, it returns exp1 only.

select isnull(200,100)

select isnull(null,100)



SUPER AGGREGATES IN SQL SERVER




Super aggregates:
Are used to calculate aggregates of aggregates
There are two super aggregates functions

Roll Up
Cube

When there is a single col in the group by than there is no difference between roll up and cube
But when there are multiple columns in group by than roll up will calculate aggregate of aggregate only at the end of the group created based on first column in the group by.


Whereas cube can calculate aggregate at the end of every group create based on every column in the group by.

Syntax:
Select */<collist> from <tablename>
[where condition]
Group by <collist> with roll up /cube [having condition] [order by]


Find the total salary paid to employee dept-wise and also find the total salary paid to employees of all department
select deptno, sum(Sal) from emp group by deptno  with rollup
or with cube

Find total salary paid to employees job wise in each department and also find the total salary paid to employee dept-wise

select deptno,job,sum(sal)as totalsalary from emp group by deptno,job with rollup


Find the total salary paid to employees job-wise in each department and also find the total salary paid to employee deptwise  and job wise
select deptno,job,sum(sal)as totalsalaryfordeptfrom emp group by deptno,job with cube

deptno-1 column
job-2 column

Cube example
The difference between cube and roll up
Roll up option calculates sub totals, based on first dimension.

Cube option calculates subtotals, based on both dimensions
In sql server 2005, both are Non-ANSI standard
In sql server 2008 both are ANSI standard

100 th Post TCL

 





Employee Table
 

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...