Table Of Contentwww.it-ebooks.info
WCF 4.0 Multi-tier Services
Development with LINQ to
Entities
Build SOA applications on the Microsoft platform with
this hands-on guide updated for VS2010
Mike Liu
professional expertise distilled
P U B L I S H I N G
BIRMINGHAM - MUMBAI
www.it-ebooks.info
WCF 4.0 Multi-tier Services Development with LINQ
to Entities
Copyright © 2010 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of
the information presented. However, the information contained in this book is sold
without warranty, either express or implied. Neither the author, Packt Publishing,
nor its dealers or distributors will be held liable for any damages caused or alleged to
be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2010
Production Reference: 1020610
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-84968-114-8
www.packtpub.com
Cover Image by Sandeep Babu (sandyjb@gmail.com)
www.it-ebooks.info
Credits
Author Editorial Team Leader
Mike Liu Akshara Aware
Project Team Leader
Reviewers
Lata Basantani
Yingwei Yang
Jeff Sanders Project Coordinator
Sneha Harkut
Acquisition Editor
David Barnes Proofreader
Kevin McGowan
Development Editor
Rakesh Shejwal Graphics
Geetanjali Sawant
Technical Editor
Akash Johari Production Coordinator
Adline Swetha Jesuthas
Copy Editor
Lakshmi Menon Cover Work
Adline Swetha Jesuthas
Indexer
Rekha Nair
www.it-ebooks.info
About the Author
Mike Liu studied Mathematics and Applied Software Engineering at Nanjing
University between 1984 and 1988. After graduating with a bachelor's degree, he
worked as a Programmer/Senior Software Engineer/Architect on Unix and DOS
using C/C++, Dbase, and Oracle. In 1995 he moved to New Zealand and studied
Business Computing at Auckland University of Technology. During the five-year
stay in New Zealand, he worked as a Senior Software Engineer on Unix and Windows
using C/C++, Java, FoxPro, Informix, Oracle, and SQL Server. He moved to the United
States in 2000 and since then has been working as a Web Developer/Senior Software
Engineer/Principal Software Engineer of various operating systems using various
programming languages and database technologies. He studied Software Engineering
at Brandeis University and graduated in 2005 with a master's degree.
Mike became a Sun Certified Java Programmer (SCJP) in 2000, a Microsoft Certified
Solution Developer (MCSD) for Visual Studio 6.0 in 2001, and an MCSD for .NET in
2004. He started using C# for production development back in the year 2001 when
C# was still in beta stage and he is now integrating a Business Process Management
application with a WCF services backend system.
Mike had his first book—MITT: Multi-user Integrated Table-processing Tool Under
Unix—published in 1993, and had his second book—Advanced C# Programming—
published in 2003. The previous version of this book—WCF Multi-tier Services
Development with LINQ—was published in 2008, with LINQ to SQL being used
as the ORM in the data access layer of the WCF services.
Many thanks to the editors and technical reviewers at Packt
Publishing. Without their help this book wouldn't be of such high
quality. And thanks to my wife, Julia Guo, and my two sons, Kevin
and James Liu, for their consideration and sacrifices while I was
working on this book.
www.it-ebooks.info
About the Reviewers
Yingwei Yang joined Microsoft in 2008. Before that he worked for ITG and
Redcats USA. Yingwei enjoys working with .NET technology and is a big fan of
Service Oriented Architecture, Silverlight, and High Performance Computing. He
always thinks that Web Services/Software as a service brings endless opportunities
and possibilities.
Yingwei Yang also helped review the book, WCF Multi-tier Services Development with
LINQ, and he is reviewing Silverlight User Interface Cookbook.
Jeff Sanders is a published author and an accomplished technologist. He is
currently employed with Avanade Federal Services as a Group Manager/Senior
Architect and as the Manager of the Federal Office of Learning and Development.
Jeff has more than 17 years of professional experience in the field of IT and
strategic business consulting, in roles ranging from leading sales to delivery
efforts. He regularly contributes to certification development with Microsoft and
speaks publicly on Microsoft enterprise technologies. With his roots in Software
Development, Jeff's areas of expertise include operational intelligence, collaboration
and content management solutions, distributed component-based application
architectures, object-oriented analysis and design, and enterprise integration patterns
and designs.
Jeff is also the CTO of DynamicShift, a client-focused organization specializing in
Microsoft technologies, specifically Business Activity Monitoring, BizTalk Server,
SharePoint Server, StreamInsight, Windows Azure, AppFabric, Commerce Server,
and .NET. He is a Microsoft Certified Trainer, and leads DynamicShift in both
training and consulting efforts.
He may be reached at jeff.sanders@dynamicshift.com.
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface 1
Chapter 1: Introducing Web Services and Windows
Communication Foundation 7
What is SOA? 7
Web services 8
What is a web service? 8
Web service WSDL 10
Web service proxy 10
SOAP 11
Web services: standards and specifications 11
WS-I Profiles 12
WS-Addressing 12
WS-Security 12
WS-ReliableMessaging 12
WS-Coordination and WS-Transaction 13
WCF: Windows Communication Foundation 13
What is WCF? 14
Why is WCF used for SOA? 14
WCF architecture 16
Basic WCF concepts—WCF ABCs 17
Address 17
Binding 17
Contract 18
Service contract 18
Operation contract 19
Message contract 19
Data contract 20
Fault contract 21
Endpoint 22
www.it-ebooks.info
Table of Contents
Behavior 23
Hosting 23
Self hosting 23
Windows services hosting 23
IIS hosting 23
Windows Activation Services hosting 24
Channels 24
Metadata 25
WCF production and development environments 25
Summary 26
Chapter 2: Implementing a Basic HelloWorld WCF Service 29
Creating the HelloWorld solution and project 29
Creating the HelloWorldService service contract interface 37
Implementing the HelloWorldService service contract 39
Hosting the WCF service in ASP.NET Development Server 41
Creating the host application 41
Testing the host application 44
ASP.NET Development Server 45
Adding an SVC file to the host application 46
Modifying the web.config file 47
Starting the host application 48
Creating a client to consume the WCF service 49
Creating the client application project 50
Generating the proxy and configuration files 50
Customizing the client application 52
Running the client application 52
Setting the service application to AutoStart 53
Summary 54
Chapter 3: Hosting and Debugging the HelloWorld
WCF Service 55
Hosting the HelloWorld WCF service 55
Hosting the service in a managed application 56
Hosting the service in a console application 56
Consuming the service hosted in a console application 61
Hosting the service in a Windows service 61
Hosting the service in Internet Information Server 62
Preparing the folders and files 62
Turn on Internet Information Services 64
Creating the IIS application 64
Starting the WCF service in IIS 65
Testing the WCF service hosted in IIS 66
Other WCF service hosting options 66
[ ii ]
www.it-ebooks.info
Table of Contents
Debugging the HelloWorld WCF service 67
Debugging from the client application 67
Starting the debugging process 67
Debugging on the client application 69
Attaching to ASP.NET Development Server 70
Stepping into the WCF service 72
Debugging only the WCF service 73
Starting the WCF Service in debugging mode 74
Starting the client application in non-debugging mode 76
Starting the WCF service and client applications in debugging mode 77
Attaching to a WCF service process 77
Running the WCF service and client applications in non-debugging mode 77
Debugging the WCF service hosted in IIS 78
Just-In-Time debugger 80
Summary 82
Chapter 4: Implementing a WCF Service in the Real World 83
Why layer a service? 84
Creating a new solution and project using WCF templates 85
Using the C# WCF service library template 85
Using the C# WCF service application template 87
Creating the service interface layer 89
Creating the service interfaces 89
Creating the data contracts 91
Implementing the service contracts 92
Modifying the app.config file 94
Testing the service using WCF Test Client 96
Testing the service using our own client 101
Adding a business logic layer 107
Adding the product entity project 107
Adding the business logic project 108
Calling the business logic layer from the service interface layer 112
Testing the WCF service with a business logic layer 116
Summary 118
Chapter 5: Adding Database Support and Exception Handling to the
RealNorthwind WCF Service 119
Adding a data access layer 120
Creating the data access layer project 120
Calling the data access layer from the business logic layer 122
Preparing the database 125
Adding the connection string to the configuration file 126
Querying the database (GetProduct) 128
Testing the GetProduct method 130
[ iii ]
www.it-ebooks.info