Tuesday, July 15, 2014

Azure Mobile Services ( Start to build)

Windows Azure cloud application provide quite amazing solutions for Mobile backed services. With Azure cloud applications allows you to store structured data (files and SQL Azure) , Authentication interrogation to Mobile applications and user updates via push notifications. Specially Azure is not only for Windows (Microsoft ) Products. It supports Window 8, Windows Phone , Android and iOS with different development platforms such as native platforms and Xamarin.

Lets build first Cloud app

1. First Log in to your Azure Portal and click on New button in left corner













2. Then from prompt Select Compute -> Mobile Service-> Create

3. With the next prompt add URL for your Mobile service in URL field. With Database field if you have already created database for mobile application back end select it if it is not select option Create new SQL Database instance  or Create Free 20MB SQL Database Option

Select your region and what kind of backed you need to have. it has .NET and Javascript backed. Choose your choise

If you plan to include Push notification with your app tick on  Advanced Push Notification Check box













4. If you checked Advanced Push Notification Check box this step you find the Push Notification Settings prompt. Azure notification hub is cross platform push notification service that can simultaneously broadcast notifications across the devices.

Create namespace to notification service and name your push notification Hub























5. Then you can define the Database Settings. You can define new database name , If you already have database server select it or else create new database server and give the credentials for it as you want.
If you need to make advanced changes to the database tick on Configure Advanced Database Settings  Check box.

With Advanced database settings you can limit the database capacity and define the character encoding for the data














Then our web service is up and it take few seconds to configure and make it online













Once our Service is created the screen is like this












Select the platform that you need to create app that using this service and scroll down to to GET STARTED area.

