Preface

On the software time scale, WCF is ancient. In August 2001, I first learned the details of a Microsoft effort to rewrite COM+ using managed code. Nothing much happened after that. Then, during a C# 2.0 Strategic Design Review in July 2002, the remoting program manager outlined in broad strokes plans to rework remoting into something that developers should actually use. At the same time, Microsoft was also working on incorporating the new security specs for web services into the ASMX stack and actively working with others on drafting a score of additional web services specs.

In July 2003, I was given access to a new transactional infrastructure that improved on the deficiencies in transactional .NET programming. At the time, there was no cohesive programming model that unified these distinct technologies. Toward the end of 2003, I was privileged to be invited to join a small team of outside industry experts and to participate in the strategic design review of a new development platform codenamed Indigo. Some of the smartest and nicest people I ever worked with were part of that team. Over the next two to three years, Indigo went through some three generations of programming models. The final declarative, endpoint-driven object model debuted in early 2005, was stabilized by August of that year, and was named the Windows Communication Foundation (WCF). WCF was released in November 2006 as part of .NET 3.0

On the software relevancy scale, WCF is a titan. It has all the correct elements required to build a modern software system including the foundational pattern of all modern software; an extensible, interception-based pipeline. It is the basis for the upcoming Azure Service Fabric and it integrates with the Service Fabric as-is. In fact, the previous three editions of this book showed how to implement all the elements of the Service Fabric already. As such, WCF is as paramount as ever.

WCF has several facets, and is the only technology today that offers interoperability, productivity and extensibility. It is the ultimate interoperability solution, an implementation of a long list of industry standards. To the distributed application developer, it is the easiest way of making remote calls and even queued calls. To the system developer, it is the next generation of productivity-oriented features, such as transactions and hosting, that provide off-the-shelf plumbing for applications. To the application developer, it is a declarative programming model for structuring applications. And to the architect, it is a tool for building service-oriented applications. WCF is, in actuality, all of those, simply because it was designed that way—to be the unified offering of Microsoft’s disparate technologies.

To me, WCF is a superior development platform, which to a large extent subsumed raw .NET programming. All .NET developers should use WCF, regardless of their application types, sizes, or industry domains. WCF is a fundamental technology that provides an easy and clean way to generate services and applications in compliance with what I regard as sound design principles. WCF was engineered from the ground up to simplify application development and deployment and to lower the overall cost of ownership. WCF services allow you to build service-oriented applications, from standalone desktop applications to web-based applications and services to high-end Enterprise applications.

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, security, and the new Windows Azure Service Fabric. But in fact, this book is not about WCF. It is about modern software engineering, using WCF as a medium to express these design ideas. The various concepts disused in this book transcend technologies. So while the book shows you how to use these features in WCF, 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.

This fourth edition has provided me with several opportunities: first, to catch up with WCF in .NET 4.6 with its new features of parallel processing and configuration. Second, I wanted to present the upcoming Azure Service Fabric, which is a fundamentally disruptive technology because of the sort of applications it allows developers to build and how it unifies development and operations. Third, I have had a few more years’ worth of WCF techniques, ideas, and helper classes, as well as improvement of the ideas I had in the previous editions. I believe this new material will make this edition valuable even to readers of the third edition.

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 4.6 and the Service Fabric 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. ServiceModelEx 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. 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.

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, WCF Essentials
This first chapter starts by explaining what WCF is, 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, Service Contracts
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, as well as some advanced techniques. The chapter also discusses how to design and factor contracts that cater to reuse, maintainability, and extensibility. It ends by showing you how to interact programmatically with the metadata of the exposed contracts at runtime.
Chapter 3, Data Contracts
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. This chapter also extends the contract design and factoring discussion of Chapter 2 to data contracts.
Chapter 4, Instance Management
This chapter answers 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, Operations
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, Faults
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, Transactions
This chapter begins by explaining the motivation for transactions in general, 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, Concurrency Management
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. The chapter also explores the Task-based Asynchronous Pattern (TAP) and how this style of asynchrony relates to WCF, highlighting best practice techniques, as well as, discussing potential pitfalls.
Chapter 9, Queued Services
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, 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, Security
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.
Chapter 11, The Azure Service Fabric
Chapter 11 introduces you to the Azure Service Fabric. This chapter starts by briefly explaining the value and rationale behind the new platform and continues by presenting the Service Fabric’s many capabilities. The chapter ends by presenting ServiceModelEx.ServiceFabric, our operational Service Fabric that simplifies how you prepare for the Service Fabric and allows you to deploy Service Fabric code to any Windows environment that can host WCF services.
Appendix A, Introduction to Service Orientation
This 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, Headers and Contexts
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, Discovery
This appendix starts by describing the basic offering of service discovery and availability announcements introduced in .NET 4.0. Discovery simplifies service and client deployment and management, and allows for great volatility across time and deployment sites. The appendix then shows some simple techniques and helper classes you can use to streamline the programming model. The appendix ends with our technique for TCP-based discovery that mimics regular WCF discovery, benefiting from discovery even where UDP is disallowed.
Appendix D, Publish-Subscribe Service
Appendix D presents several techniques for implementing a publish-subscribe event management solution. It starts with my framework, which lets you develop a publishing and a subscription service in, at most, one line of code. The appendix ends with a discovery-enabled publish-subscribe solution that requires no explicit subscriptions steps.
Appendix E, Generic Interceptor
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, WCF Coding Standard
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, ServiceModelEx Catalog
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.

