Code Samples


The companion CD inside this book contains the code samples that you’ll use as you perform the exercises. By using the code samples, you won’t waste time creating files that aren’t relevant to the exercise. The files and the step-by-step instructions in the lessons also let you learn by doing, which is an easy and effective way to acquire and remember new skills.

Installing the Code Samples

Follow these steps to install the code samples and required software on your computer so that you can use them with the exercises:

  1. Remove the companion CD from the package inside this book and insert it into your CD-ROM drive.

    Note 

    An end user license agreement should open automatically. If this agreement does not appear, open My Computer on the desktop or Start menu, double-click the icon for your CD-ROM drive, and then double-click StartCD.exe.

  2. Review the end user license agreement. If you accept the terms, select the accept option, and then click Next.

    A menu will appear with options related to the book.

  3. Click Install Code Samples.

  4. Follow the instructions that appear.

    The code samples are installed to the following location on your computer if you are using Windows XP:

    My Documents\Microsoft Press\WCF Step By Step

    The code samples are installed to the following location on your computer if you are using Windows Vista:

    Documents\Microsoft Press\WCF Step By Step

Installing and Configuring the Microsoft .NET Framework 3.0 (Windows XP only)

The exercises and samples in this book have been tested against the RTM version of the .NET Framework 3.0. If you have previously installed an earlier version of the .NET Framework 3.0, you must uninstall it and use the software provided on the companion CD. Follow these instructions to install the Microsoft .NET Framework 3.0:

  1. Using Windows Explorer, move to the \Software folder on the companion CD.

  2. Double-click the file dotnetfx3.exe. If the Open File – Security Warning dialog appears, click Run.

    Note 

    A 64-bit version of the .NET Framework 3.0 is available in the file dotnextfx3_x64.exe.

  3. In the Welcome to Setup page, read the license agreement. If you agree with the license terms, click I have read and ACCEPT the terms of the License Agreement, and then click Install.

    Installation continues in the background.

  4. When the Setup Complete page appears, click Exit.

    Important 

    You should download and install the Microsoft Windows SDK before installing the Visual Studio 2005 Extensions for .NET Framework 3.0.

    Important 

    If your copy of the Windows SDK does not include the FindPrivateKey utility, copy the file FindPrivateKeye.exe from the \Software\FindPrivateKey folder on the companion CD to the bin folder under the Microsoft Windows SDK installation folder (C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin).

Installing the Visual Studio 2005 Extensions for .NET Framework 3.0

The exercises and samples in this book have been tested against the November 2006 Customer Technical Preview version of the Visual Studio 2005 Extensions for .NET Framework 3.0. Follow these instructions to install this software:

  1. Using Windows Explorer, move to the \Software folder on the companion CD.

  2. Double-click the file vsextwfx.exe. If the Open File – Security Warning dialog appears, click Run.

  3. In the Welcome to the Visual Studio 2005 extensions for .NET Framework 3.0 (WCF WPF) November 2006 CTP Setup Wizard page, click Next.

  4. In the License Agreement page, read the license agreement. If you agree with the license terms, click I Accept, and then click Next.

  5. In the Confirm Installation page, click Next.

  6. When the Installation Complete page appears, click Close.

  7. Close the Internet Explorer window displaying the release notes.

Installing and Configuring the Microsoft Enterprise Library

The exercises and samples in this book have been developed and tested using the January 2006 release of the Microsoft Enterprise Library. Follow these steps to install the Enterprise Library:

  1. Using Windows Explorer, move to the \Software folder on the companion CD.

  2. Double-click the file Enterprise Library January 2006.exe. If the Open File – Security Warning dialog appears, click Run.

  3. In the Welcome to the Enterprise Library for .NET Framework 2.0 – January 2006 Installation Wizard page, click Next.

  4. In the License Agreement page, read the license agreement. If you agree with the license terms, click I accept the license agreement, and then click Next.

  5. In the User Information page, enter your full name and organization, and then click Next.

  6. In the Destination Folder page, click Browse, change the Destination Folder to C:\Program Files\Microsoft Enterprise Library\, and then click OK. Select the Compile Enterprise Library check box, and then click Next.

    Note 

    The exercise instructions throughout this book assume you have installed the Enterprise Library in the C:\Program Files\Microsoft Enterprise Library\ folder.

  7. In the Installation Configuration page, accept the default values, and then click Next.

  8. In the Ready to Install the Application page, click Next.

    While the Enterprise Library installs, you will see a console window appear as the installation program compiles and builds the library.

  9. When the Enterprise Library for .NET Framework 2.0 – January 2006 has been successfully installed page appears, click Finish.