Expand the Create new App. (this depend on what you select in Choose Platform Area Select the language C# or JavaScript and download the applicattion.

Run it and enjoy your first cloud app



Sunday, July 13, 2014

Stored Procedures Make things Better

Stored Procedures are a set of SQL(T-SQL) statements that compiled in to single execution plan. Think it like Method in Class the method is stored procedure and Class is Database

Benefits of Stored Procedures

 1. Increase Program performance
 Stored procedures are compiled only once and stored it as executable form and that executable cached and shared among all users.Stored procedures execute within a single call even though it has many SQL statements in the body.

2. Interoperability
If you work with stored procedure you don't need to change lot of code lines to make it functional.Stored procedure call is just enough. Each of modern languages have specific libraries for calling stored procedures

3. Security
You can restrict the data access in database with stored procedures. Ex: Just only for read.

4. Scalability
Stored procedures increase scalability by isolating application processing on the server. In addition, automatic dependency tracking for stored procedures aids the development of scalable applications.

5.  Maintainability
Once it is validated, a stored procedure can be used with confidence in any number of applications. If its definition changes, only the procedure is affected, not the applications that call it. This simplifies maintenance and enhancement. Also, maintaining a procedure on the server is easier than maintaining copies on various client machines.  


Stored Procedure Outputs

Stored procedures can return data or cursor values . Return codes in stored procedure always returns integer values and cursor can reference to out side . And there is return data sets for select statement results.

Lets Work on it

I'm working on local database file that attached with my project in Visual studio. Bit there is no difference between SQL Server procedure creation and Procedure creation in Visual studio.

You can add your own SQL Server database to the project.
CTRL+SHIFT+A -> Data->SQL Server Database

Here is tables that I'm going to working on

CREATE TABLE [dbo].[PurchaseHistory] (
    [Id]        INT              IDENTITY (1, 1) NOT NULL,
    [UserId]    UNIQUEIDENTIFIER NOT NULL,
    [AlbumId]   INT              NOT NULL,
    [Units]     INT              DEFAULT ((0)) NOT NULL,
    [Buydate]   DATETIME         DEFAULT (getdate()) NOT NULL,
    [Payed]     BIGINT           DEFAULT ((0)) NOT NULL,
    [PayedDate] DATETIME         NULL,
    [Total]     FLOAT (53)       DEFAULT ((0.00)) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Users] (
    [ApplicationId]    UNIQUEIDENTIFIER NOT NULL,
    [UserId]           UNIQUEIDENTIFIER NOT NULL,
    [UserName]         NVARCHAR (50)    NOT NULL,
    [IsAnonymous]      BIT              NOT NULL,
    [LastActivityDate] DATETIME         NOT NULL,
    PRIMARY KEY CLUSTERED ([UserId] ASC),
    CONSTRAINT [UserApplication] FOREIGN KEY ([ApplicationId]) REFERENCES [dbo].[Applications] ([ApplicationId])
);
CREATE TABLE [dbo].[Albums] (
    [AlbumId]        INT           IDENTITY (1, 1) NOT NULL,
    [AlbumName]      VARCHAR (255) NOT NULL,
    [ImageSource]    VARCHAR (50)  NOT NULL,
    [Artist]         VARCHAR (255) NOT NULL,
    [Price]          MONEY         NOT NULL,
    [Description]    VARCHAR (MAX) NOT NULL,
    [Tracks]         INT           NULL,
    [Label]          INT           NOT NULL,
    [OrigYear]       INT           NULL,
    [AdditionalInfo] VARCHAR (550) NULL,
    [Rating]         INT           NULL,
    PRIMARY KEY CLUSTERED ([AlbumId] ASC)
);

And make sure your tables containing data to test our procedures

Expand your database objects from Server explorer and R-Click on the stored procedures and select Add New Stored procedure
























If you working on SQL Server Add New Stored Procedure is on Programability->Stored Procedures 





















Here is my requirement is create procedure that returns all unpayed purchases from PurchaseHistory by selected user with Album details. This selected user will indicate by user input from outside. It will take userName as parameter

If we took it as function its like this
List<Object> Procedure(string UserName)

Here is Procedure for it. My procedure name is GetAllCartProcedure

CREATE PROCEDURE [dbo].[GetAllCartProcedure]
    @UserName varchar(255)
AS
    BEGIN
        DECLARE @UserId uniqueidentifier
        DECLARE @Total money
        --select userId from the user table to make connection between Purchase history
        SELECT @UserId=(UserId) FROM Users WHERE UserName=@UserName
        --Select all purchases that not payed by selected User
        SELECT
            Albums.AlbumName,'Images/AlbumImages/'+Albums.ImageSource,Albums.Price,
            PurchaseHistory.Buydate,PurchaseHistory.Units,PurchaseHistory.Total
        FROM Albums,PurchaseHistory
        WHERE
            Albums.AlbumId=PurchaseHistory.AlbumId
            AND
            PurchaseHistory.UserId=@UserId
            AND
            PurchaseHistory.Payed=0         
    END
RETURN

After procedure created you ca test the procedure on R-Click on the created procedure











It will prompt this dialogbox and give the relevent parameters to the prompt and check














Or Use T-SQL
USE [aspnet-E commerce-20140712191038]
GO
DECLARE @return_value Int
EXEC    @return_value = [dbo].[GetAllCartProcedure]
        @UserName = 'UseName'
SELECT  @return_value as 'Return Value'
GO

Here is result













You can access those stored procedures easily with entity framework and ADO.NET libraries. Use those with those and make better code..




Friday, July 11, 2014

REST in peace With Web services( from scratch )

REST  is stands for REpresentational State Transfer. REST is the underlying architecture of web.

It is basically the HTTP protocol, which was originally created to allow representational state transfer, not just transfer of web pages. HTTP is most commonly used to GET content and POST data. But it can also be used to PUT and DELETE data, as well as get Header information, etc

With this post I am going to create new web service that interact with data(xml) and going to make that web service into RESTFul web service

Create Web Service.  
 1. Open Visual Studio and goto -> New -> WCF -> WCF Service Application














2. Go to Solution explorer  and select two files IService1.cs and Service1.svc and delete them.(If you need you can keep them)















3. Then Press CTRL+SHIFT+A and Select Visual C# and WCF Service. Then give name for the service. Lets use it as demo_REST and Add














3. Lets open up the two files demo_REST.svc and demo_REST.cs and change its only funtion DoWork() to return string as output.

To do that first change the demo_REST.cs file's doWork as this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace REST_Demo
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "Idemo_REST" in both code and config file together.
    [ServiceContract]
    public interface Idemo_REST
    {
        [OperationContract]
        string DoWork();
    }
}


Then go to the demo_REST.svc and edit it as follow. Just need to  change return type and add code to middle

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace REST_Demo
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "demo_REST" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select demo_REST.svc or demo_REST.svc.cs at the Solution Explorer and start debugging.
    public class demo_REST : Idemo_REST
    {
        //change public void DoWork() to public string DoWork()
        public string DoWork()
        {
            //add return text
            return "Hello WCF";
        }
    }
}



now we are done with our simple WCF

run and test it ..

its open in WCF test Client from WCF test Client select the function that we create DoWork() amd click on it then click on invoke button to test it



 In Value column it will display the results .. its "Hello WCF" and our function is working..


