Upgrading ASMX web services to WCF

by Ishai Hachlili 15. June 2008 09:21

There is a lot of information on the web on using WCF for JSON, but I figured I’ll writing something short on the way I use it.

When I decided to try WCF, the first thing I noticed was how simple it was to upgrade. All you need to do is add a new WCF Service to the web project, copy the code from the ASMX service, change the [ScriptService] to [ServiceCntract] and the [WebMethod] tags to [OperationContract] and add some definitions in the web.config file.
You’re supposed to use a separate Interface to define the service contract, but it’s not a must and you can add the tags to the actual class with the implementation.

The second thing I noticed is that I can get JSON by changing some configuration settings.
When I was working on the AJAX search page, WCF was not around (it was by the time I posted the series on it here). I used XSLT to transform data to JSON and arrays. It was another step I had to do on the server side but it’s worth it, JSON is smaller than XML and very easy to work with in JavaScript.
With WCF I just change a small definition and I get the results in JSON, no need for the XSLT transformation.

Another thing we get with WCF is Data Contracts. The way I’m using it in this sample, it’s pretty much the same as using the .Net AJAX GenerateScriptType.
All I had to do is add a [DataContract] tag to the class and [DataMember] tags to each property.
I think it’s better to define this on the exposed class itself instead of adding some register definition in a web service.

The attached project is the same search page sample with the WCF service. The changes to look for are:

  • The new DataService.svc file
  • The Entities.QueryParameters classes now have the DataContract tags
  • web.config has the new serviceModel section at the end, that defines the service as an HTTP JSON enabled service
  • default.aspx – the service defined in the ScriptManager was changed to the svc file and the path was updated in the javascript Search method


WcfAjaxSearchSample.rar (883.99 kb)

Tags: , ,

AJAX | Asp.Net | WCF


by Ishai Hachlili 30. May 2008 11:40

This week I got certified, I guess. I took two tests, each one took 30 minutes or so, and got an MCTS Web Dev certification.
Because it was so easy (and cheap, test prices are 125$ but with discounts you can take a test for 85$) it makes me feel like this certificate doesn't mean much to those who know what it is.

.Net and Visual Studio can make it to easy to just drag and drop to create an application. Microsoft also has a lot of built in tools to make life simpler. Mastering these tools and drag and dropping seemed to be the focus of these tests. Sure, there were some questions about serialization, but after working with .Net ever since the first release (was it a preview or a beta? too long ago to remember) I found that most of the questions were about things I've never had to do.

Even without having experience with some (or most) of what Microsoft decided a .Net web developer should know, it was very easy to pass these tests, I guess after such a long time working with the .Net framework you understand the philosophy behind it and can guess how the folks at MS would've called a method, even f you never heard of it before.

There is a whole business built around certifications, Microsoft offers online courses and books and there are plenty of other companies in the game as well, so maybe it's all about the money, or maybe it's more important for IT certifications (system and network administrators should probably know what their doing, if you let them near the most important servers in your company).

So now I have my MCTS Web Developer certification, my guess is it won't hurt but I'm not sure how much it will help. I was planning on doing a third test to get the MCPD, but the only reason to do it now is to check if that test is any harder.

(btw, according to the Microsoft Learning web site, there are just over 20,000 MCTS Web Dev 'achievers', and 4,500 MCPD Web Dev while the number for certified windows 2003 sys admins is 186,000. This numbers are updated for April 2008, seems like not too many people felt the MCPD is worth it...)


Tags: , ,

MS Certification

I disappeared into some new technologies...

by Ishai Hachlili 14. May 2008 23:29

I've been MIA from my blog for a while, too many new technologies and libraries that I want to learn...

I've been playing around with upgrading the application the AJAX search page sample was based on to use WCF and EXTJS.

The WCF part was easy, it's not that different from ASMX when you want to do basic stuff. The main difference is you have to create a data contract, which can be done by simply adding a [ServiceContract] tag to the class and [OperationContract] tags to each exposed method.
The better way is to create a separate interface that will be used for the data contract, but if you want to upgrade an existing ASMX service, you might want to take the easy way out...
The other thing you have to do is add the serviceModel section to the web.config.

The cool thing about WCF is that I no longer have to worry about how the data is converted to JSON, with WCF it's simply a definition in the web.config, so changing between XML and JSON can be done at any time without changing code and deploying it. Also, you can have more than one access point to the same class, so you can actually return both formats from the same code.

Changing my code to WCF took a couple of hours, half of it to learn about WCF basics and the rest to copy over the code to a new class, but WCF can go a lot further with it's extensibility.

WCF also let me create the javascript proxy classes including classes for the data contracts (server side objects I want to send to and from the client side). So I don't need to use the Asp.Net AJAX GenerateScriptType anymore.

EXTJS - An AJAX and UI JavaScript Library
This is currently the best UI library out there. initially I wanted to use their grid instead of the WebFX grid I've been using, but after a short time playing with it I decided to try and create a while application with extjs for the entire UI.

This library supports using other libraries for the communication layer, so if you're already using prototype or jquery, you can use extjs just for the UI components. It does have it's own AJAX implementation, so you don't have to use other libraries and if you don't need anything else from these other libraries, it's best not to use them (too heavy and eventually too many libraries will have to mean conflicts)

So I started a new project without the MS Asp.Net AJAX. Passing objects between the client and server was easy with the WCF data contracts generating javascript classes for me, which is why I haven't blogged for a while.

the extjs community is excellent, there are a lot of additional UI controls and plug ins created by the community.
One of them is a form generation class that creates a form from some json. I created something similar for my search pages (with a lot more layout support that was required by the specs). The class is called MetaForm you can find it at Saki's extension page along with some other great controls.

I will eventually return to that series of posts and recreate it, if I don't jump to some other technology by then (I have been thinking about doing something similar with silverlight, we'll see where that goes).

As always, web technology is moving fast and new things are coming out all the time, it's hard to keep up...


About Me

Ishai Hachlili is a web and mobile application developer.

Currently working on Play The Hunt and The Next Line

Recent Tweets

Twitter October 23, 05:22
@BenThePCGuy a standard where that doesn't matter is better. One more reason to get the #Lumia920, wireless charging, no need for microUSB

Twitter October 23, 05:21
@ManMadeMoon where they dance around the issues and don't really talk about them

Twitter October 23, 05:20
@BenThePCGuy are you a @wpdev ?

Twitter October 23, 04:17
@JonahLupton But if it's black it's usually better

Twitter October 23, 02:58
@jongalloway next time ask your 5 year old how to spell