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.

AddAjaxEnabledWCFService

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

autocompleteExtenderTag

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 elJuan.org :-)

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.

servicecodefile

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

servicecodefile

 

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.

servicecodefile 

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.

servicecodefile

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()

servicecodefile

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

servicecodefile

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...

servicecodefile

 

And the end result...voila!

servicecodefile

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.

Thanks,

8 comments:

David said...

Thanks for the great post :) I've been meaning to do some similar exercises with WCF. I'm also keen to try using JQuery's AJAX functions rather than the MS ScriptManager stuff.

elsharpo said...

Thanks david, appreciate your encouraging comments! :-)

I haven't any work with JQuery at all, I look forward to your post on it. We can all benefit! :-)

elsharpo said...

But yeah, I got to have a good look at it too.. another thing on my list of to do stuff....

FTK said...

Good job Juan
I cant wait for your next exiting post. Love this blog
FTK

elsharpo said...

Thanks Frankie! Let's see if we can keep it up mate.

When are you going to start your blog?

Troy said...

Nice post, only one thing I'm not sure on; What's the benefit of building the functionality in this way as opposed to the old fashioned way of just building a web service and having the auto complete control point to it? Also, the service path is defined twice (once in the auto complete control and once in the script manager), is this necessary (ok, so there were two things I wasn’t sure about)?

Anonymous said...

Does this work for .Net 3.0?

Hank said...

hi guys, I do not know if this comment is late. I have WCF that uses TCp and is running as windows service. is there a way I can use it in autocompleteextender??

Best Regards
HANK