Google Search

Google
 

Thursday, July 24, 2008

Video: Calling Web Services Asynchronously

Presenter:Mike Taulty

Want to invoke a back-end web service without hanging your user interface? Let's look how.

The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video


Minute 0

Hello and welcome to stop the framework nugget session 1 in a series of short sessions will look interesting and key technologies within the net framework minus m Tolkien and working the developer and platform group in Microsoft in the UK and in this particular certain I want to look at its options around calling back services asynchronously so specifically here a look at how we can do this from a smart client application but the techniques can also apply perhaps from a service application at its ready just around him invoking these services are asynchronously totally straight over into a demonstration over visual studio here this grammar saw some sunscreen space at the solutions of the project I have got a web service that I ve built its called slow service and if we have a look at this is a quick look at the code and it are very simple service it has a single method on a call to get customers just hide that when of

Minute 1

I can as a single method on it or get customers which candidates out and it has goes over to see the server in order to and select all the data from the customers they will Northwind populated dataset and then finally it has a little bit of a nap in that it goes to sleep five seconds before it returns the data back to the yet consumer the corner of the web servers to let my work service I ve also written a client application son of service and you can see that really all this is it is a form nonperformers are a simple data Grid and the button and the hand that but we have the code to go and are firstly clear out the data Grid and having cleared at the data Grid William Garland in greater new instance of service proxy we call the get customers must am proxy want to get the date of setback we bind that the Grid on screen server to run this finality we are doing a synchronous invitation right now then

Minute 2

When the going click the button here you see that my UI is completely unresponsive can t do anything on and try to drag this was former and after the five second sleep or we should find is that the data comes back to us and we get on the screen to them ago and if we do that again then once again after a five second sleep the day to contractors and get it on the screen so clearly Ryanair were not built a very responsive UI so we can make this call asynchronous in order to did other betting life in the use look at how we can do this with Kodak shut down the application shall see a generator went to the Roxy for as and in addition to giving us the simple proxy method but also gives us asynchronous parents for any methods with Web service to what we can do is hysterical and get customers

Minute 3

Week ago begin to customers I noticed that by default what was once a son to take a call back from an American called back to the Midlands and Surrey are north of that and because Paterson asynchronous state across to that court that well right now I m an Arsenal air is well so if we do this then the service request begins but clearly we need also when it finishes on and on that term service regrets will also have an end to get customers and you can see that this will to return the data setback in Swinney to find a place to call this thing but is also because you begin method doesn t return a data set him on a return to single that I asynchronous felt so that I know we d need and I asynchronous felt a scoreless result equals begin to customers in the interesting thing about hyoscine result is that firstly has a placing state on it so this corresponds directly to the second parameter that we have opted to pass on all right now it has an 18

Minute 4

A tampon and we look at this vasectomy can use this to wait to see and when the quality completes and also has this is completed in a property iron which we can use to check out when the caller that he completed so it is this first as a way of checking our work has been done to do here is I m going to take the eye a single result of movement into a private members are adequate result so here without his result like this and at 8 p m the Web service proxy and the fans remember very well to so here perhaps what we do is so rather than initialising us here will discuss this character he is other than and do it that way to regret that at class construction time to visit it now were doing his when we come into as the

Minute 5

Occurred we are just beginning the get customers on vacation and now he s summer to chapter the end of the customers method completed just as examples and whether we can put this code can t let go and add an event handler for application idle event so that s when his farmer s son instructed let s add to the application idle event of new event handler am let s call it down your application idle is fine as it enters visit Odeon leaders with my variables was another term fussy about having to the bottom so do this discourage it will do a fairly basic check was so low if the result is not null so if we think we ve got something in progress and a result is completed then furtive officials are resulted in a last we ve done this cheque and then let s go and is serviced

Minute 6

And get customers we need to pass at the result of your you have a little goes back the data set and we can go back in and by matter of great residues table 0 that if it can do now is done is redeemed the beginning get request here I never ever advocated either 20 checked as it is result outstanding and if it is is it completed and don t want has completed its got to go and get the data has been brought back as the young asynchronous enter that operation that is the Dutch effort to results going on at the same time in as all kinds of things that will cause problems application earns if we split up across okay and if it drunk or get data systems in the fondest and slightly get date on this note is that the UI is now responsive and were waiting for idle time to kick him out many applications idle and we come back and we have to go and do an update that definitely do it again again

Minute 7

Occasion is responding nicely to work Windows event but they are quite where we arrive only get the data back an example that back on screen as one way of doing things and that is certainly what the dangers were checking the completion fire polling and we just borrow the applications idols it to her to do the polling forms of the sensible time we keep coming back and checking to see whether the result is completed on and we could also actually wait for the result of a completed so if rather than doing their swimmer to say the result has singled an asynchronous weight handle and we can wait for that think to be completed and if he completed the return trip so we can go out and do that so rather than checking with solid detective result is not long otherwise would have put that back in their service amaranth must progress is right we are rather keep checking is

Minute 8

Son is completed we are waiting for the result to tell us that it s completed now if we don t do this will hit a slight snag the cards at the point where we go and click get data or fines are you becomes unresponsive to get because what we ve done is gone and blocked the user interface threat in that call to wait one and so we found a thread to come process with adversities and until that call returns and would never be a spot of bother so it made him so badly were originally so that was a very smart move so there are really two solutions to us what we done their one is that when the use wait one here we could specify I m a timespan that were prepared to wait for so we can wave a certain number of milliseconds if I was just about 50 ms there are so five is now have a system science

