cyan yelloW MaGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in synchronizaTion Companion eBook Available Pro Sync Framework P Dear Reader, r Pro o The Microsoft Sync Framework is a new technology for creating software that can synchronize data smoothly between applications, data stores, and devices without having to agree on any data characteristics being synchronized in S advance. This flexible approach is a major step forward from previous synchroni- y Sync Rituraj Singh zation solutions that regularly struggled to deal with unexpected events such as storage and application errors, network failures, or unexpected conflicts n between applications. In such circumstances the synchronization system often c failed, leaving users without reliable access to their data. The Sync Framework provides a robust solution to these problems, along with additional benefits such as the ability to take an application offline and F then reconnect seamlessly, and the opportunity to reduce networking costs through improvements in data transfer efficiency and communication. r Framework Pro Sync Framework covers the Sync Framework from the ground up. It a begins with detailed instructions that outline how to install the Sync Framework and to configure it correctly before moving on to a detailed treatment of how m the Sync Framework operates and the providers that are available. Finally, the book provides a comprehensive explanation of how to create and customize purpose-built synchronization providers. e Joydip Kanjilal We’re confident that after you have read this book you will have a firm grasp of w how the Sync Framework operates and that you will be ready to develop your own applications to take advantage of it. We hope you have as much fun reading this book as we did writing it! o Rituraj Singh and Joydip Kanjilal r k Companion eBook THE APRESS ROADMAP Pro C# 2008 and the .NET3.5 Platform Expert Service- Pro Sync Framework Oriented Architecture, See last page for details Pro ASP.NET 3.5 Second Edition on $10 eBook version in C# 2008 Rituraj Singh Joydip Kanjilal SOURCE CODE ONLINE K and www.apress.com ISBN 978-1-4302-1005-4 anSi 54999 jn ilg a h US $49.99 l, Shelve in Microsoft/.NET User level: 9 781430 210054 Intermediate–Advanced www.it-ebooks.info this print for content only—size & color not accurate spine = 0.82" 352 page count www.it-ebooks.info Kanjilal_1005-4FRONT.fm Page i Thursday, October 30, 2008 8:16 AM Pro Sync Framework ■ ■ ■ Rituraj Singh and Joydip Kanjilal www.it-ebooks.info Kanjilal_1005-4FRONT.fm Page ii Thursday, October 30, 2008 8:16 AM Pro Sync Framework Copyright © 2009 by Rituraj Singh and Joydip Kanjilal 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-1005-4 ISBN-13 (electronic): 978-1-4302-1006-1 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Ewan Buckingham Technical Reviewer: Todd Meister Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Beth Christmas Copy Editor: Nancy Sixsmith Associate Production Director: Kari Brooks-Copony Production Editor: Ellie Fountain Compositor: Susan Glinert Proofreader: Nancy Bell Indexer: Brenda Miller Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http:// 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 http://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 http://www.apress.com. www.it-ebooks.info Kanjilal_1005-4FRONT.fm Page iii Thursday, October 30, 2008 8:16 AM I would like to dedicate this book to two special ladies in my life, my mom (Radhika Singh) and my wife (Priya Singh). Thanks for all your support and patience over the years and the years to come. Writing a book requires lot of time and thanks for allowing me to steal a large part of your time to write my first book. Thank you for all your sacrifices. —Rituraj Singh Dedicated to my parents and family. —Joydip Kanjilal www.it-ebooks.info Kanjilal_1005-4FRONT.fm Page iv Thursday, October 30, 2008 8:16 AM www.it-ebooks.info Kanjilal_1005-4FRONT.fm Page v Thursday, October 30, 2008 8:16 AM Contents at a Glance Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi ■ CHAPTER 1 Introduction to the Microsoft Sync Framework . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 Microsoft Sync Framework Metadata . . . . . . . . . . . . . . . . . . . . . . . . . 19 ■ CHAPTER 3 Working with Synchronization Providers: Part 1 . . . . . . . . . . . . . . . 33 ■ CHAPTER 4 Working with Synchronization Providers: Part 2 . . . . . . . . . . . . . . . 91 ■ CHAPTER 5 Working with File Synchronization Providers . . . . . . . . . . . . . . . . . 119 ■ CHAPTER 6 Microsoft Sync Framework Metadata Storage Service . . . . . . . . 141 ■ CHAPTER 7 Working with RSS Sync Feed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 ■ CHAPTER 8 Sync Services for ADO.NET 2.0: Offline Scenarios . . . . . . . . . . . . . 191 ■ CHAPTER 9 Synchronization Services for ADO.NET 2.0: Peer-to-Peer Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 ■ APPENDIX Sync Framework Library Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 295 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 v www.it-ebooks.info Kanjilal_1005-4FRONT.fm Page vi Thursday, October 30, 2008 8:16 AM www.it-ebooks.info Kanjilal_1005-4FRONT.fm Page vii Thursday, October 30, 2008 8:16 AM CCoonntteennttss Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi ■ CHAPTER 1 Introduction to the Microsoft Sync Framework . . . . . . . . . . . 1 Benefits of Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Life Before Sync Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Why the Microsoft Sync Framework? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Installing Microsoft Sync Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Core Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Synchronization Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Sync Framework Participants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Synchronization Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Synchronizing Two Replicas Using Built-in Providers . . . . . . . . . . . . . . . 11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 ■ CHAPTER 2 Microsoft Sync Framework Metadata . . . . . . . . . . . . . . . . . . . . 19 What Is Sync Metadata? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Metadata Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Components of Sync Metadata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Example of Sync Metadata Use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Sync Metadata Requirement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 ■ CHAPTER 3 Working with Synchronization Providers: Part 1 . . . . . . . . 33 What Is a Sync Provider? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Synchronizing Two Replicas Using Built-in Providers . . . . . . . . . . . . . . . 35 Creating a Synchronization Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 vii www.it-ebooks.info Kanjilal_1005-4FRONT.fm Page viii Thursday, October 30, 2008 8:16 AM viii ■ CONTENTS Provider Writer Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Change Tracking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Change Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Conflict Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Conflict Detection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Resolving Conflicts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Saving the Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Sample Synchronization Provider for Synchronizing SQL Server Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 MySyncProvider.cs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Overriding Provider Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Creating New Customer Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Synchronizing Replicas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Running the Sample Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Synchronizing New Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Detecting and Handling Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Synchronize Deleted Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Finishing Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 ■ CHAPTER 4 Working with Synchronization Providers: Part 2 . . . . . . . . 91 Change Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Creating Sync Providers with Change Units. . . . . . . . . . . . . . . . . . . . 94 Creating Providers That Use Asynchronous Change Tracking . . . . . . . . . 94 MySyncProvider.cs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Changing the Customer.cs Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Changes in the Synchronizing Application. . . . . . . . . . . . . . . . . . . . 104 Creating a Generic Sync Provider. . . . . . . . . . . . . . . . . . . . . . . . . . . 107 BaseSyncProvider. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Extending the Generic Sync Provider Class. . . . . . . . . . . . . . . . . . . 109 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 ■ CHAPTER 5 Working with File Synchronization Providers . . . . . . . . . . . 119 Sync Framework Core Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 File Sync Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 File Sync Provider Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 www.it-ebooks.info