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

Easy steps to call WCF service in Windows Phone 7

Dear All,

Today we will learn how to connect WCF service with Windows phone.

So let’s start with step by step I am using one of the best templates which Windows Phone 7 support it is data bound template.

We add WCF service project to same solution.

Image

We have created a WCF service which returns Category Id, Category name, Category description from Games database. For this we have created a custom data contract as shown in below fig

Image

Now we added the reference of this service to our windows phone project using add service reference menu which comes when we right click on windows phone project.

Once the service reference is added we won half battle now the next challenge is call the method of this service in our windows phone 7 projects. As you know the first step is to create an object of this service which we can do with following code.

 DataServiceFreeTips.CategoryInterfaceClient proxy = new DataServiceFreeTips.CategoryInterfaceClient();

Now all the services which we call using service reference always support asynchronous calling so.

We first create a handler which give us hint when the service get all the data to do this we write following code. And after this service call we call the actual service asynchronous method.

   proxy.GetCategoryCompleted += new EventHandler<DataServiceFreeTips.GetCategoryCompletedEventArgs>(proxy_GetCategoryCompleted);
            proxy.GetCategoryAsync();

Now we are good to go and now we will run it and see. We will get the entire category game list as shown in below fig.

Image

In this way we can call a WCF service.

For detail you can download the code from

You can download the code at https://skydrive.live.com/?cid=1eda2012469ff8ad&id=1EDA2012469FF8AD!243

Thanks & Best Regards,

Rajat Jaiswal

 

 

Some useful Terminology (acronyms)

Hello friends,
Cheers!
Here I am with some useful terminology and these acronyms are generally used now days in broad way take a look.
1) ESB : Enterprise Service Bus
2) POX : Plain OLD XML
3) REST: Representational State Transfer
4) SOAP: Simple Object Access Protocol
5) RIA : Rich Internet Application
6) XML : Extensible Markup Language
7) JASON: Java Script Object Notation
8) DOM : Document Object Modeling
9) XAML : Extensible Application Markup Language
10) LINQ : Language Integrated Query
11) RSS: Really Simple Syndication
12) WCF: Windows Communication Foundation
13) WF: Windows Foundation
14) WPF: Windows Presentation Foundation
15) AJAX: Asynchronous Java script and XML
16) XLST: Extensible Style Sheet Language Transformation
17) INDIGO: Code name of Microsoft windows Communication foundation Technology
18) OSLO: Code name of Microsoft Modeling Technology
19) SOA: Service Oriented Architecture
20) ORCAS: dot net 3.5 Version called ORCAS
21) AVALON: code name of Microsoft Windows Presentation foundation Technology
22) Azure: Microsoft new Operation system Related to Cloud computing
23) Astoria : Code name of Ado.net Data services

I hope you people like it.
Enjoy life with dot net.

Your host
Rajat Jaiswal

WPF Windows Persentaion Foundation with me PART- II

Hello friends,
In this session we are going to take a look how do we connect a silver light application with database.
So here I would like to say that there are basically 4 options by which you can handle database in silver light application.
1) WCF for Silver light
2) Ado.net Data Services
3) Web services
4) RIA Services (need to explore more)

Here I will explain how to connect your WPF Silver light application with database using well know Web service.
It’s easy and I think we all already work on some part of Web services.

So let’s start with it.
We have added a silver light navigation project. In navigation silver light project part we have added 2 new pages employee, register page.
Employee page is for show employee list,And register page is for register employee.


Just see project structure as below.

projectPhoto

And In web part we have added a new web service with name my services.
Whose functionality to save, updates, Delete, and read all employee record.
As shown in below fig.

MyWebService

Now our next step is how to integrate this web service with silver light.
For this we add a services reference in our navigation project with the help of Add services reference menu as shown below.

AddServices

Now you can rename name space according to your choice.

If you do not get any error in referencing then till now you are ok with your work.
Now our next step is calling web method in our pages.
Here I am taking first page which is employee list
We are calling web method which returns all employee lists so here we go.

Private Sub EmployeePage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Try
Dim myBinding As New ServiceModel.BasicHttpBinding()
Dim myEndPoint As New ServiceModel.EndpointAddress(New Uri(“http://localhost/MyTest/MyWebService.asmx&#8221;, UriKind.Absolute))
Dim proxy As New MyWebServiceSoapClient(myBinding, myEndPoint)
proxy.pub_ReadAllDataAsync()
AddHandler proxy.pub_ReadAllDataCompleted, AddressOf proxy_ReadAllEmployeeCompleted

Catch ex As Exception

End Try
End Sub

Private Sub proxy_ReadAllEmployeeCompleted(ByVal sender As Object, ByVal e As myService.pub_ReadAllDataCompletedEventArgs)
Try
Me.myGrid.ItemsSource = e.Result
Catch ex As Exception
Me.HeaderText.Text = ex.InnerException.Message.ToString
End Try
End Sub

Now things to remember here
1) Dim myBinding As New ServiceModel.BasicHttpBinding()
Its shows the binding is http binding
2) Dim myEndPoint As New ServiceModel.EndpointAddress(New Uri(“http://localhost/MyTest/MyWebService.asmx&#8221;, UriKind.Absolute))
Here the path can be absolute or relative.

3) Next you have to call web method like we have call here proxy.pub_ReadAllDataAsync()
4) once we have call the method asyncronously then we have to make a event handler like we have create here

AddHandler proxy.pub_ReadAllDataCompleted, AddressOf proxy_ReadAllEmployeeCompleted

5) when the asyncronous method is complete we have to take result complete argument and covert according to our requirement.
I have just bind the result to datagrid and get below screen.
Just see below.

employeeList

So friends in this way we can call web service in a Silverlight application.

Still we can use DataServices, RIA services, and WCF services for data manipulation application.

Hope in next few chapter we will take this example and work on it.

That’s all friends, thanks for reading the article.

Happy programming!

Thanks
Rajat

WCF Windows Communication Foundation in 5 minutes Part 1

Hello Friends,

Today I am going to discuss new topic which is WCF.

As you know today market demand is Services. Every company needed Service Oriented Architecture (SOA) and in SOA first and basic thing is communication for service so Microsoft provide a  new way of communication for services which is WCF. So let’s start with our questionnaires.

1. What is WCF?

Its full name is Windows Communication Foundation. It’s also called Indigo. Its basically introduce in Dot net 3.0.

WCF is new way of communication between client & server. Its technology by which a client & server can communicate with each other. It provides direct support for Service orientation. Its help in distributed application creation its work as separate layer for communication.

Or in other way we can say that its combination of

1) .Net Remoting

2) MSMQ

3) Web Services

4) COM +

Its basically provide Service.

The Mail dll for this is “System.ServiceModel.dll” and main Namespace Is ” System.ServiceModel”

In WCF there are 3 basic conceptual parts.

1) Services: – The Services are programs which respond to clients. They can send or receive Messages.

2) Client: – Client is that program which consume services the request for services. They either send messages or receive messages or both.

3) Intermediaries: – that’s server as bridge between client & services as shown in below fig.

 clip_image0012

2. What’s advantage?

Now as a human tendency you will think “Why?”  While we have Dot net Remotings, Web Services MSMQ, DCOM then why Microsoft introduces the “WCF” indigo.

So I will say that Microsoft Sees future. Now the era is For SOA (Service Oriented Architecture). So that’s the time demand to come with solid technology which can communicate Dot net to dot net application  + dot net to Other language Application too.

When you talking about dot net to dot net application then in that case Remoting is perfect.

But when you talking about dot net to other application Web Services is perfect. But when you talking about both the things at same time then?

At that time WCF is most perfect.

Now here if you go in deep Remoting use TCP/IP protocols for communication & web services stick with SOAP or http.

So Microsoft takes WCF in picture which is much flexible.

Other than that there are few more advantages

SOA: – Its provide Service Oriented Architecture. It provides developer to make just a simple Config base communication model.

 So just define your services and use those services according to you.

Interpretability: – WCF provide Interpretability. It integrates with other technologies also and gives great way to work with them.

Ws-* :- WCF follows Ws-* specification  which are define by Microsoft, SUN, IBM and many other big company together so that they can expose there services with common protocol.

3. Necessary component?

There are following WCF which are as below:-

1) Contract Definition: – A Service must have a service contact. It can contain multiple Service contract, Data Contract or operational contract.

2) End point Definition: – Address binding contract for binding necessary

3)  Hosting code: Some hosting code needed to create service & start services

In other word we can say service class also needed.

 

4. How it works?

To understand the WCF you have to learn ABC doesn’t laugh on it: D. Here ABC means Address Binding Contact.

 Here A- Address says where there is service or which services we have to contact.

        B- Binding says How to contact with services which ways we have to connect.

       C- Contract means what part of services which we have to access.

 clip_image002

We will study in detail in my next post with sample code.

So just wait for 2nd part of this till than

Thanks & Esteemed Regards

Rajat