Implementing data bound Web service 
I am not going details about databounded web services. My previous article Web service with DB interaction provided good demonstration about how to work with DB data.

Here I am going to bind the XML file with it. I got the xml data from W3Schools (http://www.w3schools.com/xml/cd_catalog.xml)


In Visual Studio CTRL+SHIFT+A  then select Data and select XML File  and name it as cd_data.xml then copy paste the XML data from W3Schools to the XML body.

Now cd_data.xml is look likes this
<?xml version="1.0" encoding="utf-8" ?>
!-- Edited by XMLSpy --><CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
  <CD>
    <TITLE>Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
  </CD>
  <CD>
    <TITLE>Still got the blues</TITLE>
    <ARTIST>Gary Moore</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Virgin records</COMPANY>
    <PRICE>10.20</PRICE>
    <YEAR>1990</YEAR>
  </CD>
  <CD>
    <TITLE>Eros</TITLE>
    <ARTIST>Eros Ramazzotti</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>BMG</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1997</YEAR>
  </CD>
  <CD>
    <TITLE>One night only</TITLE>
    <ARTIST>Bee Gees</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1998</YEAR>
  </CD>
  <CD>
    <TITLE>Sylvias Mother</TITLE>
    <ARTIST>Dr.Hook</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS</COMPANY>
    <PRICE>8.10</PRICE>
    <YEAR>1973</YEAR>
  </CD>
  <CD>
    <TITLE>Maggie May</TITLE>
    <ARTIST>Rod Stewart</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Pickwick</COMPANY>
    <PRICE>8.50</PRICE>
    <YEAR>1990</YEAR>
  </CD>
  <CD>
    <TITLE>Romanza</TITLE>
    <ARTIST>Andrea Bocelli</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>10.80</PRICE>
    <YEAR>1996</YEAR>
  </CD>
  <CD>
    <TITLE>When a man loves a woman</TITLE>
    <ARTIST>Percy Sledge</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Atlantic</COMPANY>
    <PRICE>8.70</PRICE>
    <YEAR>1987</YEAR>
  </CD>
  <CD>
    <TITLE>Black angel</TITLE>
    <ARTIST>Savage Rose</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Mega</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1995</YEAR>
  </CD>
  <CD>
    <TITLE>1999 Grammy Nominees</TITLE>
    <ARTIST>Many</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Grammy</COMPANY>
    <PRICE>10.20</PRICE>
    <YEAR>1999</YEAR>
  </CD>
  <CD>
    <TITLE>For the good times</TITLE>
    <ARTIST>Kenny Rogers</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Mucik Master</COMPANY>
    <PRICE>8.70</PRICE>
    <YEAR>1995</YEAR>
  </CD>
  <CD>
    <TITLE>Big Willie style</TITLE>
    <ARTIST>Will Smith</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1997</YEAR>
  </CD>
  <CD>
    <TITLE>Tupelo Honey</TITLE>
    <ARTIST>Van Morrison</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>8.20</PRICE>
    <YEAR>1971</YEAR>
  </CD>
  <CD>
    <TITLE>Soulsville</TITLE>
    <ARTIST>Jorn Hoel</ARTIST>
    <COUNTRY>Norway</COUNTRY>
    <COMPANY>WEA</COMPANY>
    <PRICE>7.90</PRICE>
    <YEAR>1996</YEAR>
  </CD>
  <CD>
    <TITLE>The very best of</TITLE>
    <ARTIST>Cat Stevens</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Island</COMPANY>
    <PRICE>8.90</PRICE>
    <YEAR>1990</YEAR>
  </CD>
  <CD>
    <TITLE>Stop</TITLE>
    <ARTIST>Sam Brown</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>A and M</COMPANY>
    <PRICE>8.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
  <CD>
    <TITLE>Bridge of Spies</TITLE>
    <ARTIST>T'Pau</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Siren</COMPANY>
    <PRICE>7.90</PRICE>
    <YEAR>1987</YEAR>
  </CD>
  <CD>
    <TITLE>Private Dancer</TITLE>
    <ARTIST>Tina Turner</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Capitol</COMPANY>
    <PRICE>8.90</PRICE>
    <YEAR>1983</YEAR>
  </CD>
  <CD>
    <TITLE>Midt om natten</TITLE>
    <ARTIST>Kim Larsen</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Medley</COMPANY>
    <PRICE>7.80</PRICE>
    <YEAR>1983</YEAR>
  </CD>
  <CD>
    <TITLE>Pavarotti Gala Concert</TITLE>
    <ARTIST>Luciano Pavarotti</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>DECCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1991</YEAR>
  </CD>
  <CD>
    <TITLE>The dock of the bay</TITLE>
    <ARTIST>Otis Redding</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Atlantic</COMPANY>
    <PRICE>7.90</PRICE>
    <YEAR>1987</YEAR>
  </CD>
  <CD>
    <TITLE>Picture book</TITLE>
    <ARTIST>Simply Red</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Elektra</COMPANY>
    <PRICE>7.20</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Red</TITLE>
    <ARTIST>The Communards</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>London</COMPANY>
    <PRICE>7.80</PRICE>
    <YEAR>1987</YEAR>
  </CD>
  <CD>
    <TITLE>Unchain my heart</TITLE>
    <ARTIST>Joe Cocker</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>EMI</COMPANY>
    <PRICE>8.20</PRICE>
    <YEAR>1987</YEAR>
  </CD>
</CATALOG>


Now we need to have some method to access those data n service. Open demo_REST.cs and inside the interface Idemo_REST add operation contract we named it as GetCatalog(). and we are going to return catalog data as JSON string then our return type for the GetCatalog() should be string.

namespace REST_Demo
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "Idemo_REST" in both code and config file together.
    [ServiceContract]
    public interface Idemo_REST
    {
        [OperationContract]
        string DoWork();
        //our newly added operation contract.
        [OperationContract]
        string GetCatelog();
    }
}

