How This Book Is Organized

This book covers the topics and skills you need to design and develop service-oriented WCF-based applications, illustrating how to take advantage of built-in features such as service hosting, instance management, concurrency management, transactions, disconnected queued calls, and security. While the book shows you how to use these features, it focuses on the "why" and on the rationale behind particular design decisions. You'll learn about not only WCF programming and the related system issues, but also relevant design options, tips, best practices, and pitfalls. I approach almost every topic and aspect from a software engineering standpoint, because my objective is to make you not just a WCF expert, but also a better software engineer. Armed with the insights this text provides, you can engineer your applications for maintainability, extensibility, reusability, and productivity.

While the first edition of this book was published in early 2007, it was actually based on material I had that dated back to 2005. This second edition has provided me with an opportunity to publish a further three years' worth of techniques, ideas, and helper classes, and even a few breakthroughs. I believe this new material will make this edition valuable even to readers of the first edition.

Over the past few years I have interacted with WCF developers on all continents apart from Antarctica, both while teaching WCF and designing systems. This has helped me immensely in finding the best ways of presenting various aspects of WCF and in discovering what concepts many developers struggle with and what they take for granted. This accumulated insight is reflected throughout this second edition, both in the old and the new material.

This book avoids many implementation details of WCF and largely confines its coverage to the possibilities and practical aspects of using WCF: how to apply the technology and how to choose among the available design and programming models. It makes the most of what .NET 3.5 Service Pack 1 has to offer, and in some respects is an advanced C# book as well.

In addition, the book contains many useful utilities, tools, and helper classes I have written, collectively known as ServiceModelEx. My tools, helper classes, and attributes aim at increasing your productivity and the quality of your WCF services. ServiceModelEx is literally a small framework that sits on top of WCF and compensates for some oversights in its design. It also simplifies and automates certain tasks. This book is as much about my tools, ideas, and techniques as it is about native WCF, and my framework also demonstrates how you can extend WCF. Many readers have told me that aside from the explanations in this book, ServiceModelEx is the most valuable asset the book offers, and readers have asked me to highlight it more and to provide an explicit catalog. I have done so in this edition, but I have also kept to my guideline that in principle, readers should not have to use all (or any part) of ServiceModelEx. In practice, ServiceModelEx is your WCF power tools collection. You can also use each helper class, utility, or framework individually, as there are few, if any, interdependencies.

During the past four years I have published a number of WCF articles in MSDN Magazine, and I write the WCF section of the Foundations column for the magazine as well. I used these articles to seed the chapters in this book, and I am grateful to the magazine for allowing me to do so. Even if you have read the articles, you should still read the corresponding chapters here. The chapters are much more comprehensive, are wider in scope (offering additional angles, techniques, and samples) and up-to-date, and often tie their subjects into other chapters.

Each chapter addresses a single topic and discusses it in depth. However, the chapters often rely on those that precede them, so you should read the book in order.

Here is a brief summary of the chapters and appendixes in this book:

Chapter 1

This first chapter starts by explaining what WCF is, and then describes essential WCF concepts and building blocks (such as addresses, contracts, bindings, endpoints, hosting, and clients) and key concepts such as reliability and transport sessions. The chapter includes a discussion of the WCF architecture, which is really the linchpin of all that follows in the subsequent chapters. This chapter assumes that you understand the basic motivation and benefit of service-orientation. If that is not the case, you should first read Appendix A. Even if you are already familiar with the basic concepts of WCF, I recommend that you give this chapter at least a cursory reading, not only to ensure that you have a solid foundation, but also because some of the helper classes and terms introduced here will be used and extended throughout the book.

Chapter 2

Chapter 2 is dedicated to the topic of designing and working with service contracts. First, it covers some useful techniques for service contract overloading and inheritance, and some advanced techniques. The chapter next discusses how to design and factor contracts that cater to reuse, maintainability, and extensibility. It ends by showing you how to interact programmatically at runtime with the metadata of the exposed contracts.

Chapter 3

Chapter 3 deals with how the client and the service can exchange data without ever actually sharing the data type itself or using the same development technology. In this chapter you will see how to deal with some interesting real-life issues, such as data versioning, and how to pass collections of items.

Chapter 4

