Note that this doesnt actually switch on sessions it

Info icon This preview shows pages 528–530. Sign up to view the full content.

View Full Document Right Arrow Icon
. Note that this doesn’t actually switch on sessions; it merely says that anything that wants to communicate using this contract had better do so with sessions enabled. Remember, the contract is separate from implementations that con- form to the contract. The effect of this setting is that WCF will produce an error if you try to use this contract without enabling sessions; we’ll see how to enable sessions by modifying the client and server configuration files once we’ve finished modifying the code. A session will be established the first time a client connects to a service, which presents our application with another problem. WCF won’t send a message until it has some- thing to send, so our chat client will first connect to the service when we send our first note. (Creating an instance of the ChatServiceProxy does not connect—nothing goes over the network until the first time you try to invoke an operation.) But we want clients to be able to receive notes straight away, without being required to post one first. So we need a way for clients to announce their presence to the service without sending a note. That’s why Example 13-9 adds a Connect method. And we’ve also provided a Disconnect method for clients to announce that they are leaving so that the chat server doesn’t attempt to send notes to clients that are no longer there. (Without this, the server would get an exception the next time it tried to send a message. Although it would notice that the clients had gone, an explicit disconnect is a bit neater—it also makes it possible to tell the difference between users who deliberately leave the con- versation and users who get cut off due to problems.) We now need to update the server to implement the modified contract, and to track the clients. § In general, the WS-* family of web service protocols avoids depending on HTTP. This may seem like a peculiar tendency for web service standards, but a lot of the organizations involved in creating these specifications wanted the message formats to be useful in message-queue-based systems as well as HTTP. So in general, they tend to avoid transport-specific mechanisms. 504 | Chapter 13: Networking
Image of page 528

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

View Full Document Right Arrow Icon
Calling the client from the server Our service is going to need to maintain a list of connected clients so that it can notify every client when it receives each note. We can store the list as private data in our service class, but since that one list needs to be available across all sessions, we need to tell WCF that we only ever want it to create one instance of that class. WCF offers several different modes for creating instances of your service class. It can create one per client session—that’s useful when you want per-session state. But in our case, all notes get sent to everyone, so the only interesting state is global. Since our application state is global, we don’t have much use for per-client instances here. WCF can also create a new instance of your service class for every single request—if you don’t hold any state in the service class itself this is a reasonable thing to do. But in our case,
Image of page 529
Image of page 530
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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