Installing and Configuring the AdventureWorks Database

The exercises and examples in this book make use of the AdventureWorks sample database. If you don’t already have this database installed on your computer, a copy of the database installation program is supplied on the companion CD. Follow these steps to install and configure the database:

  1. Using Windows Explorer, move to the \Software folder on the companion CD.

  2. Double-click the file AdventureWorksDB.msi. If the Open File – Security Warning dialog appears, click Run.

  3. In the Welcome to the InstallShield Wizard for AdventureWorksDB page, click Next.

  4. In the License Agreement page, read the license agreement. If you agree with the license terms, click I accept the terms in the license agreement, and then click Next.

  5. In the Destination Folder page, accept the default location, and then click Next.

  6. In the Ready to Install the Program page, click Install.

  7. When the InstallShield Wizard Completed page appears, click Finish.

    The InstallShield Wizard only copies the database files on to your computer. You must attach the database files to SQL Server to make the database available, as described in the following steps.

  8. On the Windows Start menu, point to All Programs, point to Accessories, and then click Command Prompt.

  9. In the Command Prompt window, move to the \Setup folder on the companion CD, and then type the following command:

     osql -E -S .\SQLEXPRESS -i attach.sql

    This command should complete without any errors (it will display a series of prompts, “1> 2> 1> 2> 3> 4> 5> 1>”).

    Note 

    The script attach.sql contains a CREATE DATABASE statement that assumes that you have installed the AdventureWorks database in the default location on the C: drive (C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data). If you have installed it elsewhere, you will need to create a copy of this file, update the paths it contains, and run this updated script instead. For example, if you are using the 64-bit version of Windows Vista, the database will be installed in the C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data folder by default.

    By default, IIS uses the ASP.NET account when executing Web services. Follow these steps to grant the ASP.NET account access to the AdventureWorks database.

  10. Copy the file aspnet.sql in the \Setup folder on the companion CD to your My Documents folder if you are using Windows XP, or to your Documents folder if you are using Windows Vista.

  11. Using Notepad, edit the file aspnet.sql in your My Documents (or Documents) folder.

    If you are using Windows XP, replace the four occurrences of the text YOUR_COMPUTER with the name of your computer.

    If you are using Windows Vista, replace the four occurrences of the text [YOUR_COMPUTER\ASPNET] with the text [NT AUTHORITY\NETWORK SERVICE] (including the square brackets and spaces).

    Save the file and close Notepad when you have finished.

  12. In the Command Prompt window, move to your My Documents (or Documents) folder and type the following command:

     osql -E -S .\SQLEXPRESS -i aspnet.sql

    This command should complete without any errors (it will display a series of prompts, “1> 2> 1> 2> 1> 2> 1> 2> 1> 2> 1>”).

  13. Close the Command Prompt window.

Installing the Microsoft Windows XP Service Pack 2 Support Tools (Windows XP only)

Some exercises make use of the httpcfg utility. If you are using Windows XP, you need to install the Support Tools. If you are using Windows Vista this is not necessary, as Vista provides its own utility called netsh. Follow these steps to install the Microsoft Windows XP Service Pack 2 Support Tools:

  1. Using Windows Explorer, move to the \Software folder on the companion CD.

  2. Double-click the file WindowsXP-KB838079-SupportTools-ENU.exe. If the Open File – Security Warning dialog appears, click Run.

  3. In the Welcome to the Windows Support Tools Setup Wizard page, click Next.

  4. In the End User License Agreement page, read the license agreement. If you agree with the license terms, click I Agree, and then click Next.

  5. In the User Information page, enter your name and organization, and then click Next.

  6. On the Select An Installation Type page, select Complete, and then click Next.

  7. On the Destination Directory page, accept the default folder, and then click Install Now.

  8. When the Completing the Windows Support Tools Setup Wizard page appears, click Finish.

Granting Access to Your Documents Folder

The practice files and exercises are installed under your My Documents (or Documents) folder. Some of the exercises require IIS to be able to run services installed with the practice files. IIS executes Web services using the ASPNET account under Windows XP, or the NETWORK SERVICE account under Windows Vista. This account must be able to read the contents of the folder holding the files for the service, as well as the parent folder hierarchy. Follow these steps to grant the ASPNET or NETWORK SERVICE account access to your My Documents folder.

