We need to provide wcf with an instance of this

Info icon This preview shows pages 534–536. Sign up to view the full content.

View Full Document Right Arrow Icon
We need to provide WCF with an instance of this callback implementation, so we modify the code at the start of Main from Example 13-7 that creates the proxy: ChatCallback callbackObject = new ChatCallback(); InstanceContext clientContext = new InstanceContext(callbackObject); ChatServiceClient chatProxy = new ChatServiceClient(clientContext); This wraps the callback object in an InstanceContext —this represents the session, and is essentially the client-side counterpart of the object returned by OperationContext.Cur rent on the server. It provides various utility members for managing the session, but here the only thing we need it for is to pass our callback object to the proxy—the proxy won’t take the callback directly and demands that we wrap it in an instance context. We have a few more modifications to make. Remember that the client now needs to tell the server that it wants to connect, so we can do that directly after asking for the user’s name: Console.WriteLine("Please enter your name:"); bool ok = false; while (!ok) { string name = Console.ReadLine(); ok = chatProxy.Connect(name); if (!ok) { Console.WriteLine("That name is taken. Please try another."); } } This checks the return code to see if the name we entered was already in use, and asks for a different name if it was. The end user can go through the relevant legal procedures to change her name, and then try again. 510 | Chapter 13: Networking
Image of page 534

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The line that calls PostNote no longer needs to pass our name each time, because the server now remembers our name based on our session: chatProxy.PostNote(note); And finally, we should add a line of code at the very end of Main to let the server know we’re going away: chatProxy.Disconnect(); We’re now ready to test the application. We can run the client and service as before, but we want an extra client or two, to test out this multiuser chat service. Visual Studio doesn’t provide a way to debug two instances of the same application, so we need to run the extra instances manually. We can do this by finding the folder where the com- piled program lives. This will be in a subfolder of the project folder—the program will be in a bin\debug subfolder. Running a couple of instances of the client we can type in some different names, and we see notes appear in the service’s console window as the users connect: Service ready Ian connected Matthew connected When we type a note in one of the clients, it appears in all of the client console windows, as well as the server. Our application’s user interface has a long way to go before it’ll become the new live chat tool of choice, but we have now demonstrated a complete, if rather basic, WCF- based application. We have only scratched the surface of WCF, of course—it’s a large enough technology to warrant a book in its own right. Learning WCF , a book we already mentioned a couple of times, is a good choice if you’d like to learn more about what WCF can do. Next, we’re going to look at how to work directly with HTTP.
Image of page 535
Image of page 536
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

0/0 PREVIEWS LEFT
Sign up to access 24/7 study resources for your classes

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern