Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST from asp.net 2.0

Tags:

rest

c#

.net

wcf

I just built a asp.net 2.0 web site. Now I need add REST web service so I can communicate with another web application. I've worked with 2 SOAP web service project before, but have no experise with REST at all. I guess only a coupleweeks would works fine. after googling, I found it's not that easy.

This is what I found:

There is NO REST out of box of asp.net.

WCF REST Starter Kit Codeplex Preview 2 base on .net 3.5 and still in beta

Rest ASP.NET Example

REST Web Services in ASP.NET 2.0 (C#)

Exyus

Handling POST and PUT methods with Lullaby

ADO.NET Data Service

...

Now my question,

a) Is a REST solution for .net 2.0? if yes, which one is best solution?

b) if I have to, how hard to migrate my asp.net from 2.0 to 3.5? is it as simple as just compile, or I have to change a lot code?

c) WCF REST Starter Kit is good enough to use in production?

d) Do I have to learn WCF first, then WCF REST Starter Kit? where is the best place to start?

I appreciate any help here.

Thanks Wes

like image 793
weslleywang Avatar asked Mar 27 '09 19:03

weslleywang


1 Answers

If your looking for a project that templates a REST service, you're correct in saying there is no out of the box solution. However, RESTful web services are possible using WCF. The key part is to use several attributes when defining your service functions that let the .NET framework know that the function is not expecting SOAP. The main attribute to use is the WebInvoke attribute.

Here is an example from developer.com:

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/admin/post/{id}")]
void UpdatePost(string id, Post post);

The above code will actually be defined in an interface for your web service. The interface is created automatically when you create your WCF web service project. The actual code for the function will be placed in the class used to implement the web service.

Check out the article on developer.com for a full tutorial. It might seem overwhelming at first if your new to WCF, but after you dive into it, I'm sure you'll start to pick things up quickly. Here is the link for the artile: http://www.developer.com/net/article.php/10916_3695436_1

To answer all of your questions,

a) In .NET 2.0 you should be able to build RESTful services using WSE2.0, but if you have the option to use .NET 3.5, I would strongly recommend going the route of WCF since it is much easier and is designed with REST in mind.

b) Converting your project won't be hard at all. It's just a matter of targetting the new version of the framework in your project settings. Converting a web service from a WSE2.0 service to a WCF service will be a bit trickier though. The easiest way to do so would be to copy the code from each of the different web service functions into the class where you implement the new version of the function. Copy-Paste shinanigans :)

c) I'm not sure what this starter kit is that you're referring to. RESTful web services should be fully supported in WCF which was fully released as of 3.5

d) It would be helpful to understand WCF at least a little before beginning, but it's not crutial to understand it completely in order to get started. I would recommend just reading through the MSDN article on WCF at least once, and then begin working. I'm sure you will come across other questions as you begin, but you can look up those parts as you come across them.

Anyway, I hope this information helps. Good luck to you.

Edit

Some improvements have been made in the REST world. As Darrel Miller mentioned in the comments, WCF was not in fact built with REST in mind. I mis-spoke previously. In fact the framework is built with SOAP in mind and the WebInvoke attribute fills the gap. Although there is a lot of debate around the topic (Web API vs WCF REST), ASP.NET Web API is a new option for building REST services in .NET. I would strongly recommend that anyone who reads this post and is able to use .NET 4.5 in their project look into it as an option.

like image 196
regex Avatar answered Sep 29 '22 10:09

regex