Duplex client weve made several changes to the

Info icon This preview shows pages 533–535. Sign up to view the full content.

View Full Document Right Arrow Icon
Duplex client We’ve made several changes to the contract: we modified the one existing method, added two new methods, and turned it into a duplex contract. We also changed the binding. Any one of these changes would need the client to be updated, because each has an impact on the work done by the Add Service Reference operation. (All these things change the contract, the configuration, or both.) However, we don’t need to completely redo the work of adding the service reference. If you right-click on an item in a client’s Service References in the Solution Explorer, you’ll see an Update Service Reference item. This modifies the generated source code and application configuration, saving you from having to build it all again from scratch. This refetches the metadata, so the service needs to be running when you do this, just as when adding the reference in the first place. Once we’ve updated the reference, rebuilding the solution now produces two compiler errors. The call to PostNote fails, because we’re passing in two arguments where the new contract requires only one. And we also see the following error on the line where we construct the ChatServiceClient proxy: error CS1729: 'ChatClient.ChatService.ChatServiceClient' does not contain a constructor that takes 0 arguments Because the service now has a duplex contract, the generated proxy insists that the client implement its half of the contract—we need to provide an implementation of the callback interface and pass that to the proxy. Example 13-10 shows a straightforward implementation of the interface. Example 13-10. Implementing the client-side callback interface [CallbackBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)] class ChatCallback : IChatServiceCallback { public void NotePosted(string from, string note) { Console.WriteLine("{0}: {1}", from, note); } } WCF | 509
Image of page 533

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

View Full Document Right Arrow Icon
The callback interface seems to have changed names. We called it IChat Client on the server, but here it’s IChatServiceCallback . This is the normal if slightly surprising behavior when using metadata exchange through Visual Studio’s Add Service Reference feature. It’s nothing to worry about. As far as WCF is concerned, a contract has only one name ( IChatService in this case), even when it happens to be split into server- side and client-side pieces. WCF considers the name of the client-side interface to be irrelevant, and doesn’t advertise it through metadata ex- change. When you add or update a reference to a service with a duplex contract, Visual Studio just makes up the client-side interface name by appending Callback to the contract name. Notice the CallbackBehavior attribute—it specifies a ConcurrencyMode just like on the server. Again, we’ve specified Reentrant —this means that this particular callback han- dler expects to be dealing with just one session at a time, but can cope with being called back by the server while it’s waiting for the server to do something. We need this so that the server can send notifications to the client inside its PostNote implementation.
Image of page 534
Image of page 535
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