Lets implement the logic behind that interface function in Open demo_REST.svc 
 Now its look like this


public class demo_REST : Idemo_REST
  {
      //change public void DoWork() to public string DoWork()
      public string DoWork()
      {
          //add return text
          return "Hello WCF";
      }
      /// <summary>
      /// Code behind the Operation contract
      /// </summary>
      /// <returns></returns>
      public string GetCatelog()
      {
          //Read the XML File.. *You need to give XML file as URL
          XDocument xmlDataSheet = XDocument.Load("http://localhost:55173/cd_data.xml");
          //Get all items in XML file to the code
          var allItems = from data in xmlDataSheet.Descendants("CD")
                         select new
                          {
                              Title = data.Element("TITLE").Value,
                              Artist = data.Element("ARTIST").Value,
                              Country = data.Element("COUNTRY").Value,
                              Company = data.Element("COMPANY").Value,
                              Price = data.Element("PRICE").Value,
                              Year = data.Element("YEAR").Value
                          };
          //Json Serializer initiation
          JavaScriptSerializer toJson = new JavaScriptSerializer();
          //convert the data in the xml to list and convert them as JSON string
          return toJson.Serialize(allItems.ToList());
      }
  }


Run and check Our new addition is working or not.

Make It RESTful

 With our Web service Most hard part is done and now we nee to make our web service as restful service. For easyness of understanding the REST I'm using string parameter to the GetCatelog()
method Now its look like GetCatelog(string Parameter) in both Open demo_REST.cs and Open demo_REST.svc files.

To make our web service REST enable we need to add Web Invoked functionality to service. Lets consider  the HTTP GET here.

//our newly added operation contract.
       //We need output as json and input text is as json and our GET Url is /GetCatelog?para=textInput
       //Make sure you add correct parameter to correct input when working with multiple parameters
       //You can try different methods
       [OperationContract]
       [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json,
          UriTemplate = "GetCatelog?para={Parameter}")]
       string GetCatelog(string Parameter);

Here is POST

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "GetCatalog")]

Lets make it happen..

In final step you need to make sure few things configured correctly. configurations are on web.config file.

To make sure our endpoint of web service getting the JSON out put we need to add endpoint behavior to the web.config under <behaviors> tag

<!--End point json behavior-->
      <endpointBehaviors>
        <behavior name="jsonbehavior">
          <webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json"/>
        </behavior>
      </endpointBehaviors>

Under the <system.servicemodel> tag we need to add and configure our web service to enable HTTP (REST) and endpoint behaviors

<services >
    <!--adding service configuration-->
    <service name="REST_Demo.demo_REST"  >
      <endpoint
                address=""
                binding="webHttpBinding"
                behaviorConfiguration="jsonbehavior"
                contract="REST_Demo.Idemo_REST" />
    </service>
  </services>


We are done.. then run and test your service in browser
Here is my credentials. http://localhost:55173/demo_REST.svc/GetCatelog?para=text
 Result 






Full Code 
http://bit.ly/wcfREST