Introducing Michael “Monty” Montgomery

In 2009 at my annual Architect’s Master Class I met Monty, who at the time was the chief architect of a large enterprise. Over the next few years we kept in touch since Monty was one of the best architects I have ever met. Monty has a natural knack for service-oriented design and the related system issues. He also proved to be a WCF expert. In 2012 he finally made the jump and joined IDesign. The reason I wanted him had almost nothing to do with his technical expertise. Monty has a fundamental appreciation for what it takes to make a developer team of a broad spectrum of acumen productive. Over the years Monty has mentored many architects on how to achieve just that objective, so he was a natural fit to help with this fourth edition of the WCF ‘Bible’. Together we bounced new and old ideas off each other, culminating in the new techniques you read in this book.   

With the addition of a second author, I wanted to keep the original author’s voice from the previous three editions when referring to personal opinions or contributions. As with the previous editions, when the text mentions “I” or “my,” it refers to Juval. When it says “we” or “our,” it refers to both Juval and Monty.

Some Assumptions About the Reader

I assume that you, the reader, are an experienced developer and that you are comfortable with object-oriented concepts such as encapsulation and inheritance. I will take advantage of your existing understanding of object and component technology and terminology, and port that knowledge to WCF. You should ideally have a fair understanding of .NET and know C# 4.5. Although the book uses C# for the most part, it is just as pertinent to Visual Basic developers.

What You Need to Use This Book

To use this book, you will need .NET 4.5 or .NET 4.6 and Visual Studio 2015 or 2016. For the Service Fabric appendix, you will need the Windows Azure Service Fabric SDK. Unless I explicitly mention otherwise, the contents apply to any platform that can run WCF. You may also install additional Windows components, such as MSMQ and IIS.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Used for technical terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for code samples, statements, namespaces, classes, assemblies, interface directives, operators, attributes, and reserved words.

Constant width bold

Used for emphasis in code samples.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Whenever I wish to make a point in a code sample, I do so with the static Assert method of the Debug class:

int number = 1+2;
Debug.Assert(number = 3);

The Assert method accepts a Boolean value and throws an exception when it is false.

The book follows the recommended naming guidelines and coding style available at iDesign. Whenever it deviates from that standard, it is likely the result of space or line-length constraints. As for naming conventions, I use “Pascal casing” for public member methods and properties; this means the first letter of each word in the name is capitalized. For local variables and method parameters I use “camel casing,” in which the first letter of each word in the name is capitalized, with the exception of the first word. The names of private member variables are prefixed with m_:

class SomeClass
{
   int m_Number;

   public int Number
   {get;set};
}

I use ellipses between curly braces to indicate the presence of code that is necessary but unspecified:

class SomeClass
{...}

In the interest of clarity and space, code examples often do not contain all the using statements needed to specify all the namespaces the examples require; instead, such examples include only the new namespaces introduced in the preceding text.

Safari® Books Online

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/prog-wcf-services.

You can also contact the author at: http://www.idesign.net.

The author has posted a comprehensive code library on the IDesign website with more than 150 downloads related to WCF essentials, contract design, instance management, operations and calls, faults, transactions, concurrency, queuing, security, and the service bus. The downloads articulate many of the code snippets in this book in a working fashion. However, because of the large number of the downloads, the maintenance involved, and the fact that these secondary, accompanying examples do not themselves appear in the book, they are provided separately from the official sources.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

I would not have been able to come to terms with WCF in its early days without the constant support of and interaction with the WCF (then Indigo) program managers. I am especially grateful to Steve Swartz, one of the WCF architects, not just for his knowledge and insight, but also for his patience with me and those long IM sessions. Thanks go to Yasser Shohoud, Doug Purdy, and Shy Cohen for the fascinating strategic design reviews and to Krish Srinivasan for his almost philosophical approach to engineering. Working with you guys has been the best part of learning WCF and a privilege in its own right. The following WCF program managers also shared their time and helped clarify WCF: Andy Milligan, Brian McNamara, Eugene Osovetsky, Kenny Wolf, Kirill Gavrylyuk, Max Feingold, Michael Marucheck, Mike Vernal, and Steve Millet. Thanks also to the group manager, Angela Mills.

Outside Microsoft, I am grateful to Nicholas Paldino for his help. This is the sixth book that Nick has reviewed for me, and by now I am hooked. I simply cannot imagine going to print without Nick’s insight, meticulous scrutiny, and diligent help, to say nothing about Nick’s technical competence, which is nothing short of awe-striking and humbling. The books are always a superior product as a result of his editing and feedback.

I dedicate the book to the IDesign Alumni. A unique post-class benefit of attending one of the IDesign Master Classes is joining this private forum where you can discuss practicing our techniques, share observation and insights, and ask pertinent questions. The members of the forum are all world-class developers, architects, and project leads, and the discussion amongst this group is profound, insightful, and of the highest quality. Many of the ideas and techniques in this book were first tried on and trialed by the fire of the Alumni. The best emails I get each day are the discussion of the Alumni forum, and the level of mutual involvement, empathy, excellence, professional integrity and responsiveness is second to none. It is truly inspiring that a community of mostly virtual peers can care about their trade and each other that much. I thank you for every time you made us stop and think about the best way of answering a question, a smarter way of solving an Alumni’s design problem, or the need for an improved technique. It is people like you that we write books for.

Get Programming WCF Services, 4th 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.