If you are using Windows XP, perform the following steps:

  1. Using Windows Explorer, move to the Documents and Settings folder, and then move to your folder. Right-click your My Documents folder, and then click Sharing and Security.

  2. In the My Documents Properties window, click the Security tab.

    Tip 

    If the Security tab is not present, close the My Documents Properties window. In the Tools menu of Windows Explorer, click Folder Options. In the Folder Options dialog box, click the View tab. Scroll to the bottom of the tree-view in the Advanced Settings list, uncheck Use simple file sharing, and then click OK. Right-click your My Documents folder, and then click Sharing and Security. The Security tab should now appear.

  3. Click Add.

  4. In the Select Users or Groups dialog box, type ASPNET, and then click OK.

  5. In My Documents Properties window, accept the default permissions, and then click OK.

If you are using Windows Vista, perform the following steps:

  1. Using Windows Explorer, move to the Documents and Settings folder, and then move to your folder. Right-click your Documents folder, and then click Share.

  2. In the “Choose people on your network to share with” window, type NETWORK SERVICE in the text box, and then click Add.

  3. Click Share.

  4. In the “Your folder is shared” window, click Done.

Using the Code Samples

Each chapter in this book explains when and how to use any code samples for that chapter. When it’s time to use a code sample, the book will list the instructions for how to open the files. The chapters are built around scenarios that simulate real programming projects, so you can easily apply the skills you learn to your own work.

For those of you who like to know all the details, following is a list of the code sample Visual Studio projects and solutions, grouped by the folders where you can find them.

Important 

Many of the exercises require administrative access to your computer. Make sure you perform the exercises using an account that has this level of access.

Open table as spreadsheet

Solution Folder

Description

Chapter 1

ProductsService

This solution gets you started. Creating the ProductsService project leads you through the process of building a simple WCF service hosted by IIS. The service enables you to query and update product information in the AdventureWorks database.

The ProductsClient project is a console-based WCF client application that connects to the ProductsService service. You use this project for testing the WCF service.

Chapter 2

ProductsServiceHost

This solution contains Windows Presentation Foundation application that provides a host environment for the ProductsService service. You use this application to manually start and stop the service.

You configure the ProductsClient application to connect to the service hosted by this application by using a TCP endpoint.

WindowsProductService

This solution contains a Windows Service that hosts the ProductsService service. You can start and stop the service from the Services applet in the Windows Control Panel.

You reconfigure the ProductsClient application to connect to this service by using an endpoint based on the Named Pipe transport.

This exercise requires Visual Studio 2005 Professional Edition.

Chapter 3

ProductsServiceFault

The ProductsService service in this solution traps exceptions and reports them back to the client application as Simple Object Access Protocol (SOAP) faults. It defines fault contracts and specifies faults that each operation can throw.

You modify the ProductsClient application to catch the SOAP faults thrown by the service.

Chapter 4

ProductsService

When building this solution, you see how to configure security for the ProductsService service and the ProductsClient application. The techniques you use are appropriate for WCF client applications and services running inside the same organization over a corporate intranet. You learn how to:

  • Apply message level security over a TCP binding.

  • Implement transport level security over an HTTP binding.

  • Implement message level security over an HTTP binding.

  • Implement user authentication by using Windows credentials.

  • Implement user authorization by using declarative and imperative .NET Framework security.

Chapter 5

ProductsService

When creating this solution, you see how to configure security for the ProductsService service and the ProductsClient application using techniques that are appropriate to the Internet. You learn how to:

  • Implement authentication and authorization by using the SQL Server Role Provider.

  • Authenticate and authorize users by using certificates rather than usernames and passwords.

  • Reconfigure the service to authenticate itself to the client application by using a certificate.

Chapter 6

ProductsService

In this solution, you modify the service contract in the ProductsService and run the ProductsClient application to understand which modifications constitute breaking changes and which don’t.

ProductsServiceV2

When creating this solution, you update the ProductsService service and modify the data contract defining the data structures one of its operations returns. You also use the solution to examine how to implement data contract compatibility with client applications that use an older version of the data contract.

Chapter 7

ShoppingCartService

This solution contains a new service that implements shopping cart functionality and a client application that exercises this functionality. You use this solution to understand how to implement services based on sessions and how to maintain state information between operation calls.

Chapter 8

ShoppingCartService