This chapter is dedicated to answering the question of which service instance handles which client's request. WCF supports several service instance management, activation, and lifetime management techniques, and your choices will have drastic implications for scalability, performance, the programming model, and the business workflow. This chapter presents the rationale behind each of the instance management modes, offers guidelines on when and how to best use them, and also addresses some related topics, such as durability and throttling.

Chapter 5

Chapter 5 deals with the types of operations clients can invoke on a service and related design guidelines, such as how to improve on and extend the basic WCF offering to support callback setup and teardown, manage callback ports and channels, and provide for type-safe duplex proxies.

Chapter 6

This chapter discusses the best practices of error handling, enabling you to decouple the client's error handling from the service's. When required, the chapter shows how services can report errors and exceptions back to their clients, since constructs such as exceptions and exception handling are technology-specific and should not transcend the service boundary. This chapter also demonstrates how you can extend and improve on WCF's basic error-handling mechanism.

Chapter 7

This chapter begins by explaining the motivation for transactions in general, and then discusses the many aspects of transactional services: the transaction management architecture, transaction propagation configuration, the declarative transaction support offered by WCF, and how clients can create transactions. The chapter ends by discussing relevant design guidelines such as transactional service state management and instancing modes.

Chapter 8

Chapter 8 first describes the powerful yet simple declarative way WCF offers for managing concurrency and synchronization, both for the client and the service. The chapter then presents more advanced aspects of concurrency management, such as callbacks, reentrancy, thread affinity, and synchronization context, best practices and guidelines for avoiding deadlocks, and asynchronous call management.

Chapter 9

Chapter 9 shows how clients can queue up calls to services, thus enabling asynchronous, disconnected work. The chapter starts by showing how to set up and configure queued services, and then focuses on aspects such as transactions, instance management, and failures and their impact on both the business model of the service and its implementation. It also presents techniques for streamlining queues, call management, and several original design ideas (such as a queued response service).

Chapter 10

This chapter demystifies service-oriented security by breaking down this multifaceted task into its basic elements, such as message transfer, authentication, and authorization. It also demonstrates how to provide security for key scenarios such as intranet and Internet applications. Finally, it presents my framework for declarative WCF security, designed to automate security setup and to considerably simplify managing security.

Appendix A

This first appendix is designed for readers who want to understand what service-orientation is all about: it presents my take on service-orientation and puts it in a concrete context. The appendix defines service-oriented applications (as opposed to mere architecture) and the services themselves, and examines the benefits of the methodology. It then presents the principles of service-orientation and augments the abstract tenets with a few more practical points required by most applications. In this appendix, I also share my perspective on where SOA and WCF are heading.

Appendix B

This appendix introduces two distinct techniques for enabling the client to pass out-of-band parameters to the service, resulting in a custom logical context: you will see how to use either the message headers or the context binding to achieve this goal. This appendix also presents my helper classes, which greatly simplify and encapsulate the required programming. These helper classes and custom contexts are used in several places in the book.

Appendix C

Appendix C presents my framework for implementing a publish-subscribe event management solution. The framework lets you develop a publishing and a subscription service in at most one line of code. While this pattern could have been included in Chapter 5, I've instead put it in a dedicated appendix because it utilizes aspects described in other chapters of this book, such as transactions and queued calls.

Appendix D

Appendix D starts by explaining what code-access security is and discussing the limited programming model WCF supports out of the box, and then presents my breakthrough for restoring full code-access security for WCF. You will see how partially trusted clients can use WCF, how to host services in a partially trusted environment, and how to execute services in partial trust. This appendix uses advanced .NET and WCF programming techniques, yet the resulting programming model is that of plain WCF.

Appendix E

This appendix presents a general-purpose extensible framework for intercepting calls to your WCF services. It walks through the technique and thought process behind such an extension and shows two examples of how to utilize this simple yet powerful and useful technique.

Appendix F

Appendix F is basically a consolidated list of all the best practices and dos and don'ts mentioned throughout this book. The standard is all about the "how" and the "what," not the "why." The rationale behind it is found in the rest of the book. The standard also uses the terms and helper classes discussed in this book.

Appendix G

The final appendix presents a catalog of the 100 or so public helper types (excluding internal helper types) of ServiceModelEx mentioned in the book, arranged by categories and techniques, with a short description of each.

Get Programming WCF Services, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.