Friday, February 29, 2008

AjaxControl Toolkit AutoCompleteExtender with WCF

Went to Heroes yesterday and one of the things that really impressed was the session on Windows Communication Foundation.

From MSDN, "Windows Communication Foundation (WCF) is Microsoft's unified programming model for building service oriented applications".

First thing to do of course, is add a new Ajax enabled WCF Service to your solution.


What I wanted to do is have the ubiquitous textbox with an autocompleteextender. The code is very simple.


Then I went into MyAjaxEnabledService class and added to get my books names once the user entered the book names.

When creating a WCF service, you have to define the contract for the services that are going to be called by the consumers and describe how to interact with your service.

You do this by creating an interface that defines the input and output types which apply to the ServiceContract Attribute to the interface and the OperationContract Attribute to the methods that you want to expose.

In this case I have set the ServiceAttribute with a namespace value of :-)

And then created the methods that I want to reach from my aspx pages and  expose as part of the public contract and adorned them with the OperationContract Attribute.


Last thing to do, on our ScriptManager we add a reference to our new service and the path to it.



Ok, now we are all set, note that I have for the purposes of this simple post an array with "books" and by pure coincidence, three of them start with 'Coo'.

Run the project and entering Coo.. our service will be called and the books which name start with Coo will be returned to us.


Also not that I have another public contract called GetBookByID(int):String

What I want to do is get a book by it's ID,when we click the "show me" button,  for this simple example I'm just indexing the array, but it can be easily expanded to get data from a database etc.


First we created a number of JavaScript functions that I'm going to use for this calls.

I have wired up the showMe button onclick event to my InvokeWCF() Javascript function.

Note that as I type the name of my WCF service, VS's intellisense will show me the available public contracts, in this case GetBookByID()


Now note how the intellisense again picked up that GetBookByID takes an integer as parameter too, very cool.


You have to define an onSuccess function, and optionally an onFailed and an userContext.

I defined an Success and OnWCFError function for this first time ever WCF service as show below...



And the end result...voila!


This is the first ever time I played with WCF and it's pretty simple. In my next post I'll generate REST and SOAP protocol enabled services.


Friday, February 15, 2008

First Ever

thanks mate. :-)