Presenter:Mike Taulty
In this session we'll look at both the client side and service side work involved in taking an asynchronous approach to our web services.
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 the net framework of the accession one in a series of short sessions but we are invited 10 minutes looking at how we can do particular things with the net framework minus m Tolkien I work in the developer and platform group in Microsoft in the UK and this particular set out to take a look at a speed net 1 1 what we can do around asynchronous Web services what we can do around doing things them in an asynchronous manner so and here inside a visual studio net 2003 and has set up a little bit of the scenario here so that we can play around with it little while got Deirdre got three projects in here and the first thing I thought he is less of a mocked up version of legacy service so I have this code here is just a console application I ve had a quick look at Dennis console application won t does ears hear lessons on a socket it is very simple quality code
Minute 1
Since it listens socket on the local machine and is except incoming connections it reads some data from the socket that comes in so gather some in incoming data but certain diagnostics to the console throws away the day to accompany the text really no interest in that data and then the data that term it sends back to the person who spoke that it is set to an essentially incremented by values and we generate one K of data and we send it back to the person who contacted us and them close at socket with that person is not very a special code and thus nothing very clever of others and the SNP has this to say is that in the middle of this piece of code is a sleek 30 seconds to the idea is lots of people have systems out there that do this kind of thing that take a long time to do something as we ve got this back end piece of metal infrastructure like that the
Minute 2
Rather socket and does something that takes quite a long time ago now being modern people we no longer want to speak to this thing this way because it s not very open and easy to understand what is that it accepts and what it is that it returns so if I wrapped it with a web service and case of the causes dived onto Web servers here is my web service that is a quick look at the close of this and essentially this wrapped up in a very right way because were still returning my throat is not ready what you want do but in a very lightweight wraps up that legacy service to all this does it takes a string of data it converted into a bunch of bites it makes a socket to talk to that legacy service connected up sends a bunch of bites rendered at the legacy service ignores that and it is really care what comes across it reads back the response from the legacy service returns to its collar so clearly this was seen
Minute 3
Year because of the way legacy service works viciously attack at the very least 30 seconds today so we ve got a long running operation going on here inside of Web service code for comebacks to a limit we also have a simple Windows forms client so that I ve is incredibly simple we have a button in the text box when we click the button we ve added a reference to the Web service or have a web reference across to the World Service and Khashoggi and only click the button recreated a sense of proxy we invoked the wet methods and when that return is backed is 30 seconds later readers take a byte array and prodded into the text box is not really meant to be a useful data on a lighter so in the first density that s just wrong what we have what we have legacy service on much longer that there is just a console application and you consider that sitting listening on its socket
Minute 4
At a skipper that outcomes in Windows forms application and if I can just find it in the least vaguely on the century at the same time I thought that there that there are never going to invoke that Web service and effort and as the vote that left a note is that while this is happening my Windows forms application is completely dead but finally we get a response back okay so one part of fixing this is we can very easily go and invoke this web service asynchronously solid set applications from an under look at this Windows forms application to go back into the code verse then what we can do down here is rather invoking this web service synchronous directly or we can do is make any asynchronous call on the proxy providing the data that we want to provide a list of which five hello and then providing a coal black and some asynchronous data from the past that over some of the make and you call back here
Minute 5
Okay and will just have started to on the phone legacy completed perhaps after that it will not pass any data will pass a second and were we have did it over here is how the private void on legacy completed this advice they can I a sink result for that result and we need to get hold of proxy so we need Passat across the state and then he will need to get that back to eat it to legacy service S discounts via a sink result state so get hold of that and then we can do and talk to legacy service passing the result and getting back the byte array once we got that byte array we need to still set the textile text box so what we need to do now is right function which does out that it was fixed at
Minute 6
Tags are above to a private void set text box text given a byte array and would have thought this check this code into here and that s fine and now we need to find me because this legacy complete will complete on the wrong thread throws will complete an unacceptable spread we need to get back to user Interface Road and invoke this function and the way that we would do that is through using the invoke at method with either needed delegates so let s call that function delegates are to have to pass the byte arrays we have to credit you object to radio and pass nothing across and now it is the function delegate so it s nontrivial but it s not rocket science with no completely split this operation of to do to pieces and we begin it here we completed it here and then just because of the way the Windows controls work we have to then invoke across to another thread user interface rate ratchet change the text on
Minute 7
Export first if he can build code and we can t close as you re still here on legacy complete it should be on legacy complete to build a code and still can t make compilation work and were just register away the icing result with we got the wrong return type I don t have the fine built up if we had gone on the project against alert start of legacy service so that s running let s not start our client we have that running does get some space in on screen and get the choosing side by side suits you were doing to address our invoke function noticed out our client is responsive so it argues is now happy because they re still calling up observers but now the getting a much nicer UI so in doing this week made things better and but our view
Minute 8
It we haven t exactly fixed the problem out the client is looking a lot better than it was before but let s just think about what were doing on the web servers from minutes were sufficient and his applications in the Web service we have to remember that what we re doing it with his view that code from second ears than we going ahead and we are sitting a piece of code here on the web server and were making this callout to sockets to receive some date back and we know that that might take 30 seconds if we were to put this on the load them all happen is the first rebel come en suite facility seconds effectively as will the second one I saw the third one as pretty soon we are going to start using an awful of thread and possibly even run out of threads within the circle to service these requests as we essentially have a scalability problem are introduced by the fact that we are sitting here
Minute 9
Hanging a thread of doing absolutely nothing when we know full well that nothing could have 30 seconds so it really nice it would be if we can take the asynchronous outlook will use on the client side and applied it over here are always looking to asynchronous working on that one would be to take a delicate and pointed at this function here and make it asynchronous ourselves so just in pieces of pseudo code if you like where s there had a delegate D it s okay this is roughly new delicate effort to point out to press the send if you like animated indeed there are studies to look at how we might do that and that would be fine except the problem is that when we do this and we want to delegate at abuse occurred and we did begin in vogue unfortunately than its the power of the same result of the downside if you like
Minute 10
Was into the very same thread pool which were already causing a problem with because we re sitting here doing this sleep so we do is this take up another thread instead of this thread and also problem would be we have to be a little bit careful we can t really allow this method here to return out of the bottom if you like and before the responses ready so coming in here we have facial only return back to Earth peanut now we have acted response to sign back otherwise the use is not going to get his response so it s not enough just to sets of asynchronous were going here and fall straight out of the bottom of this method or the very smart people designer of the net foresaw this kind of problem and gave us a pattern that we can use to go and change Web service here from being asynchronous Web service to being an asynchronous Web service does little ever happens is replaced in some code that the sketches are for us if we go ahead and in
Minute 11
To comment the word method out for a minute and rebuild the service and then there s just run it in a deep of it to have a look at the test page from an anti war ASB governor actually generates for us and if we look at the web pages SQLNet maitres perhaps even a little document was really interesting here if we look at the operations even service had begin and end all will notice is that the there s only one operation on his World Service which is talked to first video nasties and cleverness for to an essentially making our own operation we have right now asynchronous and putting those two pieces together for as sofa go back and code lets and Rifat Druker is such that we can do a real asynchronous operation rather what were doing at the moment so can t take that code hand what we need to do it is essentially up until this point here we know that the
Minute 12
Request we could make this asynchronous will we know that happens pretty quickly to another other that Ryanair is to receive that causes a lot of problems so Scotland s code handle up to do here is the same workers with him before but on the socket class here rather than doing a receiver went on to do is a begin receiving the world has ever begin receive socket is being real asynchronous work it s not doing a thing of creating a separate thread and cerebral just to run this one particular function as my housework is using real windows asynchronous I O to do that work and begin receive is going to take offer from us like that before but it s also going to take in Centre offer the size of the buffer so socket flags which frozen them to be none whatsoever and an alcohol battle with already got a call by coming into this matter that there and some state authorities at some stage Anglicans to completely miss operational going to a
Minute 13
What it hears a call and receive on the socket so clearly we need a way of transporting softly from the call cited out of this course are here so I ve just inserted in code role and type it in in front of you earn and spend extra time that someone has done it essentially ears in begin call as we were saying instead of doing a synchronous received if you begin receive and what we do here is a pasty callback and state that we can provide it here so we don t interfere with the score and stay to tour what we do is we hope we get hold of the IAC result that the socket returns to us that it is a synchronous operation replace that and the state that we need which is socket and also byte array we place those into a hash table indexed by and this results about using the result of an election to a hash table of state and we delay nurse
Minute 14
Safeway and only return a result call oh we do not exist on and then in the end routine here what we do is use the results to index back into that hash table we keep around with all our state that suffers us the socket and the byte array that allows us to give in the socket we can finish the receive operation using the result and finally we can return the byte array back to the caller to associate with guns of military career and using hash table here to move some state from the faceless place but apart from that you standard begin and end a single processing the nice thing about this now is that when a speed on it comes in calls us here as soon as we return this result as Fiona can return it straight back to the pool and doesn t need to keep that french fry processing until he gets signalled at that terms socket response has come back which might have come back and invoke as he is run as very quickly to ensure we didn t break anything with
Minute 15
Going to run off legacy services read it before so that companies listening without to run our client is written to him before that will come up and appear on screen for them both side by side smoother over there and put us here will invoke it are set the video once again soon after woeful 30 seconds of a case we got a letter back and thinks a working as they were before but now we do think asynchronously on the server side as opposed to synchronously as important to say a couple of things about this firstly my implementation on the I look a bit hairy and was made a lot easier here because my begin operation needed to return the racing result unfortunately I was using something that is real a sink I owe i e the socket and returns are racing result if I wasn t doing have to implement pricing was not myself which is a little harder than what were doing here so that s the first thing to say the second link is
Minute 16
Pay is this is not a solution of queueing things so in between the point at which this operation starts executing and this part of the operation complete executing the caller is still waiting from HTTP response and are some fine than other time hour so we can t have vast amounts of time between here and here and we can have small amounts of time and that s fine but we don t want a half minutes saw something between hearing and that will be unrealistic to me to make sure this is not a reflection of queueing is just an isolated off trying to the scalable of the use of threads on the server side when the writing is web services just to summarise what we seem here will receive a toxic gases have begin and end which allows us to do asynchronous invocation from a client and we seemed on the server side we can also be listing of beginning and ending and right asynchronous methods on the server side loneliness be not now
Minute 17
A cool thing to do and we say that this is an applicable fur everything he gets hard if you have dry eyes and resultant indentations ourselves and is also not a solution of queueing things which may take an hour is all minutes or whatever it might be in order to complete at that point but we do have split the operation into two pieces and have declined comeback at a later stage and ask whether it is the National Marts weekly God polling ready at that point where can you go to get more information about services are probably the best place to start would be at MSDN developer Santos that s just a misty and the Microsoft com web services all one word there s some great stuff at there in a section on the left and cycled understanding Web services are some really brilliant articles equally there to get a good pitch of lots of non in this area and if the something is it specifically in this session only thinking home an hour s more information on back there feel free to drop me a mail and am told it Microsoft com has a seven screen their
Minute 18
Drop me a line and I ll arguably response other than that I will thought to hearing CD advice relativity
Click here to go to original video page
Thursday, July 24, 2008
Video: Working with asynchronous ASP.NET Web Services
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment