How to create RESTful WCF Service ?

 

Hello friends,

Many times you heard about RESTful services. I know most of you pretty much aware also but let me Introduce in my way.

The first question  come to your mind which is

What is REST?

So here is my answer it is Representational State Transfer.

Its all about using HTTP protocol.With the help of this you can perform various action like PUT,GET,POST,DELETE etc.

Suppose you have a student database and want to pull a particular roll number’s record. then in that case you will do following thing
http://indiandotnet.in/GetData/Name

Where 123 is roll number. So with simple URL you can fetch the information similar way you can edit,delete and create student record also with similar type of URL(may be few parameters vary depending on your requirement)

How to create a RESTFUL WCF services ?

Creating RESTFUL WCF service is not a typical part Lets understand it by Step by Step Example

So lets start a new project WCF project as shown in below image i have added one project

 REST_Solution_1

Step 2:-

Now  i am adding two methods here which are  basically for showing two different format XML & JASON using REST WCF application. The important point

definedTwomethod

Step 3:-

Now to make this method as a RESTful service we need to add WebGET attribute as  i am dealing with only Fetching and GET method. as shown in above image.  I am using WebGet but you can try different method like, POST,DELETE,PUT using Web Invoke attribute.

To make this simple to understand i am using Student class with only two property first name & Last Name

Student_Class

Step 4: Below are the two methods in which from one method  we are responding XML & from one method we are returning the JASON.

Method_Definition

Step5:

Once step 4 is done we will configure most the most important part of RESTFUL WCF application which is binding , contract  information etc.

Capture

Once the above configuration is done just run the service.

lets run it with fiddler to understand it more.

When we  running the URL with GETData method as shown it below

  http://localhost:51550/Service1.svc/GetData/Name 

running_1

When we run it we get xml Response as shown below

Rest_Response_XML_2

Now we are running other method using JASON method (as shown in below method)

We will get following response.

Response_XML_3

So in this way we can create RESTFul application.

You can download  code from

Thanks & Best Regards,

Rajat Jaiswal

Advertisements

How to create SELF signed certificate with IIS ?

 

Dear friend,

How to create Self Signed Certificate with IIS ?

Sometimes you require Certificate to test various objectives. to achieve this task lets start with step by step as shown below

Step 1: Open IIS and click on server certificates options as shown below

Self_Signed_Certificate_1

 

Step 2:- Once you then server certificates option you will get below screen. If you see on right side there are couple of action items in this we have one action item which is “Create –Self Signed Certificate”. Now click on this action item.

create_Self_Signed_certificate_2

Step 3:- Once you clicked on “Create- Self Signed certificate” you will get below option you just need to write a friendly name and you are good to go.

Self_Signed_Certificate_2

Sep 4:- Once you full fill the detail and click on the finish button. your certificate is ready now you can use it just see in below image.

Self_Signed_Certificate_3

I hope you are good to go with this.

Thanks & Regards,

Rajat Jaiswal

Step by Step Example to implement FaceBook local currency payments (latest September-2013 changes) with JavaScript SDK in ASP.NET ?

 

Step by Step Example to implement FaceBook local currency payments (latest September-2013 changes) with JavaScript SDK in ASP.NET ?

Dear Friends,

I know you came to this blog because you really frustrated how to implement the chances which introduce by FaceBook in local currency payment changes of September 2013 because you not found any specific example so far with JavaScript SDK and ASP.NET .

So don’t worry actually i was also frustrated with latest changes because whatever i have implemented  before this changes is very easy straight forward and have proper example in Facebook developer forum but for this new changes i did not find any thing which is relevant.

With following example you can achieve Static pricing & Dynamic pricing with same line of code.

So here  is the basic steps which you need to follow to achieve Facebook payment dialog and process payment

1. Create a  new ASPX page i have created page with name “MyProductDetail.aspx” in attached sample

2. On this page add meta tags with and also add run at server property (if you want dynamic pricing) see below snap shot

facebook_Local_currency_payment_Sample_Step_1