This solution contains a version of the ShoppingCartService service that uses transactions to maintain database integrity.

You modify the client application to initiate a transaction and control the outcome of the transaction.

Chapter 9

ShoppingCartService

You configure the ShoppingCartService service and client application in this solution to implement reliable messaging. You run the client application and use the WCF Service Trace Viewer utility to examine the messages passing between the client application and service.

You then add a custom binding to the ShoppingCartService service that enables you to configure replay detection and test it by using the client application.

Chapter 10

ShoppingCartService

This solution contains an implementation of the ShoppingCartService service that programmatically creates a custom binding rather than using one of the WCF predefined bindings. When building this solution, you also create a custom service behavior that enables you to inspect request messages sent to the service and response messages that it sends back to client applications.

ProductsServiceV2

This solution contains a copy of the ProductsService service from Chapter 6. The client application connects to the service by creating a binding and a channel programmatically rather than using a generated proxy class.

SimpleProductsService

This solution contains a stripped down version of the ProductsService service. The client application connects to the service by creating a binding and a channel and then manually creates and sends a SOAP message to the service. It receives the response also as a SOAP message.

Chapter 11

OneWay

This solution contains a new service called AdventureWorksAdmin. The AdventureWorksAdmin service exposes an operation that can take significant time to run, and you see how to implement this operation as a OneWay operation.

You also use this solution to understand the circumstances under which a OneWay operation call can block a client application and how to resolve this blocking.

Async

This solution contains a version of the AdventureWorksAdmin service that implements an operation that can execute asynchronously.

You also modify the client application in this solution to invoke the operation asynchronously.

MSMQ

This version of the solution contains an implementation of the AdventureWorksAdmin service that uses a message queue to receive messages from client applications. You run the client application and service at different times and verify that messages sent by the client application are queued and received when the service runs.

Chapter 12

Throttling

This solution contains the ShoppingCartService service and a multi-threaded client application that establishes a number of concurrent sessions with the service. You use this service and client application to test the way in which you can configure WCF to conserve resources during periods of heavy load.

MTOM

This solution contains the ShoppingCartPhotoService that retrieves images of products from the AdventureWorks database. The client application displays these images in a WPF form. The service encodes the binary data constituting the image by using the Message Transmission Optimization Mechanism (MTOM).

Streaming

This solution contains a version of the ShoppingCartPhotoService that uses streaming to send the image data to the client application rather than MTOM.

Chapter 13

Load-Balancing

Router This solution contains a WCF service that acts as a load-balancing router for two instances of the ShoppingCartService service. The client application connects to the router, which transparently redirects requests to one instance or the other of the ShoppingCartService service.

Chapter 14

ProductsServiceV3

This solution contains a version of the ProductsService service that implements a callback contract. The service uses this callback contract to enable a client application to register an interest in an event and provide a reference to a method that the service can invoke when that event occurs.

Chapter 15

ShoppingCartService

The ShoppingCartService service in this solution implements claims-based security. The client application uses Windows CardSpace to manage user credentials and send claims information to the service. The service uses verified claims to authorize access to users.

Chapter 16

ASPNETService

This solution contains an ASP.NET Web service and client application. You examine the Web Services Description Language (WSDL) description of ASP.NET Web service to implement a WCF Web service that can support the ASP.NET client application.

WCFService

This is a WCF Web service that implements the same WSDL interface as the ASP.NET Web service. You reconfigure the ASP.NET client application to connect to this service, but you do not modify any code in the client application.

ProductsServiceHost

This is the host application for the WCFService. You use this application to start and stop the service.

Products

This solution contains a COM+ application that you configure to appear to client applications as a WCF service.

ProductsClient

This solution contains a test client application that connects to the Products COM+ application by using WCF.

In addition to these projects, all of the projects have solutions available for the practice exercises. The solutions for each project are included in the folder for each chapter and are labeled Solution.

Uninstalling the Code Samples

Follow these steps to remove the code samples from your computer.

  1. In Control Panel, open Add Or Remove Programs.

  2. From the list of Currently Installed Programs, select WCF Step By Step.

  3. Click Remove.

  4. Follow the instructions that appear to remove the code samples.




Microsoft Windows Communication Foundation Step by Step
Microsoft Windows Communication Foundation Step by Step (Step By Step Developer Series)
ISBN: 0735623368
EAN: 2147483647
Year: 2007
Pages: 105
Authors: John Sharp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net