To explore wcf well build a very simple instant

Info icon This preview shows pages 505–507. Sign up to view the full content.

View Full Document Right Arrow Icon
in the same way no matter what communication mechanisms are in use. To explore WCF, we’ll build a very simple instant messaging application to allow multiple users to chat with one another. So that we can focus on the communication code, the client will be a simple console application. Creating a WCF Project We’ll start with the server for our chat application. If you want to build your own copy of the project as you read, open Visual Studio’s New Project dialog (Ctrl-Shift-N) and in the template list on the left, select Visual C# WCF. Choose the WCF Service Library project template. Call the project ChatServerLibrary . Ensure that the “Create directory for solution” checkbox is checked, and call the solution WcfChat . This project will produce a DLL as its output, because the WCF Service Library project template doesn’t commit to hosting the WCF service in any particular container ap- plication. WCF can run inside IIS, a Windows Service, a console application, or indeed pretty much any .NET application. If you want to use a particular kind of host, you can just create the relevant type of project—for example, instead of creating a WCF Service Library, you could create an ASP.NET web application project if you wanted to host your WCF service in there. (You can add a WCF service as a new item to an existing web project, so you don’t need a WCF-specific project type.) But there are a couple of benefits to this library-based template: as you’ll see shortly, it provides an easy way to do simple manual testing of the service. Also, it means you can host the service in multiple different host applications, which can be useful for automated testing—you can test the service without having to deploy it into its intended environment. Visual Studio will have added a single service to the project, called Service1 . This con- tains some example code that does things we don’t need in our chat application, so we’ll ignore that. (Feel free to delete them if you’re building your own version as you read this.) We’ll add a new WCF Service item to the project with the Add New Item dialog, called ChatService . Visual Studio adds two files to the project: ChatService.cs and IChatService.cs . This reflects the fact that WCF makes a distinction between the code that implements a service, and the contract for that service. WCF | 481
Image of page 505

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

View Full Document Right Arrow Icon
WCF Contracts When two systems communicate over a network, they need to agree on what infor- mation is to be sent back and forth. WCF formalizes this with what it calls contracts . So the IChatService interface added by the wizard represents a service contract . The service contract defines the operations the service offers. As Example 13-1 shows, the interface is marked with a ServiceContract attribute to make it clear that it’s a contract definition.
Image of page 506
Image of page 507
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