Table Of ContentVisit us today at
microsoftpressstore.com
• Hundreds of titles available – Books, eBooks, and online
resources from industry experts
• Free U.S. shipping
• eBooks in multiple formats – Read on your computer,
tablet, mobile device, or e-reader
• Print & eBook Best Value Packs
• eBook Deal of the Week – Save up to 60% on featured titles
• Newsletter and special offers – Be the first to
hear about new releases, specials, and more
• Register your book – Get additional benefits
Hear about
it first.
Get the latest news from Microsoft Press sent to
your inbox.
• New and upcoming books
• Special offers
• Free eBooks
• How-to articles
Sign up today at MicrosoftPressStore.com/Newsletters
Wait, there’s more...
Find more great content and resources in the
Microsoft Press Guided Tours app.
The Microsoft Press Guided Tours app provides
insightful tours by Microsoft Press authors of new and
evolving Microsoft technologies.
• Share text, code, illustrations, videos, and links with
peers and friends
• Create and manage highlights and notes
• View resources and download code samples
• Tag resources as favorites or to read later
• Watch explanatory videos
• Copy complete code listings and scripts
DDoowwnnllooaadd ffrroomm
WWiinnddoowwss SSttoorree
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2014 Microsoft Corporation
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any
means without the written permission of the publisher.
ISBN: 978-0-7356-9565-8
Microsoft Press books are available through booksellers and distributors worldwide. If you need support
related to this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what
you think of this book at http://aka.ms/tellpress.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights
under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval
system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or
otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/IntellectualProperty/
Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of
their respective owners.
The example companies, organizations, products, domain names, email addresses, logos, people, places, and
events depicted herein are fictitious. No association with any real company, organization, product, domain
name, email address, logo, person, place, or event is intended or should be inferred.
This book expresses the authors’ views and opinions. The information contained in this book is provided without
any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or
distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this
book.
Acquisitions, Developmental, and Project Editor: Devon Musgrave
Editorial Production: Flyingspress and Rob Nance
Copyeditor: John Pierce
Cover: Twist Creative • Seattle and Joel Panchot
1
Table of Contents
Introduction ............................................................................................................................................................................................ 9
Who should read this book ......................................................................................................................................................... 9
Assumptions .................................................................................................................................................................................. 9
This book might not be for you if… ......................................................................................................................................... 9
Organization of this book ..........................................................................................................................................................10
The Fix It sample application ....................................................................................................................................................12
Azure Websites ...............................................................................................................................................................................15
System requirements ....................................................................................................................................................................17
Downloads: Code samples .........................................................................................................................................................17
Acknowledgments .........................................................................................................................................................................18
Errata, updates, & book support ..............................................................................................................................................19
Free ebooks from Microsoft Press...........................................................................................................................................19
We want to hear from you .........................................................................................................................................................19
Stay in touch ....................................................................................................................................................................................19
Chapter 1 Automate everything ...............................................................................................................................................20
DevOps workflow ...........................................................................................................................................................................20
Azure management scripts ........................................................................................................................................................21
Environment creation script ......................................................................................................................................................21
Run the script ..............................................................................................................................................................................21
A look at the scripts .................................................................................................................................................................25
Parameters in the main script ..............................................................................................................................................25
Create the website ....................................................................................................................................................................25
Create the storage account ...................................................................................................................................................26
Create the databases ...............................................................................................................................................................26
Store app settings and connection strings .....................................................................................................................28
Preparing for deployment .....................................................................................................................................................29
2
Troubleshooting and error handling ................................................................................................................................ 29
Deployment script ......................................................................................................................................................................... 30
Summary ........................................................................................................................................................................................... 31
Resources .......................................................................................................................................................................................... 32
Chapter 2 Source control ............................................................................................................................................................ 33
Treat automation scripts as source code ............................................................................................................................. 33
Don’t check in secrets .................................................................................................................................................................. 34
Structure source branches to facilitate DevOps workflow ............................................................................................ 34
Add scripts to source control in Visual Studio ................................................................................................................... 36
Store sensitive data in Azure ..................................................................................................................................................... 38
Use Git in Visual Studio and Visual Studio Online ........................................................................................................... 40
Summary ........................................................................................................................................................................................... 46
Resources .......................................................................................................................................................................................... 46
Chapter 3 Continuous integration and continuous delivery ........................................................................................ 48
Continuous integration and continuous delivery workflow ......................................................................................... 48
How the cloud enables cost-effective CI and CD ............................................................................................................. 49
Visual Studio Online ..................................................................................................................................................................... 49
Summary ........................................................................................................................................................................................... 50
Resources .......................................................................................................................................................................................... 50
Chapter 4 Web development best practices ....................................................................................................................... 52
Stateless web tier behind a smart load balancer .............................................................................................................. 52
Avoid session state ........................................................................................................................................................................ 56
Use a CDN to cache static file assets ..................................................................................................................................... 56
Use .NET 4.5’s async support to avoid blocking calls ..................................................................................................... 57
Async support in ASP.NET 4.5.............................................................................................................................................. 57
Async support in Entity Framework 6 ............................................................................................................................... 58
Summary ........................................................................................................................................................................................... 59
Resources .......................................................................................................................................................................................... 60
Chapter 5 Single sign-on ............................................................................................................................................................. 62
3
Introduction to Azure Active Directory .................................................................................................................................62
Set up an Azure AD tenant ........................................................................................................................................................65
Create an ASP.NET app that uses Azure AD for single sign-on ..................................................................................75
Summary ...........................................................................................................................................................................................79
Resources ..........................................................................................................................................................................................79
Chapter 6 Data storage options ................................................................................................................................................81
Data storage options on Azure ................................................................................................................................................81
Hadoop and MapReduce ...........................................................................................................................................................83
Platform as a Service (PaaS) versus Infrastructure as a Service (IaaS) .......................................................................86
Choosing a data storage option ..............................................................................................................................................89
Demo—Using SQL Database in Azure ..................................................................................................................................91
Entity Framework versus direct database access using ADO.NET ..............................................................................96
SQL databases and the Entity Framework in the Fix It app ..........................................................................................97
Choosing SQL Database (PaaS) versus SQL Server in a VM (IaaS) in Azure ...........................................................99
Summary ........................................................................................................................................................................................ 101
Resources ....................................................................................................................................................................................... 101
Chapter 7 Data partitioning strategies ................................................................................................................................ 104
The three Vs of data storage .................................................................................................................................................. 104
Vertical partitioning ................................................................................................................................................................... 105
Horizontal partitioning (sharding) ....................................................................................................................................... 106
Hybrid partitioning..................................................................................................................................................................... 107
Partitioning a production application ................................................................................................................................ 108
Summary ........................................................................................................................................................................................ 108
Resources ....................................................................................................................................................................................... 109
Chapter 8 Unstructured blob storage .................................................................................................................................. 110
What is Blob storage? ............................................................................................................................................................... 110
Creating a storage account..................................................................................................................................................... 111
Using Blob storage in the Fix It app .................................................................................................................................... 112
Set up the Blob container ................................................................................................................................................... 112
4
Store the uploaded photo in Blob storage ................................................................................................................. 114
Display the uploaded file .................................................................................................................................................... 116
Summary ........................................................................................................................................................................................ 118
Resources ....................................................................................................................................................................................... 118
Chapter 9 Design to survive failures .................................................................................................................................... 120
Types of failures .......................................................................................................................................................................... 120
Failure scope ................................................................................................................................................................................. 120
Machine failures ..................................................................................................................................................................... 121
Service failures ........................................................................................................................................................................ 121
Region failures ........................................................................................................................................................................ 121
SLAs .................................................................................................................................................................................................. 122
Composite SLAs ...................................................................................................................................................................... 123
Cloud SLAs compared with enterprise downtime experience ............................................................................. 123
Not all cloud services have SLAs ...................................................................................................................................... 124
Not all downtime counts toward SLAs .......................................................................................................................... 124
Summary ........................................................................................................................................................................................ 124
Resources ....................................................................................................................................................................................... 125
Chapter 10 Monitoring and telemetry ................................................................................................................................ 127
Buy or rent a telemetry solution .......................................................................................................................................... 127
Log for insight .............................................................................................................................................................................. 139
Log in production .................................................................................................................................................................. 140
Differentiate logs that inform from logs that require action ............................................................................... 140
Configure logging levels at run time ............................................................................................................................. 141
Log exceptions ........................................................................................................................................................................ 141
Log calls to services ............................................................................................................................................................... 142
Use an ILogger interface ..................................................................................................................................................... 142
Semantic logging ................................................................................................................................................................... 142
Logging in the Fix It app ......................................................................................................................................................... 143
The ILogger interface ........................................................................................................................................................... 143
5
The Logger implementation of the ILogger interface ............................................................................................ 143
Calling the ILogger methods ............................................................................................................................................. 144
Dependency injection in the Fix It app .............................................................................................................................. 146
Built-in logging support in Azure ........................................................................................................................................ 147
Summary ........................................................................................................................................................................................ 150
Resources ....................................................................................................................................................................................... 150
Chapter 11 Transient fault handling .................................................................................................................................... 153
Causes of transient failures ..................................................................................................................................................... 153
Use smart retry/back-off logic to mitigate the effect of transient failures ......................................................... 153
Circuit breakers ............................................................................................................................................................................ 155
Summary ........................................................................................................................................................................................ 156
Resources ....................................................................................................................................................................................... 156
Chapter 12 Distributed caching ............................................................................................................................................. 158
What is distributed caching? .................................................................................................................................................. 158
When to use distributed caching ......................................................................................................................................... 159
Popular cache population strategies .................................................................................................................................. 159
Sample cache-aside code for the Fix It app ..................................................................................................................... 160
Azure caching services .............................................................................................................................................................. 161
ASP.NET session state using a cache provider ................................................................................................................ 161
Summary ........................................................................................................................................................................................ 161
Resources ....................................................................................................................................................................................... 162
Chapter 13 Queue-centric work pattern ............................................................................................................................ 163
Reduced latency .......................................................................................................................................................................... 163
Increased reliability .................................................................................................................................................................... 164
Rate leveling and independent scaling .............................................................................................................................. 166
Adding queues to the Fix It application ............................................................................................................................ 167
Creating queue messages ....................................................................................................................................................... 167
Processing queue messages ................................................................................................................................................... 169
6
Description:Microsoft Press, 2014. — 198 p.Book: Best practices for DevOps, data storage, high availability, and more walks you through a patterns-based approach to building real-world cloud solutions. The patterns apply to the development process as well as to architecture and coding practices.DescriptionTop