Pro WCF 4 Practical Microsoft SOA Implementation SECOND EDITION ■ ■ ■ Nishith Pathak www.it-ebooks.info Pro WCF 4: Practical Microsoft SOA Implementation, Second Edition Copyright © 2011 by Nishith Pathak All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-4302-3368-8 ISBN-13 (electronic): 978-1-4302-4302-3368-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Mark Beckner Development Editor: Jonathan Hassell Technical Reviewer: Henry Li Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Adam Heath Copy Editor: Damon Larson Compositor: MacPS, LLC. Indexer: BIM Indexing and Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. www.it-ebooks.info v Contents Contents at a Glance iv About the Author xiv About the Technical Reviewer xv Acknowledgments xvi Introduction xviI Part I: Introducing Windows Communication Foundation 1 ■ Chapter 1: WCF and SOA Basics 3 Understanding SOA 3 What Is a Service? 5 Tenet 1: Boundaries Are Explicit 6 Tenet 2: Services Are Autonom ous 6 Tenet 3: Services Share the Schema and Contract, Not the Class 7 Tenet 4: Service Compatibility Is Based on Policy 7 A Brief History of the Microsoft Distributed Stack 8 Why Are Web Services the Preferred Option? 10 What Does WCF Solve? 11 Unification of Existing Technologies 12 Interoperability Across Platforms 13 WCF As a Service-Oriented Development Tool 15 Exploring Features in WCF 18 Developer Productivity 18 Attribute-Based Development 18 www.it-ebooks.info ■ CONTENTS vi Coexisting with Existing Technology 19 Hosting Services 19 Migration/Integration with Existing Technology 20 One Service, Multiple Endpoints 21 Integration Technologies 22 How Do You Unify All These Technologies? 22 Summary 24 ■ Chapter 2: What’s New in WCF 4 25 Introducing the ABCs of WCF 25 Standard Endpoints 27 Simplified Configuration 31 Default Endpoints 32 Default Bindings 34 Default Behavior 36 Fileless Activation 37 Service Discovery 37 Support of Visual Studio 41 Routing Service 42 Improved REST Support 48 Workflow Services 51 Summary 53 ■ Chapter 3: Exploring the WCF Programming Model 55 Introducing the Technical Architecture 56 Introducing the Programming Approach 56 WCF Programming Model 57 Addresses 58 Bindings 60 Contracts 62 www.it-ebooks.info ■ CONTENTS vii Looking Inside the WCF Layers 69 The Messaging Layer 70 Using ServiceHost and ChannelFactory 72 ServiceHost 72 ChannelFactory 79 Service Description 84 Service Runtime 84 Applying Behaviors 85 Service Behavior 86 Contract Behavior 88 Channel Behavior 89 Operation Behavior 89 Service Metadata Behavior 89 Using the Configuration Tool 91 Configuring Diagnostics 92 Configuring Instrumentation 96 Summary 98 Part II: Programming with WCF 99 ■ Chapter 4: Installing and Creating WCF Services 101 Understanding the Requirements 101 Hardware Requirements 101 Software Requirements 102 Installing the .NET 4.0 Development Components 103 Understanding Service Contracts 105 Contract First or Code First? 106 Service Design 107 Programming Model 108 Hello World 108 Hello World with Interfaces 110 www.it-ebooks.info ■ CONTENTS viii Hosting on IIS 117 ServiceContract Attribute 120 OperationContract Attribute 125 ServiceBehavior Attribute 128 OperationBehavior Attribute 128 Understanding Data Contracts 129 XML Serialization 129 Data Contracts 136 Message Contracts 138 Summary 144 ■ Chapter 5: Hosting and Consuming WCF Services 145 Hosting Environment Features 147 Hosting Environment Requirements 148 Self-Hosting Your Service 149 Hosting in Windows Services 150 Hosting Using IIS 158 Core IIS Features 159 Hosting WCF Services in IIS 160 Configuring WCF Services in IIS 161 Accessing ServiceHost in IIS 163 Recycling 164 ASP.NET Compatibility Mode 165 Windows XP and IIS 5.1 167 Windows Server 2003 and IIS 6.0 168 Hosting in IIS 7.0 169 WAS 171 Hosting WCF Services in Windows Azure 173 Hosting Options 181 Consuming WCF Services 182 Service Proxies 182 www.it-ebooks.info ■ CONTENTS ix Summary 183 ■ Chapter 6: Managing WCF Services 185 Exploring the Business Drivers 186 Building Custom Code to Monitor Activity 187 Using Configuration Files 194 Configuration Editor: SvcConfigEditor.exe 194 Using Tracing and Message-Logging Capabilities 195 Message Logging 195 Enabling Message Logging 196 Enabling Tracing 199 Using SvcTraceViewer.exe 199 Using WCF Performance Counters 200 Enabling Built-In WCF Performance Counters 201 Creating Custom Perform ance Counters 205 Using WMI 212 Summary 215 Part III: Advanced Topics in WCF 217 ■ Chapter 7: Implementing WCF Security 219 Business Drivers 220 Introducing the WCF Security Features 220 Security Features of Bindings 221 Protection Levels 222 Credentials and Claims 222 Presenting Credentials and Claims to WCF 226 Binding Support for Credentials 227 Transport-Level Security 228 Message-Level Security 231 Mixed-Mode Security 236 www.it-ebooks.info ■ CONTENTS x Federated Security Model in WCF 238 Authorization in WCF 239 Authorization Options for One-Way Communications 244 Auditing for Security Features in WCF . 245 Windows CardSpace . 247 How Windows CardSpace Works . 248 Enabling Windows CardSpace in WCF . 249 Summary . 251 ■ Chapter 8: Implementing Reliable Messaging and Queue-Based Communications 253 The Need for Reliable Messaging . 254 Challenges of Implementing Reliable Messaging . 254 Communication Issues 255 Processing Issues 255 Reliable Sessions 256 Enabling WCF Web Services with Reliable Sessions 257 The ReliableSessionBindingElement Class . 260 Some Pointers on Reliable Messaging . 260 Queuing in WCF 262 Installing MSMQ 264 Microsoft Message Queues in Windows Server 2008 266 Transport Channels 267 Integration Channels 274 Some Pointers on Using MSMQ . 278 Summary . 279 ■ Chapter 9: Using Transactions in WCF 281 What’s a Transaction? 281 Understanding the Types of Transactions in WCF 284 Defining Transactions in WCF . 286 www.it-ebooks.info ■ CONTENTS xi Using the TransactionFlow Attribute 286 Using the ServiceBehavior and OperationBehavior Attributes 287 Defining Transactions in QuickReturns Ltd. 289 Working with Transactions and Queues 297 Summary 301 ■ Chapter 10: Integrating with COM+ 303 Why Integrate with COM+? 303 Running a COM+ Application As a WCF Service 304 Visual Basic 6 COM+ Component Sample Setup 305 COM+ Application WCF Service W rapper 309 .NET Enterprise Services and COM + Components 318 Consuming WCF Services from COM+ 325 QuickReturns Ltd. Quote Service 326 Security Credentials with IChannelCredentials 333 Summary 335 ■ Chapter 11: Working with Data 337 Understanding the Data Transfer Architecture 337 Exploring the Serialization Options in WCF 338 Introducing Data Contracts 339 Data Contract Names 341 Data Contract Equivalence 343 Data Contract Versioning 347 Round-Tripping 350 XML Serialization 351 Security 353 Introducing Message Contracts 353 Fine-Tuning SOAP 355 Security 357 Performance 357 www.it-ebooks.info ■ CONTENTS xii Using the Message Class 358 Filtering 360 Filters 360 Filter Tables 361 Best Practices for Versioning 361 Versioning with Schema Validation 361 Versioning without Schema Validation 362 Putting It All Together: Quote Client Sample Application 363 Creating the Service 363 Creating the Client 366 Summary 369 ■ Chapter 12: Developing Peer-to-Peer Applications with WCF 371 Introducing P2P Computing 371 Why Use P2P? 372 The Challenges of P2P 373 P2P Development Life Cycle 374 Windows P2P Networking 375 How Does a P2P Mesh Work? 378 What Is Peer Channel? 380 QuickReturnTraderChat Sample 381 P2P Security 385 QuickReturnSecureTraderChat Sam ple 387 Working with NetShell 390 Listing Clouds 391 Cloud Scopes 393 Listing Peers in a Cloud 393 Cloud Statistics 394 Working with Peers 395 SOA with P2P 396 Summary 398 www.it-ebooks.info [...]... 1 ■Chapter 1: WCF and SOA Basics 3 ■Chapter 2: What’s New in WCF 4 25 ■Chapter 3: Exploring the WCF Programming Model 55 Part II: Programming with WCF 99 ■Chapter 4: Installing and Creating WCF Services 101 ■Chapter 5: Hosting and Consuming WCF Services 145 ■Chapter 6: Managing WCF Services 185 Part III: Advanced Topics in WCF 217... complete guide to Windows Communication Foundation (WCF) from a service-oriented architecture (SOA) perspective With each new version of the NET Framework, Microsoft has improved many key areas of WCF In NET 4.0, Microsoft has given developers a better experience and enabled them to become more productive The book not only provides deep insight into the new WCF functionality that ships with NET 4.0—such... www.it-ebooks.info CHAPTER 1 ■ WCF AND SOA BASICS Exploring Features in WCF To a distributed object veteran, WCF might look like yet another distributed technology WCF has taken a lot of features from the existing distributed stack, but also extends the existing features and defines new boundaries We’ll now discuss some of the new features in WCF Developer Productivity WCF increases a developer’s productivity by... WS-Discovery and routing services in WCF • How queue management and reliable messaging work in WCF • Implementing transaction support in WCF • How to make WCF services interoperable with other SOA offerings • Best practices for using WCF effectively • Developing WCF applications with Visual Studio 2010 It will also address the business drivers that dictate the need for these WCF features, as well as the industry... unified programming model of WCF and how WCF provides the best tools for creating secure, interoperable web services Part 2: Programming with WCF This part covers the technical features of WCF in detail You’ll concentrate on the programming aspects of WCF with the assistance of a fictitious stock market application Chapter 4 guides you through installing and creating WCF services Chapter 5 covers creating... serviceoriented architecture (SOA) Once you have an understanding of some of these concepts, you can appreciate the simplicity and flexibility of Windows Communication Foundation (WCF) This part also discusses how SOA principles are applied in WCF Chapter 1 covers the reasons SOA is needed, and provides a brief introduction to WCF Chapter 2 looks at some of the key new features of WCF that were released with... to a more loosely coupled set of autonomous tasks SOA is more than a collection of services It’s a methodology encompassing policies, procedures, and best practices that allow the services to be provided and consumed effectively SOA is not a product that can be bought off the shelf; however, many vendors have products that can form the basis of an SOA implementation It is important that the services... by WSDL This provides for a great base architecture for building distributed applications Since a web service and its clients are independent of each other, they need to adhere only to the XSD and WSDL document standards in order to communicate The next Microsoft offering to address SOA is WCF We’ll now discuss how WCF complements web services and enhances their value What Does WCF Solve? WCF is not... configuration—but also provides extensive examples to make it easier to put the new concepts into practice You'll learn how to move your current offerings to WCF, and how to integrate those applications with WCF 4.0 This book offers genuine insight into solving real enterprise problems using WCF and NET 4.0 This books covers the following: • The new features of WCF with NET 4.0 • The WCF programming model... default implementation of components, including channels and protocols, but all of them are pluggable and can be replaced with better options without much code modification Prior to NET, processes were used to isolate applications from each other Each process had its own virtual address space, and the code that ran in one process could not access the code or data of another process In NET, one process . Pro WCF 4 Practical Microsoft SOA Implementation SECOND EDITION ■ ■ ■ Nishith Pathak www.it-ebooks.info Pro WCF 4: Practical. 1 ■ Chapter 1: WCF and SOA Basics 3 ■ Chapter 2: What’s New in WCF 4 25 ■ Chapter 3: Exploring the WCF Programming Model 55 Part II: Programming with WCF 99 ■ Chapter