Essential Windows Communication Foundation......Page 1 Contents......Page 10 Foreword......Page 26 Preface......Page 28 Acknowledgments......Page 34 Why WCF Matters......Page 38 Introduction......Page 40 Implementing a WCF Service......Page 43 Writing a WCF Service Entirely in Code......Page 44 Writing a Service with Code and Configuration Files......Page 47 More on Configuration Files......Page 49 More on Service Hosting......Page 50 Exposing the Metadata Exchange (MEX) Endpoint......Page 51 Writing a WCF Client Entirely in Code......Page 55 Writing a Client with Code and Configuration......Page 56 Discussion......Page 60 Hosting a Service in IIS in Three Steps......Page 61 Tools Support......Page 64 Generating Client Proxy Class and Configuration Files......Page 65 2 Contracts......Page 70 Service Contracts......Page 74 Synchronous Request-Response Operations......Page 75 Asynchronous Request-Response Operations......Page 78 One-Way Operations......Page 81 Duplex Operations......Page 83 Multiple Contracts and Endpoints in a Service......Page 91 Names of Operations, Types, Actions, and Namespaces in WSDL......Page 94 Data Contracts......Page 97 Defining XML Schema for a .NET Class......Page 99 Defining Class Hierarchies......Page 102 Exposing Additional Types in WSDL with KnownTypes......Page 104 Versioning Data Contracts......Page 109 Data Contract Equivalence......Page 112 Working with Collections......Page 113 Message Contracts......Page 115 Typed Messages......Page 116 Untyped Messages......Page 119 Using SOAP Headers with Untyped Messages......Page 122 3 Channels......Page 128 One-Way Communication Pattern......Page 131 Duplex Communication......Page 132 Request-Reply Communication......Page 134 Shape Changing......Page 135 Operation Contract and Channel Shapes......Page 136 Channel Listeners......Page 138 Channel Factories......Page 139 ChannelFactory......Page 141 ICommunicationObject......Page 142 4 Bindings......Page 148 Choosing an Appropriate Binding......Page 153 Sample Application......Page 156 netTcpBinding......Page 160 Local Machine Communication Between .NET Applications......Page 164 netNamedPipeBinding......Page 165 Communication Using Basic Web Services......Page 168 basicHttpBinding......Page 169 Communication Using Advanced Web Services......Page 172 wsHttpBinding......Page 174 ws2007HttpBinding......Page 177 wsDualHttpBinding......Page 180 Comparing Binding Performance and Scalability......Page 189 Communication Using Queued Services......Page 191 netMsmqBinding......Page 192 msmqIntegrationBinding......Page 202 Creating a Custom Binding......Page 205 Binding Elements......Page 208 Transports......Page 209 Encoders......Page 210 Security......Page 211 Transport Upgrades/Helpers......Page 212 Shape Change......Page 213 Exposing a Service Contract over Multiple Bindings......Page 214 5 Behaviors......Page 218 Concurrency and Instancing (Service Behavior)......Page 221 Default Concurrency and Instancing with Sessionless Binding......Page 224 Multithreading a Single Instance......Page 226 Implementing a Singleton......Page 227 Session-Level Instances......Page 230 Controlling the Number of Concurrent Instances......Page 232 Controlling the Number of Concurrent Calls......Page 236 Controlling the Number of Concurrent Sessions......Page 238 Exporting and Publishing Metadata (Service Behavior)......Page 241 Implementing Transactions (Operation Behavior)......Page 244 Transactional Operations Within a Service......Page 245 Flowing Transactions Across Operations......Page 252 Choosing a Transaction Protocol—OleTx or WS-AT......Page 258 Transaction Service Behaviors......Page 260 Implementing Custom Behaviors......Page 261 Implementing a Message Inspector for Service Endpoint Behavior......Page 264 Exposing a Parameter Inspector for Service Operation Behavior as an Attribute......Page 267 Exposing a Service Behavior Through Configuration......Page 270 Security Behaviors......Page 274 Serialization Versus Encoding......Page 278 DataContractSerializer......Page 280 NetDataContractSerializer......Page 284 XmlSerializer......Page 286 DataContractJsonSerializer......Page 289 Preserving References and Cyclical References......Page 291 Sharing Type with the NetDataContractSerializer......Page 297 Roundtrip Serialization Using IExtensibleDataObject......Page 301 Serializing Types Using Surrogates......Page 307 Streaming Large Data......Page 313 Using the XmlSerializer for Custom Serialization......Page 314 Custom XmlSerialization Using Attributes......Page 315 Custom XmlSerialization Using IXmlSerializable......Page 316 Choosing an Encoder......Page 318 Text Versus Binary Encoding......Page 319 Sending Binary Data Using MTOM Encoding......Page 320 Getting to Know the WebMessageEncoder......Page 321 7 Hosting......Page 324 Hosting a Service in Windows Process Activation Services......Page 325 Hosting a Service in IIS 7......Page 329 Enabling ASMX Features in an IIS-Hosted Service......Page 331 Self-Hosting......Page 338 Self-Hosting in a Managed Windows Service......Page 339 Hosting Multiple Services in One Process......Page 342 Defining Service and Endpoint Addresses......Page 345 8 Security......Page 352 Authorization......Page 353 Transport and Message Security......Page 354 Concepts......Page 356 Setup......Page 357 Transport-Level Security......Page 359 Encryption Using SSL......Page 360 Client Authentication......Page 364 Service Identity......Page 369 Message-Level Security......Page 371 Authenticating with wsHttpBinding......Page 372 Securing Services with Windows Integrated Security......Page 377 Section Examples Introduction......Page 378 Authenticating Users with Windows Credentials......Page 380 Authorizing Users with Windows Credentials......Page 383 Authorization Using AzMan......Page 385 Impersonating Users......Page 390 Securing Services over the Internet......Page 395 ASP.NET Integration......Page 397 Authentication Using Membership Providers......Page 398 Role-Based Authorization Using Role Providers......Page 401 Using Forms Authentication......Page 403 Logging and Auditing......Page 408 9 Diagnostics......Page 412 Tracing......Page 413 End-to-End Tracing......Page 414 Activities and Correlation......Page 415 Enabling Tracing......Page 416 Enabling Message Logging......Page 418 Additional Configuration Options......Page 420 Message Filters......Page 421 Trace Source Auto Flushing......Page 422 Performance Counters......Page 423 Using the Service Configuration Editor......Page 424 Logging Options......Page 426 Configuring Sources......Page 427 Configuring Listeners......Page 428 Activity View......Page 430 Graph View......Page 432 Analyzing Logs from Multiple Sources......Page 434 Filtering Results......Page 437 10 Exception Handling......Page 440 WCF Exception Communication via SOAP......Page 441 Unhandled Exception Example......Page 442 Detecting and Recovering a Faulted Channel......Page 445 Communicating Exception Details......Page 446 Managing Service Exceptions with FaultException......Page 448 Using FaultCode and FaultReason to Extend FaultException......Page 449 Limitations of Basic FaultExceptions......Page 451 Declaring Fault Definitions with FaultContract......Page 452 Defining a FaultContract......Page 454 Throwing a FaultException with a Defined FaultContract......Page 455 Implementing Client Fault Handlers......Page 456 Error-Handling Application Block......Page 457 Exception Shielding......Page 458 11 Workflow Services......Page 460 Integration Points......Page 461 Calling a WCF Service from WF......Page 463 Using a Send Activity......Page 464 Writing a Custom Activity......Page 467 Exposing a Service from WF......Page 470 Define the Interface......Page 471 Receive Activity......Page 472 Configuration in app.config......Page 476 Hosting a Service-Enabled Workflow......Page 478 Self-Hosting a Service-Enabled Workflow......Page 479 Hosting a Service-Enabled Workflow in IIS......Page 480 Correlation and Durable Services......Page 481 Long-Running Workflow......Page 482 Handling the Context......Page 487 Persisting Workflow State on the Server......Page 489 Controlling Access to Service-Enabled Workflows......Page 491 Programmatic Access Control......Page 492 Approaches to Building Distributed Applications......Page 496 N-Tier Applications......Page 497 Peer-to-Peer Applications......Page 498 Mesh Networks......Page 499 Message Flooding Versus Directional Messaging......Page 501 netPeerTcpBinding......Page 502 Resolving Peers Using PNRP......Page 505 Windows Internet Computer Names......Page 506 PnrpPeerResolver......Page 507 Registering Names Using PNRP......Page 508 System.Net.Peer......Page 509 Implementing a Custom Peer Resolver......Page 511 Limiting the Number of Hops for a Message......Page 515 Collaboration Using Windows Vista......Page 517 People Near Me......Page 518 Windows Contacts......Page 519 Invitations......Page 520 System.Net.PeerToPeer.Collaboration......Page 522 Directional Messaging Using Custom Binding......Page 529 13 Programmable Web......Page 540 All About the URI......Page 541 The Ubiquitous GET......Page 543 Web Programming with WCF......Page 544 URI and UriTemplates......Page 545 Building URIs......Page 546 Parsing URIs......Page 547 Creating Operations for the Web......Page 548 Hosting Using WebHttpBinding......Page 549 WebInvoke......Page 551 ASP.NET AJAX Integration......Page 553 Using the WebOperationContext......Page 560 WebScriptServiceHostFactory......Page 567 Content Syndication with RSS and ATOM......Page 568 Publishing Metadata Endpoints......Page 574 mexTcpBinding......Page 575 Creating Clients from Metadata......Page 576 Creating Silverlight Clients from Metadata......Page 578 Sharing Ports Between Services......Page 579 Configuring Service Quota Settings......Page 580 Configuring HTTP Connections......Page 582 Adjusting Connection Lifetime......Page 583 Disabling HTTP Keep-Alives......Page 584 Recycling Idle Connections......Page 586 Exposing LINQ-to-SQL Entities......Page 587 A......Page 590 B......Page 591 C......Page 592 D......Page 593 E......Page 594 G–H......Page 595 M......Page 596 O......Page 597 Q–R......Page 598 S......Page 599 T......Page 601 V–W......Page 602 X–Z......Page 603