3. Now for this page in page load event  (C# code) you need to write following things according to your requirement

in brief i can say on page load event we set all the meta tag at runtime according to query string parameter which will be dynamic . just check below screen.

facebook_Local_currency_payment_Sample_Step_2

4. Now open  the page on which you need to implement payment. Add the payment javascript as shown below to invoke payment dialog. You just need to copy the detail of JavaScript from facebook’s new  pay dialog example

facebook_Local_currency_payment_Sample_Step_3

Now you are good to go to run this.

You can test the product URL with value in facebook debugger

whole code can be download from download code.

I hope this will help you in developing the facebook payment integration.

 

Thanks

Rajat Jaiswal

How to Improve query by reducing CXPACKET WAIT type with simple option?

 

Dear All,

I am sure you have faced this problem that when you running query like Aggregation , grouping etc then due to default setting the query uses all the CPU available to machine and this case is parallelism.

Now you are thinking its very good your query will be faster if it distributed amount the multiple processor.

but sometime this is not the case due to this distribution suppose one process is processing slowly then  overall your combine result have to wait this wait is “CXPACKET WAIT”

Now you are wondering how to handle this. so no worries Microsoft provided option to tweak this setting with MAXDOP (Maximum degree of Parallelism ) option.

You can change overall SQL Server setting or for particular query.

see below screen from where you can change MAXDOP settings.

MAXDOP

Now the default value is “0” . It means can use all the CPU.

Now this is not we generally prefer instead of this we can use MAXDOP option in query option.

But before doing this you just need to cross check whether you require it or not ?

if your CXPACKET wait type  if too much then you can use this option

SELECT ISNULL(MAX(EnrollmentHistoryId),0) AS maxenrmollmentHistoryId ,     
           se.StudentId      
       FROM dbo.StudentEnrollment se WITH (NOLOCK)       
       INNER JOIN dbo.student s WITH (NOLOCK) ON s.studentId = sh.studentId       
                             AND s. YearId = @YearID
       WHERE sh.Statusid = @StatusActivateId 
       GROUP BY sh.StudentID
    OPTION (MAXDOP 1)

in the above query we used MAXDOP 1 which means query use only once CPU.

With this option the CXPACKET will reduce but it may be possible your query may take time so be careful.

But this is good option. It helped me to reduce the time.

Lets try if this can help you.

Enjoy  learning 🙂

Thanks & Best Regards,

Rajat Jaiswal

How to Improve query by reducing CXPACKET WAIT type with simple option?

 

Dear All,

I am sure you have faced this problem that when you running query like Aggregation , grouping etc then due to default setting the query uses all the CPU available to machine and this case is parallelism.

Now you are thinking its very good your query will be faster if it distributed amount the multiple processor.

but sometime this is not the case due to this distribution suppose one process is processing slowly then  overall your combine result have to wait this wait is “CXPACKET WAIT”

Now you are wondering how to handle this. so no worries Microsoft provided option to tweak this setting with MAXDOP (Maximum degree of Parallelism ) option.

You can change overall SQL Server setting or for particular query.

see below screen from where you can change MAXDOP settings.

MAXDOP

Now the default value is “0” . It means can use all the CPU.

Now this is not we generally prefer instead of this we can use MAXDOP option in query option.

But before doing this you just need to cross check whether you require it or not ?

if your CXPACKET wait type  if too much then you can use this option

SELECT ISNULL(MAX(EnrollmentHistoryId),0) AS maxenrmollmentHistoryId ,     
           se.StudentId      
       FROM dbo.StudentEnrollment se WITH (NOLOCK)       
       INNER JOIN dbo.student s WITH (NOLOCK) ON s.studentId = sh.studentId       
                             AND s. YearId = @YearID
       WHERE sh.Statusid = @StatusActivateId 
       GROUP BY sh.StudentID
    OPTION (MAXDOP 1)

in the above query we used MAXDOP 1 which means query use only once CPU.

With this option the CXPACKET will reduce but it may be possible your query may take time so be careful.

But this is good option. It helped me to reduce the time.

Lets try if this can help you.

Enjoy  learning 🙂

Thanks & Best Regards,

Rajat Jaiswal

Improve Query Performance by just a small change “OPTIMZE FOR”

 

Dear All,

If your SQL statement is slow then sometimes just a little change help you a lot in my case it was helpful hope it will helpful in your problem also.

Suppose you want to fetch  data from two large tables  for a specific constant value. Let take example of this

suppose you have two table 1. Student table 2. student enrollment table both table have large amount of data.

Now you want to fetch max record of enrollment id student wise for  particular status in Student enrollment  table.

Then for this you need to write following query .

SELECT ISNULL(MAX(EnrollmentHistoryId),0) AS maxenrmollmentHistoryId ,     
          se.StudentId      
      FROM dbo.StudentEnrollment se WITH (NOLOCK)       
      INNER JOIN dbo.student s WITH (NOLOCK) ON s.studentId = sh.studentId       
                            AND s. YearId = @YearID
      WHERE sh.Statusid = @StatusActivateId 
      GROUP BY sh.serializationid

Now if you find here the StatusActivateId  is fixed in this case so  we need to optimize this statement   here we used optimize for option 

SELECT ISNULL(MAX(EnrollmentHistoryId),0) AS maxenrmollmentHistoryId ,     
           se.StudentId      
       FROM dbo.StudentEnrollment se WITH (NOLOCK)       
       INNER JOIN dbo.student s WITH (NOLOCK) ON s.studentId = sh.studentId       
                             AND s. YearId = @YearID
       WHERE sh.Statusid = @StatusActivateId 
       GROUP BY sh.serializationid
     OPTION (OPTIMIZE FOR (@StatusActivateId =21))

if you  compare above statements you will find there is only one difference in last line which is option (OPTIMIZE FOR)

As i shared earlier we know the fixed value so we explicitly added the variable name and value for which we need to optimization for the query.

In SQL Server 2008  this option ins more robust which taking any unknown option.

I hope it will help you some where . Just hit and try.

Learn & Share

Thanks

Rajat Jaiswal