CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN .NET Companion eBook Available Pro WCF 4 Pro Pro WCF 4: Practical Microsoft SOA Implementation is a complete guide to Windows Communication Foundation from the SOA perspective, demonstrat- ing why WCF 4 is critical to service-oriented architecture and development. W Gain deep insight into the functionality of WCF 4 and the improvements from the .NET 4.0 Framework – learn about service discovery, the routing ser- vice, a new simplified configuration paradigm, and other advanced features. Deepen your mastery of WCF 4 through informative examples that will aid you Pro Nishith Pathak, Author of in understanding and implementing these important additions. C Pro WCF: Practical Microsoft Inside, you’ll find coverage of the unified programming model, reliable mes- SOA Implementation saging, security, and the peer-to-peer programming model. You'll also learn how to move your current .NET remoting and web service applications to WCF F WCF 4 and how to integrate those applications with WCF 4. This book offers genuine insight into solving real enterprise problems using WCF and .NET 4.0. In Pro WCF 4, learn more about: 4 • New features of WCF with .NET 4.0 • A comprehensive WCF programming model • How queue management and reliable messaging work in WCF • Implementing transaction support in WCF • Making WCF services interoperable with other SOA offerings • Best practices in using WCF effectively • Developing WCF applications with Visual Studio 2010 Practical Microsoft SOA Implementation The release of .NET 4.0 brought a wide range of new functionality to WCF. Developers and architects with experience using WCF 3.5 or earlier who want to apply this new functionality to their application will benefit greatly from the discussions and code samples in this book. Pro WCF 4 is also a great resource for application developers and architects new to SOA or the core concepts of WCF. Creating the next generation of secure, reliable, and interoperable services Companion eBook THE APRESS ROADMAP Pro WCF 4 SECOND EDITION See last page for details Introducing Expert SECOND on $10 eBook version .NET 4 WCF 4 Pro EDITION C# 2010 and the .NET 4 Platform Nishith Pathak SOURCE CODE ONLINE P www.apress.com ISBN 978-1-4302-3368-8 a t 54999 h a k US $49.99 Shelve in: .NET User level: 9 781430 233688 Intermediate–Advanced this print for content only—size & color not accurate 7.5 x 9.25 spine = 0.875" 472 page count For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance Contents ..................................................................................................................... v About the Author ..................................................................................................... xiv About the Technical Reviewer .................................................................................. xv(cid:1) Acknowledgments ................................................................................................... xvi(cid:1) Introduction ............................................................................................................ xvii(cid:1) Part I: Introducing Windows Communication Foundation .......................................... 1(cid:1) ■Chapter 1: WCF and SOA Basics .............................................................................. 3(cid:1) ■Chapter 2: What’s New in WCF 4 ........................................................................... 25(cid:1) ■Chapter 3: Exploring the WCF Programming Model .............................................. 55(cid:1) Part II: Programming with WCF ................................................................................ 99(cid:1) ■Chapter 4: Installing and Creating WCF Services ................................................ 101(cid:1) ■Chapter 5: Hosting and Consuming WCF Services .............................................. 145(cid:1) ■Chapter 6: Managing WCF Services .................................................................... 185(cid:1) Part III: Advanced Topics in WCF ........................................................................... 217(cid:1) ■Chapter 7: Implementing WCF Security .............................................................. 219(cid:1) ■Chapter 8: Implementing Reliable Messaging and Queue-Based Communications ......................................................... 253(cid:1) ■Chapter 9: Using Transactions in WCF ................................................................ 281(cid:1) ■Chapter 10: Integrating with COM+ .................................................................... 303(cid:1) ■Chapter 11: Working with Data ........................................................................... 337(cid:1) ■Chapter 12: Developing Peer-to-Peer Applications with WCF ............................. 371(cid:1) ■Chapter 13: Implementing SOA Interoperability ................................................. 399(cid:1) ■Appendix: QuickReturns Ltd. ............................................................................... 419(cid:1) Index ....................................................................................................................... 425 iv Introduction This book is a 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 as service discovery, routing services, and simplified 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 • Implementing 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 best practices for applying them. Who This Book Is For The release of .NET 4.0 brought a wide range of new functionality to WCF. Developers and architects with experience using WCF 3.5 or earlier who want to be able to apply this new functionality to their applications will benefit greatly from the discussions and code samples in this book. This book is also a great resource for application developers and architects new to SOA and/or the core concepts of WCF. An Overview of This Book This book specifically targets WCF in .NET 4.0. The text that you hold in your hands is a massive retelling of this book’s first printing to account for all of the major changes that are found in .NET 4.0. Not only will you find brand-new chapters, you will find that many of the previous chapters have been expanded in great detail. This book divided into three parts, each of which contains related chapters. The following xvii ■ INTRODUCTION sections describe each part. The book also has one appendix, where you’ll find a description of the sample application used throughout this book. Part 1: Introducing Windows Communication Foundation This part of the book explains the business motives and pain points of the various distributed technologies developed by Microsoft. It explains how you can address these issues using WCF. Once you understand some of these basic concepts of WCF, including the business and technological factors, you can appreciate its simplicity and flexibility. Chapter 1 covers the service standards and introduces WCF. Chapter 2 explains the new features of WCF that ship with .NET 4.0. Chapter 3 discusses the 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 services, and the various hosting options available in WCF services. Chapter 6 discusses how to manage WCF services to obtain the best return on investment for your application. Part 3: Advanced Topics in WCF Real-world SOA applications will have many demanding features to implement. These complex real-world web service implementations will address security issues (both client and service), reliable messaging, transactions, COM+ integration, data integration issues, and peer-to-peer communications. In Chapters 7 through 12, you will concentrate on these topics. In addition, you’ll investigate the WCF interoperability options available to seamlessly communicate with non-Microsoft platforms in Chapter 13. Prerequisites To get the most out of this book, you should install WCF and the .NET Framework 4.0. I recommend using Microsoft Visual Studio 2010 as the development environment to experiment with the code samples, which you can find in the Source Code section of the Apress web site (www.apress.com). Obtaining Updates for This Book As you read through this text, you may find the occasional grammatical or code error (although I sure hope not). If this is the case my sincere apologies. Being human, I am sure that a glitch or two may be present, regardless of my best efforts. If this is the case, you can obtain the current errata list from the Apress web site (located once again on the home page for this book), as well as information on how to notify me of any errors you might find. xviii ■ INTRODUCTION Contacting the Author If you have any questions regarding this book’s source code, are in need of clarification for a given example, or simply wish to offer your thoughts regarding WCF and .NET 4.0, feel free to drop me a line at [email protected]. I will do my best to get back to you in a timely fashion; however, like yourself, I get busy from time to time. Thanks for buying this text. I hope you enjoy reading it and putting your newfound knowledge to good use. xix P A R T I ■ ■ ■ Introducing Windows Communication Foundation Part 1 of this book introduces web service standards and the fundamental components of service- oriented 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 .NET 4.0. This is followed by a discussion of the WCF programming model in Chapter 3. C H A P T E R 1 ■ ■ ■ WCF and SOA Basics In today’s world, implementing distributed systems that provide business value in a reliable fashion presents many challenges. We take many features for granted when developing nondistributed systems—features that can become issues when working with disparate distributed systems. Although some of these challenges are obvious (such as a loss of connectivity leading to data being lost or corrupted), for other aspects—such as tightly coupled systems—the dependencies between various system components make it cost prohibitive to make changes to meet the demands of the business. Business processes are quite often supported by systems that run on different platforms and technologies both within and outside of the organization. Service-oriented architecture (SOA) is a mechanism that enables organizations to facilitate communication between systems running on multiple platforms. During the past decade, a lot of research has been done in the field of distributed computing. Microsoft and other leading vendors have come up with various distributed technologies. Each of these technologies reduces the convolution of building rich applications and lowers development costs. The latest from Microsoft is Windows Communication Foundation (WCF), which provides a uniform way of developing distributed applications by providing a service-oriented programming model. WCF (formerly known as Indigo) handles the communication infrastructure of .NET 3.x and later, which allows you to create a diverse range of applications through its simplified model. Based on the notion of services, WCF contains the best features of today’s distributed technology stack for developing the connected systems. This chapter not only introduces the fundamental concepts of SOA, but also provides a brief discussion of WCF and its features. After completing this chapter, you will have learned about the following: • What SOA means, and what makes it the preferred approach to designing complex heterogeneous IT systems • The four tenets of SOA • Existing distributed technologies and their pitfalls • The key architectural concepts and features that underpin WCF • How WCF unifies existing distributed technologies Understanding SOA It is not practical to build monolithic systems in current multinational enterprises. These systems often take many years to implement and usually address a narrow set of objectives. Today a business needs to be agile and adapt processes quickly, and SOA is a design principle that can help address this business need. Although any kind of implementation can be an SOA implementation, unfortunately many implementations using web services are marketed as SOA implementations when they are not. 3 CHAPTER 1 ■ WCF AND SOA BASICS In reality, SOA is a collection of well-defined services. A service is an autonomous (business) system that accepts one or more requests and returns one or more responses via a set of published and well- defined interfaces. Each of these individual services can be modified independently of other services to help respond to the ever-evolving market conditions of a business. Unlike traditional, tightly coupled architectures, SOA implements a set of loosely coupled services that collectively achieve the desired results; in addition, since the underlying implementation details are hidden from the consumer, changes to the implementation won’t affect the service, as long as the contract does not change. This allows systems based on SOA to respond more quickly and cost- effectively for the business. Although some of these aspects might be similar to component-based development (which is based on strict interfaces), the key difference is that SOA provides an approach based on open standards and generic messages that aren’t specific to any platform or programming language. As a result, you can achieve a high degree of loose coupling and interoperability across platforms and technologies. ■ Note Although SOA might seem abstract, it marks a significant shift from procedural and object-oriented languages 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 don’t get reduced to a set of interfaces, since they are the keys to communication between the provider and the consumer of the service. In a traditional client-server world, the provider will be a server and the consumer will be a client. When factoring in services, try to model the flow and process on recognized business events and existing business processes. You also need to answer a few questions to ensure a clean design for services: • What services do you need? • What services are available to consume? • What services will operate together? • What substitute services are available? • What dependencies exist between various services and other applications? • Does the application have components that qualify it for being a service? Service orientation, as described earlier, is about services and messages. Figure 1–1 shows an example of how service providers and consumers can coexist with a repository to form an SOA implementation. Service providers are components that execute some business logic based on predetermined inputs and outputs, and expose this functionality through an SOA implementation. A consumer, on the other hand, is a set of components interested in using one or more of the services offered by the providers. A repository contains a description of the services, where the providers register their services and consumers find what services are provided. 4 CHAPTER 1 ■ WCF AND SOA BASICS Figure 1–1. How SOA components interact with each other What Is a Service? The term service has been used to describe everything from web services (discussed later in the chapter) to business processes. You should use services to represent the functions of the business and explicitly define the boundaries of what the business does, which essentially will define what the service can and cannot do. The key is that it is not a technology-driven approach, but rather a business-driven approach. ■ Note The term loose coupling refers to situations in which any two entities reduce the assumptions they make about each other when they try to exchange information. Contrary to this, tight coupling increases the range of assumptions made between two entities (such as when the communication protocol becomes more complex), as well as the efficiency of their communication. However, the two entities also become less tolerant to changes in, say, interruptions in the communication, because they are tightly bound (or coupled) to each other. Local method invocation is an excellent example of tight coupling, because it requires many assumptions to be made between the called and calling routines, since both need to be in the same process, use the same language, pass the same number of parameters in the agreed data formats, and so on. Service orientation is a business-driven modeling strategy that defines business functionality in terms of loosely coupled autonomous business systems (or services) that exchange information based on messages. The term services is used in many contexts, but in the context of service orientation, a service is based on four fundamental tenets. We’ll discuss these four tenets, originally proposed by the WCF team at Microsoft, in the following sections. 5