Minute 9

Answer and average your uncle get they to go to the application s responses at home but in truth it s having 50s with the second polls times in it because we are doing that weight one on its idol Luke and it could be costing as much as 50 ms every time we do it does not really ideal the only real way of getting around that would be to move this call to wait one took time out back out of it then it would really wait for us but we customers that cultivate one onto a separate thread aren t that separate thread with the Galway one to and when that was done with callback onto this further and make everything tire that is not really much point doing that because be existing call to begin get customers and can take a callback parameter if the past that callback ran out of there and that s exactly what is in the frame at looking for is anyway so let s go

Minute 10

That this code where we ve been poling of waiting and let s will add a callback gratitude to dysfunction here Chronicle begin and get customers to what we want are not loved as it is in essence over our title they see callback so lets that artists are called my completion callback to outdo ago and add their member variable to a class called a visibility of tight AC callback and colic completion callback just stick with my variable naming convention are just made up Prozac that so it really do set that some were so perhaps that the construction of the form must remove the application idle Cote d Ivoire allows a completion callback equals new AC callback and pointless act than a function within class of this is going to be on

Minute 11

It very and only to write that function is voted down here private for it on call completed and this will need to receive the night a sink result so now rather than having to store the results and I m apparently funny to think that animals reconstruct away at that point we don t you dismember feral animal and what they can do here is we can to service stocks and get customers with the result that not it was back dataset and then we could can do a data Grid one data source equals Diaz tables are ready for viewing and back this should work for us first rebuild Arsenal had to run this now and then when it hit the button without

Minute 12

Begin and after five seconds I should come back to us and refresh what we have a screen so I guess that s enough about Foreign Secretary in SRU I still respond to fear and that five seconds into a slightly elongated and done something timeless strangers happened on the screen here so what s going on at once this might not be the expected result I certainly know that the doing something bad here I am in code within this customers asking the callback am using this encore completed the problem here is that very similar to what we were just talking about this encore completed routine is actually going to be called on eighth read that is not the user interface thread and user interface controls such as this day degree I ll not supposed

Minute 13

Touched across the road so they have with the thread affinity there created on a particular thread they must only be cut from that thread on which they were created and here I m touching this growth in the wrong thread and is behaving strangely I am done that s really from his side result of of me touching it from the wrong thread to what I did to that is to fix those words of the Windows forms has a mechanism I have a school control invoke so I can call my invoke method on this form and you see that it wants are a delicate invoke and some arguments to pass across somebody need another function to be executed on the UI thread as a postal executing this one so and perhaps we could do is recur down right function call to fight her sector and data stores actually lets be explicit and called on UI thread and got invoke that with an object rates are the object of

Minute 14

A pass across lets pass across to young the data set in train so that s fine but now what we need is then firstly we need at a method to invoke some scoreless handset data source on UI thread and let s passed this datasets and DNS and that dysfunction change the data source on the on the grid so that will be fine the if you can t just put their son indication we can t pass the function then straighten him that she have to provide an instance of a delicate summer to need a delegate type and let s define a delicate time and it went to be avoided delegate let s call the town set data source delegate and if I staggered out into freaking greatness is one of those now that some have set data source delegate

Minute 15

That already equals new set data source delegate and that is the call will be set data assaults on UI thread function but it is a case in right and then it we capacity rather than the other function to what s happening here is that when we come back on the on the completion threat we are gathering the data set great in the US as a delegate and only would want to do this every time we met one of retirement is one and were invoking that delegate on the user interface is read passenger data set across so this function here will be called on the user interface read with the dataset and will do the data binding different compilation found it looks like we did in the round and we now go and invoke we get the data one way far five seconds or so hopeless around the degradable dislocation in the correct way rather than what we got the

Minute 16

Round and the UI is still responsive at this point is just around the data goes back that s thus just run again first indications is that the transitional of service and sequel server and so on and so forth to get it when I get in the the UI is responsive and is doing the right things were not getting spreading issue any more so nothing really anything were not really doing is we re not out of the fact that this call to get customers might actually fail in my throat that an exception as old as all kinds of places were exceptions but there could get thrown here if you want to catch those exceptions to do it and they will be returned for is effectively bicycle and get customers to what we would need to do is this rapper a tribal ground that I catch perhaps at a single exceptional something other and receded do the right thing here are without exception at notice that still here we are on the the coal black thread when

Minute 17

On the user interface that so whilst we can do something like that of a message box we shouldn t interfere with the UI from here we have to go through invoking order to do there a setting that is where we could exception handling if we didn t hear there will be seen in this search short session here we seen that we can do in the case of web services asynchronously within that we got begin and end pattern on the proxy which is just generated for as we look to have little poll from results with to how we could wait for results and an odyssey we saw the long awaited results would need either to wake the limited period of time or would need a separate threat away from results for as Alnwick Castle if they going to go as far as that is probably easy just at a callback it is that we ll end up doing anyway and into the begin their ethical and we also saw how we could do without UI thread issues wreckage of the user interfaces only invoked a single thread through the control invoke method that we saw there is a floodlit threatens is the more

Minute 18

Information on this stuff and there s the Web services developer Centre on MSDN are not likely to be a smart client develops and on MSDN at this article if you search out your favourite ascendant and is at full back Powell and done asynchronous Web services you find a few articles that that s written there at their great articles on on both clients and servers I have done asynchronous Web services are fine if you want to follow directly on what you see here feel free to mail me my addresses are on the slide and other than that thanks very much listening and will see

Click here to go to original video page

No comments: