Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Pro WCF 4: Practical Microsoft SOA Implementation doc
Nội dung xem thử
Mô tả chi tiết
Pro WCF 4
Practical Microsoft SOA Implementation
SECOND EDITION
■ ■ ■
Nishith Pathak
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
[email protected], or visit www.springeronline.com.
For information on translations, please e-mail [email protected], 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.
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 Autonomous ........................................................................................................... 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
■ 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
■ 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
■ 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
■ 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 Performance 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
■ 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
■ 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 Wrapper .............................................................................................. 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
■ 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 Sample ................................................................................................ 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
■ CONTENTS
xiii
■Chapter 13: Implementing SOA Interoperability ................................................. 399
Achieving Java/J2EE Interoperability ............................................................................. 399
Non-Microsoft SOA Platforms ............................................................................................................... 400
Interoprability with WS-I Basic Profile .................................................................................................. 401
Sending Binary Data Over Web Services .............................................................................................. 404
Using WS-ReliableMessaging ........................................................................................ 414
WS-ReliableMessaging Example .......................................................................................................... 415
Platform Support of WS-ReliableMessaging ......................................................................................... 418
Summary ........................................................................................................................ 418
■Appendix: QuickReturns Ltd. ............................................................................... 419
Market Overview ............................................................................................................ 420
Service and Collaboration .............................................................................................. 420
Asset Manager ...................................................................................................................................... 420
Market Maker ........................................................................................................................................ 421
Exchange .............................................................................................................................................. 421
Depository ............................................................................................................................................. 422
Data Contracts ............................................................................................................... 422
Quote ..................................................................................................................................................... 422
Trade ..................................................................................................................................................... 423
Execution .............................................................................................................................................. 423
Settlement ............................................................................................................................................. 424
Position ................................................................................................................................................. 424
Index: ...................................................................................................................... 425
iv
Contents at a Glance
Contents ..................................................................................................................... v
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
■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
■Chapter 7: Implementing WCF Security .............................................................. 219
■Chapter 8: Implementing Reliable Messaging and
Queue-Based Communications ......................................................... 253
■Chapter 9: Using Transactions in WCF ................................................................ 281
■Chapter 10: Integrating with COM+ .................................................................... 303
■Chapter 11: Working with Data ........................................................................... 337
■Chapter 12: Developing Peer-to-Peer Applications with WCF ............................. 371
■Chapter 13: Implementing SOA Interoperability ................................................. 399
■Appendix: QuickReturns Ltd. ............................................................................... 419
Index ....................................................................................................................... 425
xvii
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
■ INTRODUCTION
xviii
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.
■ INTRODUCTION
xix
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.