84
Object Innovations Course 4153 Student Guide Revision 1.1 Introduction to WCF Using C#

Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

Object Innovations Course 4153

Student Guide Revision 1.1

Introduction to WCF Using C#

Page 2: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

Rev. 1.1 Copyright ©2008 Object Innovations Enterprises, LLC ii All Rights Reserved

Introduction to WCF Using C# Rev. 1.1 This custom course consists of the first chapter of a three-day Object Innovations course of the same name. Student Guide Information in this document is subject to change without notice. Companies, names and data used in examples herein are fictitious unless otherwise noted. No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of Object Innovations. Product and company names mentioned herein are the trademarks or registered trademarks of their respective owners. Author: Robert J. Oberg Copyright ©2008 Object Innovations Enterprises, LLC All rights reserved. Object Innovations 877-558-7246 www.objectinnovations.com Published in the United States of America.

Page 3: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

Rev. 1.1 Copyright ©2008 Object Innovations Enterprises, LLC iii All Rights Reserved

Table of Contents (Overview) Chapter 1 Introduction to WCF Chapter 2 Addresses and Bindings Chapter 3 Service Contracts Chapter 4 Instance Management Chapter 5 Data Contracts Chapter 6 More about Service Contracts Chapter 7 Handling Errors Appendix A Learning Resources Appendix B Configuring IIS for ASP.NET 2.0

Page 4: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

Rev. 1.1 Copyright ©2008 Object Innovations Enterprises, LLC iv All Rights Reserved

Directory Structure

• Install the course software by running the self-extractor Install_WcfCs_11.exe.

• The course software installs to the root directory c:\OIC\WcfCs.

− Example programs for each chapter are in named subdirectories of chapter directories Chap01, Chap02 and so on.

− The Labs directory contains one subdirectory for each lab, named after the lab number. Starter code is frequently supplied, and answers are provided in the chapter directories.

− The Demos directory is provided for performing in-class demonstrations led by the instructor.

Page 5: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

Rev. 1.1 Copyright ©2008 Object Innovations Enterprises, LLC v All Rights Reserved

Table of Contents (Detailed)

Chapter 1: Introduction to WCF ........................................................................................ 1 What Is WCF?..................................................................................................................... 3 WCF Services ..................................................................................................................... 4 Service Orientation ............................................................................................................. 6 WCF and Web Services ...................................................................................................... 7 WCF = ABC ....................................................................................................................... 8 Address, Binding, Contract................................................................................................. 9 Example – Hello WCF...................................................................................................... 10 Hosting Services ............................................................................................................... 11 Internet Information Services ........................................................................................... 12 Internet Services Manager ................................................................................................ 13 Virtual Directory............................................................................................................... 14 Anonymous Access........................................................................................................... 16 Configuring .svc Extension............................................................................................... 17 Demo – Hello WCF .......................................................................................................... 20 A Service Contract............................................................................................................ 22 A Website for the Service ................................................................................................. 23 WCF Service Template..................................................................................................... 24 Service Configuration ....................................................................................................... 25 Configuration Details........................................................................................................ 26 Referencing the Class Library .......................................................................................... 27 Examining the Service in the Browser ............................................................................. 28 Metadata Publishing Support............................................................................................ 29 Service in the Browser with Metadata.............................................................................. 30 WCF Clients...................................................................................................................... 31 Creating WCF Clients....................................................................................................... 32 Demo – A Client for Hello WCF...................................................................................... 33 Lab 1 ................................................................................................................................. 37 WCF Architecture............................................................................................................. 38 WCF Architecture............................................................................................................. 39 ServiceHost and ChannelFactory...................................................................................... 40 Service Contexts and Instances......................................................................................... 41 Summary ........................................................................................................................... 42

Chapter 2: Addresses and Bindings .................................................................................. 47 Addresses .......................................................................................................................... 49 Transports ......................................................................................................................... 50 Bindings ............................................................................................................................ 51 Message Exchange Patterns (MEPs) ................................................................................ 52 Security ............................................................................................................................. 53 Choosing a Binding .......................................................................................................... 54

Page 6: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

Rev. 1.1 Copyright ©2008 Object Innovations Enterprises, LLC vi All Rights Reserved

HTTP Bindings ................................................................................................................. 55 TCP and Named Pipe Bindings ........................................................................................ 56 MSMQ Bindings............................................................................................................... 57 Working with Endpoints ................................................................................................... 58 Importance of BasicHttpBinding ...................................................................................... 59 Demo – ASMX Web Service Client ................................................................................. 60 Lab 2A .............................................................................................................................. 65 WCF Service Hosting ....................................................................................................... 66 ServiceHost ....................................................................................................................... 67 Demo – Self-Hosting ........................................................................................................ 68 Client Program without Proxy Class ................................................................................ 70 Using a Channel Factory................................................................................................... 71 Service Endpoints ............................................................................................................. 72 Multiple Endpoints ........................................................................................................... 73 Multiple Protocol Example............................................................................................... 75 Configuration Files ........................................................................................................... 76 Simplified Host Code........................................................................................................ 77 Configuring Metadata Exchange ...................................................................................... 78 Lab 2B............................................................................................................................... 79 Summary ........................................................................................................................... 80

Chapter 3: Service Contracts............................................................................................. 93 Service Contracts at Class Level ...................................................................................... 95 Service Contracts at Interface Level................................................................................. 96 Benefits of Interface Level Definition .............................................................................. 97 A Service with Multiple Contracts ................................................................................... 98 ServiceContractAttribute .................................................................................................. 99 ServiceContractAttribute Example ................................................................................. 100 Viewing WSDL Files...................................................................................................... 101 Contract Inheritance........................................................................................................ 102 Operation Overloading ................................................................................................... 104 Enabling Operation Overloading .................................................................................... 105 Operation Overloading Client......................................................................................... 106 Lab 3 ............................................................................................................................... 107 Summary ......................................................................................................................... 108

Chapter 4: Instance Management ................................................................................... 117 Behaviors ........................................................................................................................ 119 WCF Behaviors............................................................................................................... 120 Configuring Behaviors.................................................................................................... 121 Configuring Behaviors in Code ...................................................................................... 122 WCF Instancing Models ................................................................................................. 123 Per-Call Instancing ......................................................................................................... 124 Per-Session Instancing.................................................................................................... 125 Sessions and Threading .................................................................................................. 126 Singleton Instancing ....................................................................................................... 127

Page 7: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

Rev. 1.1 Copyright ©2008 Object Innovations Enterprises, LLC vii All Rights Reserved

Which Model to Use? ..................................................................................................... 128 PerSession Example........................................................................................................ 129 PerCall Example ............................................................................................................. 132 Singleton Example .......................................................................................................... 133 Lab 4 ............................................................................................................................... 134 Summary ......................................................................................................................... 135

Chapter 5: Data Contracts............................................................................................... 139 Data Contracts................................................................................................................. 141 Data Contract Example ................................................................................................... 142 Data Contract Demonstration ......................................................................................... 143 XSD for Data Contract.................................................................................................... 148 Client Demo.................................................................................................................... 150 Arrays.............................................................................................................................. 152 Array in XML Schema.................................................................................................... 153 Array in Proxy ................................................................................................................ 154 Generic Collections......................................................................................................... 155 Generic Collection in XML Schema .............................................................................. 156 Generic Collection in Proxy ........................................................................................... 157 Lab 5A ............................................................................................................................ 158 Enumerations in Data Contracts ..................................................................................... 159 Enumeration Data Contract Example ............................................................................. 160 Employee Client Program............................................................................................... 161 Saving and Restoring ...................................................................................................... 162 Serialization in WCF ...................................................................................................... 163 Serialization in .NET ...................................................................................................... 164 Serialization Example ..................................................................................................... 165 SOAP Serialization ......................................................................................................... 166 JSON Serialization.......................................................................................................... 167 DataContract Serialization.............................................................................................. 168 Using XmlSerializer ....................................................................................................... 170 XmlSerializer Example................................................................................................... 172 Restoring Data ................................................................................................................ 174 Versioning....................................................................................................................... 176 New and Missing Members ............................................................................................ 177 Versioning Demonstration.............................................................................................. 178 New Client of Old Service.............................................................................................. 180 Round Trip ...................................................................................................................... 181 Required Members.......................................................................................................... 182 OnDeserializing Event .................................................................................................... 183 Lab 5B............................................................................................................................. 184 Summary ......................................................................................................................... 185

Chapter 6: More about Service Contracts ..................................................................... 197 Versioning Service Contracts ......................................................................................... 199 Versioning Example........................................................................................................ 200

Page 8: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

Rev. 1.1 Copyright ©2008 Object Innovations Enterprises, LLC viii All Rights Reserved

Version 1 Service............................................................................................................ 201 Version 2 Service............................................................................................................ 202 New Operations .............................................................................................................. 203 Version 3 Service............................................................................................................ 204 Version 1 Client / Version 3 Service .............................................................................. 205 Message Exchange Patterns............................................................................................ 206 Request-Reply................................................................................................................. 207 Oneway ........................................................................................................................... 208 Oneway Example ............................................................................................................ 209 Duplex............................................................................................................................. 210 Callbacks......................................................................................................................... 211 Invoking a Callback ........................................................................................................ 212 Callback on the Client..................................................................................................... 213 Asynchronous Proxies .................................................................................................... 214 Threading Considerations ............................................................................................... 215 Lab 6 ............................................................................................................................... 217 Summary ......................................................................................................................... 218

Chapter 7: Handling Errors ............................................................................................ 227 Errors in Distributed Systems ......................................................................................... 229 Errors in .NET and WCF ................................................................................................ 230 Demo of WCF Error Behavior........................................................................................ 231 Service Library Code ...................................................................................................... 233 Client Code ..................................................................................................................... 234 Client Exception Handling.............................................................................................. 235 Exception Handling Demo.............................................................................................. 236 Fault Exceptions ............................................................................................................. 238 Faults............................................................................................................................... 239 Exception Details in Faults ............................................................................................. 240 Exception Details Demo ................................................................................................. 241 Exceptions Dialog........................................................................................................... 243 Fault Contracts ................................................................................................................ 244 Fault Contract Example .................................................................................................. 245 Fault Contract Example – Client .................................................................................... 246 Custom Faults ................................................................................................................. 247 Faulted Channels............................................................................................................. 248 Lab 7 ............................................................................................................................... 249 Summary ......................................................................................................................... 250

Appendix A: Learning Resources.................................................................................... 257

Appendix B: Configuring IIS for ASP.NET 2.0............................................................. 261 ASP.NET Versions Side-by-Side ................................................................................... 262 Configuring for ASP.NET 2.0 ........................................................................................ 263 Installing ASP.NET ........................................................................................................ 266

Page 9: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 1 All Rights Reserved

Chapter 1

Introduction to WCF

Page 10: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 2 All Rights Reserved

Introduction to WCF

Objectives

After completing this unit you will be able to:

• Explain how WCF unites and extends existing distribution technologies.

• Understand the importance of address, binding and contract to WCF services.

• Create a simple WCF service hosted in IIS.

• Implement a client of a WCF service.

• Understand the WCF architecture and runtime.

Page 11: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 3 All Rights Reserved

What Is WCF?

• Windows Communication Foundation (WCF) is a new service-oriented programming framework for creating distributed applications.

− It was previously known as ‘Indigo’ and is part of .NET 3.0.

• WCF is designed to provide one mechanism for building connected applications:

− Within app domains

− Across app domains

− Across machines

• WCF builds upon and extends existing ways of building distributed applications:

− ASMX Web services, .NET Remoting, COM, MSMQ.

• All these do the same basic job (connecting elements in distributed applications) but they are very different at the programming level, with complex APIs and interactions.

− WCF provides one model for programming distributed applications. Developers only need to learn one API.

• WCF leverages existing mechanisms.

− It uses TCP, HTTP and MSMQ for transport.

Page 12: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 4 All Rights Reserved

WCF Services

• When using WCF, you create and consume services.

− A service comprises a set of related operations, which the programmer sees as method calls.

• Services are described by metadata, which clients can use to determine what operations are available, and how the service can be contacted.

− Metadata for WCF services is similar to the WSDL used by web services.

• Clients and services exchange messages.

− A client (which can be another service) communicates with a WCF service by sending and receiving messages. WCF uses SOAP as its messaging mechanism, and SOAP messages can be sent using a number of transports.

− Using SOAP does not imply that WCF communication is inefficient; efficient binary encodings are employed whenever possible.

• WCF supports several transports out of the box.

− TCP, HTTP, HTTPS and MSMQ.

− Custom transports can be added.

Page 13: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 5 All Rights Reserved

WCF Services

• WCF supports the WS-* family of Web service protocols.

− The WS-* family of protocols have been developed by various bodies (including OASIS and W3C) to provide features such as security, transactions and reliable messaging to web services.

• WCF is very good at interop.

− Support for a wide range of transports, encodings and the WS-* protocols means that WCF services can interoperate with a wide range of platforms and technologies, including J2EE and web services using WS-* protocols.

• WCF provides a foundation for service orientation.

− WCF helps developers write distributed applications in which loosely coupled services are called by clients and one another.

Page 14: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 6 All Rights Reserved

Service Orientation

• Service orientation is characterized by four concepts.

• Boundaries are explicit

− The boundary between client and service is explicit and highly visible, because calls are made via SOAP messages. There is no pretending – as there is in DCOM and Java’s RMI – that you are simply playing with a remote object, and can thus ignore the cost of remoting.

• Services are autonomous

− Services are independent entities that each have their own life cycle; they may have been developed completely independently of one another. There is no run-time making sure that services work well together, and so services must be prepared to handle failure situations of all sorts.

• Share schemas and contracts, not classes

− Services are not limited to implementation in OO languages, and so service details cannot be provided in terms of classes. Services should share schemas and contracts, and these are typically described in XML.

• Use policy-based service compatibility

− Services should publish their requirements (i.e. Requiring message signing or HTTPS connections) in a machine readable form. This can be used at runtime to ensure compatibility between service and client.

Page 15: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 7 All Rights Reserved

WCF and Web Services

• Even though it uses SOAP messaging, WCF is more than simply another way of writing Web services.

− WCF can be used to write traditional Web services, as well as more sophisticated services that can use the WS-* protocols. But the design of WCF means that it provides a far more general solution to distribution than Web services.

• WCF can use several transports.

− Web services tend to use HTTP or HTTPS, while WCF is configured to use TCP, HTTP, HTTPS and MSMQ. It is also possible to add new transports, should the need arise.

• WCF can work throughout the enterprise.

− WCF services can be hosted in-process, by a Windows Service or IIS. Message exchange will be optimized to use the most efficient method for exchanging data for a particular scenario.

• WCF is good at interop.

− WCF services can interoperate with a number of different platforms and technologies.

• WCF is highly customizable.

− It is possible to customize almost every part of WCF, adding or modifying transports, encodings and bindings, and plugging in new ‘behaviors’ that affect the way WCF services work.

Page 16: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 8 All Rights Reserved

WCF = ABC

Service

CBA

CBA

CBA

A

B

C

Client C B A

= address

= binding

= contract

Where?

How?

What?

CB A = an endpoint

Page 17: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 9 All Rights Reserved

Address, Binding, Contract

• An address defines where a service can be found.

− It will often be an HTTP address, although other addressing schemes are supported.

• A binding defines how a service can be contacted

− Via HTTP, TCP, MSMQ or some custom mechanism.

• A contract defines what a service can do.

− In terms of method calls, their arguments and return types.

• A combination of an address, a binding and a contract is called an endpoint.

− A service can expose more than one endpoint, and endpoint data can be made available to clients in the form of metadata.

Page 18: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 10 All Rights Reserved

Example – Hello WCF

• A contract defines a set of operations that a service supports.

− Define a contract as an interface decorated with the ServiceContract attribute.

− Decorate operations with the OperationContract attribute.

[ServiceContract] public interface IHello { [OperationContract] string SayHello(string name); }

• A service class implements the interface.

− And so it has to implement all the operations defined in the contract.

public class Hello : IHello { public string SayHello(string name) { return "Hello, " + name; } }

• Note that this code says nothing about how the client communicates with the service.

− It is only the 'C' of the service 'ABC'.

Page 19: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 11 All Rights Reserved

Hosting Services

• There are three ways to host a WCF service.

• Self-hosting in an EXE.

− Use any type of EXE: Console application, Windows application (Windows Forms or WPF), or Windows Service.

− Need to manage service lifecycle yourself.

• Hosting in IIS.

− IIS will manage the service lifecycle for you, starting the service when the first request comes in.

− You can only use HTTP and port 80.

− Configure the service using a .svc file.

• Hosting in Windows Activation Service (WAS)

− WAS is a new feature that is part of Vista.

− Similar advantages to hosting in IIS, but you can use other transports and ports as well.

− WAS also uses .svc files.

− It is the preferred way of hosting services on Vista.

Page 20: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 12 All Rights Reserved

Internet Information Services

• Our early examples use IIS hosting, so you will need to have Internet Information Services (IIS) installed on your system.

− IIS Version 5.x runs on Windows XP Professional, Version 6.x runs on Windows 2003, and version 7.0 runs on Vista.

• On Windows 2003 and Windows XP Professional, Internet Information Services is not installed by default.

− For Windows XP Professional, use the Control Panel and Add/Remove Windows Components.

− For Windows 2003 Installation details, see the instructions in MSDN. You can use either the Manage Your Server wizard or the Control Panel Add/Remove Windows Components.

• In this course we will illustrate the use of IIS 5.x on Windows XP.

• Appendix B discusses some IIS configuration issues, which are especially critical if you have more than one version of .NET installed on your computer.

Page 21: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 13 All Rights Reserved

Internet Services Manager

• The management tool for IIS is a Microsoft Management Console (MMC) “snap-in,” the Internet Services Manager, which you can find under Administrative Tools in the Control Panel.

− You can Start and Stop the Web server and perform other tasks by right-clicking on Default Web Site.

− Choosing Properties from the context menu will let you perform a number of configurations on the Web server.

• The default home directory for publishing Web files is \Inetpub\wwwroot on the drive where Windows is installed.

− You can change this home directory using Internet Services Manager.

Page 22: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 14 All Rights Reserved

Virtual Directory

• You can access Web pages stored at any location on your hard drive by creating a virtual directory.

− The easiest way to create one is from Windows Explorer. Right-click over the desired directory, choose Properties..., select the Web Sharing tab, click on the Add button, and enter the desired alias, which will be the name of the virtual directory.

• The figure on the next page illustrates creating an alias WcfCs, or virtual directory, for the folder \OIC\WcfCs.

− You should perform this operation now on your own system in order that you may follow along as the course examples are discussed.

Page 23: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 15 All Rights Reserved

Virtual Directory (Cont’d)

• Once a virtual directory has been created, you can access files in it by including the virtual directory in the path of the URL.

Page 24: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 16 All Rights Reserved

Anonymous Access

• After you have created the virtual directory, examine its properties in Internet Information Services.

− Right-click on WcfCs and select Properties.

− Click on the Directory Security tab.

− In the Anonymous access and authentication control area, click the Edit ... button.

− Make sure that the Anonymous access checkbox is checked.

Page 25: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 17 All Rights Reserved

Configuring .svc Extension

• In IIS, right-click over the root virtual directory you are using.

− Select Properties from the context menu.

− The Properties dialog will come up. Make sure you have the Virtual Directory tab selected.

− Click the Configuration button.

Page 26: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 18 All Rights Reserved

Configuring .svc Extension (Cont’d)

− The Application Configuration dialog will come up. Make sure the Mappings tab is selected.

− Select the .svc extension, if present.

− Click Edit. If .svc is not present, click Add.

Page 27: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 19 All Rights Reserved

Configuring .svc Extension (Cont’d)

− The Add/Edit Application Extension Mapping dialog comes up.

− Make sure that the Executable box specifies the version of aspnet_isapi.dll that you are using. Click the Browse button to navigate to the correct version, if necessary.

− Click OK, OK, and OK.

• You should now be assured that the .svc extension will be recognized by ASP.NET.

Page 28: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 20 All Rights Reserved

Demo – Hello WCF

• We’ll create a “Hello WCF” service, hosted in IIS.

• Our solution will have three projects:

− A class library implementing the service

− A website exposing the service via HTTP

− A Windows client that invokes the service

1. Use Visual Studio to create a new blank solution HelloWCF in the Demos folder.

2. In Solution Explorer, right-click over the solution and choose Add | New Project.

Page 29: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 21 All Rights Reserved

Demo – Hello WCF (Cont’d)

3. From the .NET Framework 3.0 project types choose the WCF Service Library template. Enter HelloLib as the name of your new project.

4. Change the name of the file Class1.cs to Hello.cs. Examine the contents of this file, which include extensive comments and starter code for both a Service Contract and also a Data Contract.

Page 30: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 22 All Rights Reserved

A Service Contract

5. Edit Hello.cs to include only a simple service contract IHello with one method, SayHello(). The class Hello implements the service contract.

using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; using System.Runtime.Serialization; namespace HelloLib { [ServiceContract()] public interface IHello { [OperationContract] string SayHello(string name); } public class Hello : IHello { public string SayHello(string name) { return "Hello: " + name; } } } 6. Build the service library project.

Page 31: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 23 All Rights Reserved

A Website for the Service

• We will host our new WCF service using IIS.

• To do this, we’ll add a website project to our solution.

1. In Solution Explorer, right-click on the solution and choose Add | New Web Site.

2. For template choose WCF Service. For location select HTTP. Browse to the Demos\HelloWCF folder. You will be informed that ‘HelloWCF’ is not marked as an application in IIS. Click Yes to open it anyway. Type in HelloSvc at the end of the path. Select the appropriate language.

Page 32: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 24 All Rights Reserved

WCF Service Template

3. Examine the generated website files:

− Service.cs contains a sample service contract and implementation.

− Service.svc provides a .svc endpoint, which helps the service model find the correct service type to host.

− Web.config provides service model configuration settings for the sample service.

4. Delete the file Service.cs from the project. We have already provided the service contract and implementation in the class library project. Our approach is flexible, making it easy to provide different hosts for our service.

5. Edit the file Service.svc to specify the service type, based on the namespace and class name of the service implementation. This file is analogous to the .asmx file in ASP.NET Web services.

<% @ServiceHost Language=C# Debug="true" Service="HelloLib.Hello" %>

Page 33: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 25 All Rights Reserved

Service Configuration

• Services can be configured in code or using config files

− XML configuration files are preferred, because they make it easier to reconfigure services, and to reuse services in different contexts without needing to rebuild the service code.

• When configuring IIS-hosted services, information on the service can be included in the Web.config file for the website.

− Data is placed in the <system.ServiceModel> element

6. Continuing the demo, edit the generated Web.config file to show the service name and contract as specified in the class library project.

<?xml version="1.0"?> <configuration xmlns="..."> <system.serviceModel> <services> <!-- Before deployment, you should ... --> <service name="HelloLib.Hello" behaviorConfiguration="returnFaults"> <endpoint contract=" HelloLib.IHello" binding="wsHttpBinding"/> </service> </services> ... </system.serviceModel> ... </configuration>

Page 34: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 26 All Rights Reserved

Configuration Details

• From the XML configuration file shown above, you can deduce that…

• An application can support more than one service

− By default, Visual Studio 2005 uses a combination of the namespace and the name you gave for the service as the service name in the config file.

• A service can define more than one endpoint

− Endpoints are independent of the hosting application type. Even though the simple service is hosted in a console application, it can specify an HTTP address in its endpoint. It is the job of the WCF runtime to make this work; it is of no concern to the service how client contact it.

• Endpoints are defined by address, binding and contract

− Any or all of these three can be defined in code, if you wish. It provides most flexibility if you specify them all in XML.

− Visual Studio 2005 will specify the address in code, leaving the binding and contract in the configuration file.

Page 35: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 27 All Rights Reserved

Referencing the Class Library

7. In Solution Explorer, right-click over the website and choose Add Reference.

8. Select the Projects tab and make sure the HelloLib project is selected.

9. Observe that a Bin folder is created containing HelloLib.dll. Also, a number of assemblies are added to Web.config.

Page 36: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 28 All Rights Reserved

Examining the Service in the Browser

• Since our service is hosted in IIS, we can examine the Service.svc file in the browser.

10. Build the solution.

11. Right-click over the website in Solution Explorer and choose Set as Startup Project.

12. Run the solution. Internet Explorer should come up, showing a page similar to this:

Page 37: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 29 All Rights Reserved

Metadata Publishing Support

13. Currently metadata publishing is not supported. We’ll need metadata to generate a proxy for a client, so let’s add metadata support, as instructed by the page displayed in the browser. A behavior is already supplied, so need to do just two things:

− Provide an endpoint for the contract IMetadataExchange.

− Provide a <serviceMetadata> element for the behavior, with HTTP GET enabled.

<services> <service name="HelloLib.Hello" behaviorConfiguration="returnFaults"> <endpoint contract="HelloLib.IHello" binding="wsHttpBinding"/> <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="returnFaults"> <serviceDebug includeExceptionDetailInFaults="true"/> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> 14. Build and run.

Page 38: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 30 All Rights Reserved

Service in the Browser with Metadata

15. Examine the page displayed in the browser now. Metadata publishing has been enabled, so more information is available. Everything is in place for generating a proxy, and the page gives instructions for doing so.

Page 39: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 31 All Rights Reserved

WCF Clients

• As in all common distribution technologies, clients interact with services through proxies.

− The proxy hides details of the communication mechanism being used from the client code.

− The proxy implements the same interface as the service, so that the client can use exactly the same calls.

• Proxies are created using metadata provided by the service.

• The ABC (address, binding, contract) information provided by the service can be used to construct a proxy that

− Knows where to contact the service.

− Implements code to use the appropriate communication mechanism (eg. HTTP call versus TCP).

− Implements the operations defined in the service interface.

Page 40: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 32 All Rights Reserved

Creating WCF Clients

• Visual Studio 2005 can create clients to WCF services in the same way as it creates them for Web services.

− The configuration file contains endpoint details.

• You can use the svcutil.exe command line tool to generate proxies.

− Useful when not using Visual Studio.

− It also provides more control over the proxy generation process.

• The client code creates a proxy and calls methods

− Use exception handling, because lots can go wrong in distributed applications.

Page 41: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 33 All Rights Reserved

Demo – A Client for Hello WCF

• We’ll create a Windows Forms client.

1. Add a third project to the solution, this time a Windows Application. Specify HelloWin as the name of your new project.

Page 42: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 34 All Rights Reserved

Client Demo (Cont’d)

2. Provide the following user interface.

3. In preparation for generating the proxy, one more time run the website project, and copy the URL you see to the Clipboard. (We are running the project simply to see what the URL is.)

http://localhost/WcfCs/Demos/HelloWCF/HelloSvc/Service.svc 4. In Solution Explorer, right-click over the HelloWin project and

select Add Service Reference from the context menu.

5. Paste in the URI for the service.

Page 43: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 35 All Rights Reserved

Client Demo (Cont’d)

6. In HelloWin verify that there is a reference to System.ServiceModel. (Added automatically when the service reference is added.)

7. Build the solution to make sure you get a clean compile for all three projects.

8. Observe in Solution Explorer there is now a file localhost.cs under localhost.map under Service References.

9. Examine the file localhost.cs. This contains the code for the proxy. Note in particular that the proxy class is HelloClient, in the namespace HelloWin.localhost.

10. In Form1.cs import the namespace HelloWin.localhost.

using HelloWin.localhost;

Page 44: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 36 All Rights Reserved

Client Demo (Cont’d)

11. Add a handler for the Greet button and provide the following code to invoke the SayHello() method on the proxy.

private void btnGreet_Click(object sender, EventArgs e) { HelloClient proxy = new HelloClient(); txtResponse.Text = proxy.SayHello(txtName.Text); } 12. Make HelloWin the startup project.

13. Build and run. You should be able to enter any name and get back a greeting from the service.

14. A copy of this completed demo is provided in the Chap01\HelloWCF folder.

Page 45: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 37 All Rights Reserved

Lab 1

Creating a Simple Service and Client

In this lab, you will use Visual Studio 2005 to create a simple WCF service that is hosted in IIS. You will then create a client Console application, use svcutil.exe to create a proxy for the service, and demonstrate that the client can contact the service.

Detailed instructions are contained in the Lab 1 write-up at the end of the chapter.

Suggested time: 45 minutes

Page 46: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 38 All Rights Reserved

WCF Architecture

• Services and clients exchange SOAP messages.

− But they are not limited to text format; they can be sent in binary if that is more efficient.

• Channels abstract the communication process.

− Channels can be composed, so that the output from one channel acts as the input to another.

− There are two types of channel.

• Transport channels implement transport mechanisms

− Such as HTTP and TCP/IP.

• Protocol channels implement elements of the SOAP-based messaging protocol

− For example, the security channel implements SOAP security.

− The channel stack can be specified in configuration files or in code.

• Behaviors extend or modify service and client operation

− For example, whether metadata is published, or authentication is required.

− Behaviors can be specified in configuration or code.

Page 47: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 39 All Rights Reserved

WCF Architecture

• Client code creates a proxy.

− The client often uses a ChannelFactory object to create the proxy. The proxy sends messages through the channel stack to the Dispatcher, which makes a call to an endpoint on the service.

• Service code often uses ServiceHost objects to manage WCF services.

Client

Proxy

Protocol Channel(s)

Transport Channel

Protocol Channel(s)

Transport Channel

Dispatcher

Service Endpoints

Page 48: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 40 All Rights Reserved

ServiceHost and ChannelFactory

• ServiceHost is used on the server side.

− A ServiceHost is used to host services in code (ie. Self-hosted services). The ServiceHost creates a ServiceDescription object, which consists of a type (implementing the service), a collection of behaviors (which control how the service behaves) and descriptions of one or more endpoints.

− The WCF runtime uses the ServiceDescription to build the channel stack and configure the endpoints.

• ChannelFactory is used on the client side.

− A ChannelFactory creates a ClientDescription. This consists of a collection of behaviors, and one endpoint. There is no need for a type, because clients don’t implement service contracts.

− The WCF runtime uses the ClientDescription to build the channel stack and proxy.

Page 49: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 41 All Rights Reserved

Service Contexts and Instances

• Each .NET host process can contain one or more app domains, and each app domain may contain zero or more ServiceHosts.

− Within a ServiceHost, service instances live in contexts; a WCF context is similar to a COM+ (Enterprise Services) context or a .NET context-bound object, in that it provides an environment in which service instances live. Properties and methods on the context allow the developer to control the behavior of the service instance, especially with respect to concurrency and object lifetime. A context can host at most one instance of a service object, so it is possible to have empty contexts.

Host Process

AppDomain

ServiceHost

Context

Service Object

Empty Context

Page 50: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 42 All Rights Reserved

Summary

• WCF unifies a number of existing technologies for creating distributed applications.

• WCF services can be hosted in IIS, Windows applications, or WAS.

• WCF services and clients exchange SOAP messages.

• WCF services and clients can be configured in code, or via XML configuration files.

• WCF services are defined by addresses, bindings and contracts.

Page 51: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 43 All Rights Reserved

Lab 1

Creating a Simple WFC Service and Client

Introduction In this lab, you will use Visual Studio 2005 to create a simple WCF service that is hosted in IIS. You will then create a client Console application, use svcutil.exe to create a proxy for the service, and demonstrate that the client can contact the service. Suggested Time: 45 minutes. Root Directory: OIC\WcfCs Directories: Labs\Lab1 (create solution here)

Chap01\Add (contains lab solution) Part 1: Creating the Service Library 1. Use Visual Studio 2005 to create an empty solution Add in the working directory.

2. Add a new project AddLib to your solution using the WCF Service Library template.

3. Rename the file Class1.cs to Add.cs.

4. Delete the starter code except for the namespace imports and the declaration of the AddLib namespace.

5. Provide the following simple service contract ICalc.

namespace AddLib { [ServiceContract] public interface ICalc { [OperationContract] int Add(int x, int y); } } 6. Provide the following implementation:

public class Calc : ICalc { public int Add(int x, int y) { return x + y; }

Page 52: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 44 All Rights Reserved

} 7. Build the solution.

Part 2: Creating the Service Website 1. Add a new Web Site AddSvc to your solution, using the WCF Service template. Use

HTTP and the following URL:

http://localhost/WcfCs/Labs/Lab1/Add/AddSvc 2. Delete the file Service.cs from the Web Site.

3. Add a reference to the AddLib project. Observe that a Bin folder has been created, containing the assembly AddLib.dll.

4. Edit the file Service.svc to specify AddLib.Calc as the service.

<% @ServiceHost Language=C# Debug="true" Service="AddLib.Calc" %> 5. Edit Web.config to provide the proper names of the service and contract.

<?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.serviceModel> <services> <!-- Before deployment, you should remove the ... --> <service name="AddLib.Calc" behaviorConfiguration="returnFaults"> <endpoint contract="AddLib.ICalc" binding="wsHttpBinding"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="returnFaults"> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> 6. In Solution Explorer make the Web Site the startup project.

7. Build and run this project. Internet Explorer should come up, displaying a page stating that metadata publishing has been disabled.

8. Enable metadata publishing by adding a new endpoint for metadata publishing and a <serviceMetadata> element to the behavior.

Page 53: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 45 All Rights Reserved

<service name="AddLib.Calc" behaviorConfiguration="returnFaults"> <endpoint contract="AddLib.ICalc" binding="wsHttpBinding"/> <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/> </service> ... <behavior name="returnFaults"> <serviceDebug includeExceptionDetailInFaults="true"/> <serviceMetadata httpGetEnabled="true"/> </behavior> 9. Build and run again. Again Internet Explorer will be brought up. Now you should see

a page with instructions for creating a proxy by running svcutil.exe.

10. Copy the syntax for running svcutil.exe at the command line into a new text file, which you should save under the name MakeProxy.bat.

Part 3: Creating a Console Client 1. Add a third project to your solution, a Console application named AddCon.

2. Add a reference to System.ServiceModel.

3. Bring up the Visual Studio 2005 command prompt and run the MakeProxy.bat batch file that you created at the end of Part 2.

4. Copy the generated Calc.cs and output.config files to the AddCon folder. Rename output.config to app.config.

5. Add these files to the AppCon project.

6. Provide the following code in Program.cs to instantiate a proxy object and call the Add() method, displaying the result.

static void Main(string[] args) { CalcClient proxy = new CalcClient(); int sum = proxy.Add(5, 7); Console.WriteLine("sum = {0}", sum); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); } 7. Make AddCon the startup project.

8. Build and run.

Page 54: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 1

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC 46 All Rights Reserved

Page 55: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 197 All Rights Reserved

Chapter 6

More about Service Contracts

Page 56: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 198 All Rights Reserved

More about Service Contracts

Objectives

After completing this unit you will be able to:

• Explain the issues of versioning in the use of service contracts.

• Describe and implement the three message exchange patterns available in WCF:

− Request/Reply

− Oneway

− Duplex

• Implement asynchronous calls to a service through a special proxy.

Page 57: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 199 All Rights Reserved

Versioning Service Contracts

• Many changes can be made to a service contract without impacting existing clients.

− The table summarizes the impact on existing clients of various service contract changes.1

Change Impact

New parameter in an operation

Client still runs. Service initializes new parameter to default value.

Remove parameter Client can still call service, but superfluous parameters are ignored by the service and data is lost.

Parameter type modified The incoming type will be converted by the service, otherwise an exception will be thrown.

Return type modified The return type will be converted, otherwise an exception will be thrown.

New operation Client unaffected. It will not invoke operations it does not know about.

Remove operation An exception will be thrown.

1 This table is based on one in the book Learning WCF by Michele Bustamante.

Page 58: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 200 All Rights Reserved

Versioning Example

• We illustrate a number of versioning scenarios with our math calculator example.

− See MathVersion in three steps for the same client talking to three different versions of the service.

− See MathVersion2 for a version 2 client talking to a version 2 service.

− See MathVersion3 for a version 3 client talking to a version 3 service.

• Here is version 3:

Page 59: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 201 All Rights Reserved

Version 1 Service

• Version 1 service contract and implementation: [ServiceContract(Namespace="http://oi.com")] public interface IMath { [OperationContract] string Add(int x, int y); [OperationContract] string Subtract(int x, int y); } public class Math : IMath { public string Add(int x, int y) { return (x + y).ToString(); } public string Subtract(int x, int y) { return (x - y).ToString(); } }

• Version 1 client: private void btnAdd_Click(object sender, EventArgs e) { MathClient proxy = new MathClient(); int x = Convert.ToInt32(txtX.Text); int y = Convert.ToInt32(txtY.Text); txtResult.Text = proxy.Add(x, y); }

Page 60: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 202 All Rights Reserved

Version 2 Service

• The version 2 service contract modifies the parameter and return types of the two version 1 operations.

[ServiceContract(Namespace="http://oi.com")] public interface IMath { [OperationContract] int Add(string x, string y); [OperationContract] int Subtract(string x, string y); ... } public class Math : IMath { public int Add(string x, string y) { Debug.WriteLine("Add v2.0"); int nx = Convert.ToInt32(x); int ny = Convert.ToInt32(y); return (nx + ny); } ...

• The version 1 client invokes the service using version 1 operation signatures.

− int parameter type is converted to string

− int return type is converted to string

• Net result is that the version 1 client runs unchanged against the version 2 service, and the user output appears identical.

Page 61: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 203 All Rights Reserved

New Operations

• The version 2 service also provides two additional operations.

[ServiceContract(Namespace="http://oi.com")] public interface IMath { ... [OperationContract] string Multiply(string x, string y); [OperationContract] string Divide(string x, string y); }

• The version 1 client does not know about the new operations, which are therefore never called.

• The debug write statement in Add() shows in the Output window that we are indeed running the version 2 service.

Add v2.0

Page 62: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 204 All Rights Reserved

Version 3 Service

• The version 3 service adds a new parameter, used to label the result, which is now returned as a string by all operations.

[ServiceContract(Namespace="http://oi.com")] public interface IMath { [OperationContract] string Add(string x, string y, string label); [OperationContract] string Subtract(string x, string y, string label); [OperationContract] string Multiply(string x, string y, string label); [OperationContract] string Divide(string x, string y, string label); } public class Math : IMath { public string Add(string x, string y, string label) { Debug.WriteLine(label + ": Add v3.0"); int nx = Convert.ToInt32(x); int ny = Convert.ToInt32(y); return label + ": " + (nx + ny).ToString(); } ...

Page 63: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 205 All Rights Reserved

Version 1 Client / Version 3 Service

• The version 1 client does not know about the additional parameter and so does not pass any information for the third parameter.

− On the service side this third parameter is initialized to its default value, the empty string.

• The version 1 client runs successfully against the version 3 service.

− But the result is slightly different, with a colon prepended to the actual result.

Page 64: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 206 All Rights Reserved

Message Exchange Patterns

• In WCF a message exchange pattern describes how messages are sent between a client and a service.

• There are three main message exchange patterns:

− Request-reply

− Oneway (“fire and forget”)

− Duplex (or callback)

Client ServiceRequest/Reply

Client Service

Client Service

Request

Oneway

Callback

Page 65: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 207 All Rights Reserved

Request-Reply

• The classic pattern is request-reply:

− The client issues a request message and blocks until it receives a reply message. The operation is synchronous.

− The request message contains input parameters.

− The reply message contains the returned value and any output parameters.

− If a reply is not received within the timeout period (default is one minute), the client will receive a TimeoutException.

• For an example of the familiar request-reply pattern, see HelloRequestReply in the chapter directory.

Page 66: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 208 All Rights Reserved

Oneway

• The oneway pattern is useful when an operation has no returned value and the client does not care about the success or failure of the operation.

• When a client issues a oneway call, WCF creates a request message, but no associated reply message will ever return to the client.

• You can specify that an operation should be treated as oneway by using the IsOneWay attribute on the operation contract.

− A oneway operation must have void as the return type.

[ServiceContract(Namespace="http://oi.com")] public interface IHello { [OperationContract(IsOneWay=true)] void SayHello(string name); }

• A client making a oneway call does not block.

− The service model releases the client’s connection as soon as the message arrives at the channel and before the operation is invoked.

− This means that clients are not notified of any exceptions that occur.

Page 67: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 209 All Rights Reserved

Oneway Example

• See HelloOneway in the chapter folder.

• No response is returned to the client.

• The service writes the generated greeting to a file. public class Hello : IHello { public void SayHello(string name) { StreamWriter sw = File.AppendText("Output.txt"); sw.WriteLine("Hello: " + name); sw.Close(); } }

• Look for the output file in the bin\Debug folder of the Host project.

Page 68: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 210 All Rights Reserved

Duplex

• In the duplex message exchange pattern there is a bi-directional flow between a client and a service.

• The service provides a callback contract.

− A callback contract is like a service contract, with methods decorated with the OperationContract attribute.

public interface IHelloCallback { [OperationContract(IsOneWay=true)] void SayHelloCallback(string greeting); }

− The ServiceContract attribute is missing, because it is implied by its being a callback contract.

− The operations should be marked as oneway to avoid a deadlock.

• What makes this particular interface a callback contract is its association with a regular contract through the CallbackContract attribute.

[ServiceContract(Namespace = "http://oi.com", CallbackContract=typeof(IHelloCallback))] public interface IHello { [OperationContract(IsOneWay=true)] void SayHello(string name); }

− An operation that will call back to the client should be designed as oneway.

Page 69: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 211 All Rights Reserved

Callbacks

• With a callback the role of client and service are reversed: the client assumes the role of service, and the service becomes the client.

• When the service provides a callback contract, the generated proxy will include code allowing the client both to be a service and to host the callback object.

• Only certain bindings support duplex communication with callbacks:

− NetTcpBinding

− NetNamedPipeBinding

− WSDualHttpBinding (via two HTTP channels)

Page 70: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 212 All Rights Reserved

Invoking a Callback

• The service can obtain a reference to a callback using the OperationContext class.

− See HelloDuplex in the chapter folder for a complete example.

public class Hello : IHello { public void SayHello(string name) { StreamWriter sw = File.AppendText("Output.txt"); string greeting = "Hello: " + name; sw.WriteLine(greeting); sw.Close(); IHelloCallback callback = OperationContext.Current.GetCallbackChannel <IHelloCallback>(); callback.SayHelloCallback(greeting); } }

Page 71: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 213 All Rights Reserved

Callback on the Client

• When the service specifies a callback, the generated proxy contains code to implement duplex communication.

− A special constructor of the proxy takes a context object.

public partial class Form1 : Form, IHelloCallback { ... private HelloClient proxy; private void Form1_Load(object sender, EventArgs e) { InstanceContext context = new InstanceContext(this); proxy = new HelloClient(context); } public void SayHelloCallback(string greeting) { txtResponse.Text = greeting; } private void btnGreet_Click(object sender, EventArgs e) { proxy.SayHello(txtName.Text); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { proxy.Close(); } }

Page 72: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 214 All Rights Reserved

Asynchronous Proxies

• WCF provides another technique for a client to receive a callback, through asynchronous proxies.

− Use SvcUtil.exe with the /async option.

− In this case you must use SvcUtil.exe rather than adding a service reference in Visual Studio.

• The proxy will then contain two additional methods:

− BeginXxxx()

− EndXxxx()

• Use the BeginXxxx() method to start the async operation and pass a callback function.

int x = Convert.ToInt32(txtX.Text); int y = Convert.ToInt32(txtY.Text); proxy.BeginAdd(x, y, OnCompletion, null);

• Implement the callback OnCompletion(), where you can call EndXxxx() to retrieve result data.

private void OnCompletion(IAsyncResult result) { ... int sum = proxy.EndAdd(result); ...

Page 73: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 215 All Rights Reserved

Threading Considerations

• In a Windows client program, the callback is received on a thread that is different from the UI thread.

• It is unsafe to update a control from a thread other than the UI thread.

− You could hit a race condition.

• Windows Forms provides an Invoke() method that can be used to pass control to the UI thread by means of a delegate.

private delegate void ShowCompletionDelegate( string strSum); ... private void OnCompletion(IAsyncResult result) { string msg = string.Format( "OnCompletion() on thread {0}", Thread.CurrentThread.GetHashCode()); Debug.WriteLine(msg); int sum = proxy.EndAdd(result); // Use Invoke to pass control to UI thread ShowCompletionDelegate dlg = new ShowCompletionDelegate(ShowCompletion); this.Invoke(dlg, new object[] { sum.ToString() }); }

− The delegate is declared and initialized with a reference to a callback function.

Page 74: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 216 All Rights Reserved

Threading Considerations (Cont’d)

• The callback function that receives control from Invoke() runs on the UI thread.

private void ShowCompletion(string strSum) { string msg = string.Format( "ShowCompletion() on thread {0}", Thread.CurrentThread.GetHashCode()); Debug.WriteLine(msg); txtSum.Text = strSum; txtStatus.Text = "Asynchronous call completed"; }

• See AsyncMath in the chapter folder for a complete example of an async proxy.

− You can show a message while the async operation is pending.

• Debug trace statements show the different threads involved.

btnAsyncAdd_Click() on thread 10 OnCompletion() on thread 14 ShowCompletion() on thread 10

Page 75: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 217 All Rights Reserved

Lab 6

A Callback in a Contact Management System

In this lab you will implement a callback operation in a contact management system. Starter code provides a tabbed user interface with panels for an address book, for sending emails to a distribution list, and for receiving a result notification.

Detailed instructions are contained in the Lab 6 write-up at the end of the chapter.

Suggested time: 30 minutes.

Page 76: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 218 All Rights Reserved

Summary

• In WCF service contracts are quite version tolerant.

• There are three message exchange patterns available in WCF:

− Request/Reply

− Oneway

− Duplex

• In the duplex message exchange pattern there is a bi-directional flow between a client and a service, implemented with the use of a callback contract.

• You can implement asynchronous calls to a service through a special proxy.

Page 77: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 219 All Rights Reserved

Lab 6

A Callback in a Contact Management System

Introduction In this lab you will implement a callback operation in a contact management system. Starter code provides a tabbed user interface with panels for an address book, for sending emails to a distribution list, and for receiving a result notification.

Suggested Time: 30 minutes. Root Directory: OIC\WcfCs Directories: Labs\Lab6\ContactManager (do your work here) Chap06\ContactManager\Start (starter code backup) Chap06\ContactManager\Duplex (answer to Part 2) Part 1: Examine the Starter Project 1. Run the starter solution. There are three projects: a service library, a host, and a

Windows client program. Build the solution and start the host. Then start the Windows client. You will see a tabbed user interface.

2. Begin with the first tab, Contacts, which maintains an address book. See the screen capture on the following page. The functionality is similar to the Contact Manager in the previous chapter. The contact information now contains an email address. As before the contact information is saved in session store, but there is the additional capability to persist the data by clicking Save and restore the data by clicking Restore.

Page 78: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 220 All Rights Reserved

3. Add a new contact and click Save. Examine the XML file contacts.xml in the bin\Debug folder of the Host project.

<?xml version="1.0"?> <ArrayOfContact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Contact> <FirstName>John</FirstName> <LastName>Smith</LastName> <Country>United States</Country> <EmailAddress>[email protected]</EmailAddress> ... </ArrayOfContact> 4. Terminate the program. Examine the file ContactManager.cs in the service library

project. The Save() method is implemented using XML serialization.

public void Save() { XmlSerializer ser = new XmlSerializer(typeof(List<Contact>)); FileStream fs = new FileStream("contacts.xml", FileMode.Create); ser.Serialize(fs, contacts); fs.Close(); } 5. There is a second interface, IEmailManager. Note the oneway method for sending a

text email message to a distribution list.

public interface IEmailManager { [OperationContract(IsOneWay = true)] void Send(string text, DistributionList list, string from); } 6. There is a data contract for DistributionList, which is an array of strings containing

email addresses.

Page 79: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 221 All Rights Reserved

[DataContract(Namespace="http://schemas.oi.com")] public class DistributionList { [DataMember] public string[] EmailList; } 7. There is a class Message representing a message sent to the distribution list.

public class Message { public string To; public string From; public string Text; } 8. The Send() method simulates sending the email to the distribution list by creating an

XML file containing the messages.

public void Send(string text, DistributionList list, string from) { string NL = Environment.NewLine; List<Message> messages = new List<Message>(); foreach (string to in list.EmailList) { Message m = new Message(); m.To = to; m.From = from; m.Text = text; messages.Add(m); } XmlSerializer ser = new XmlSerializer(typeof(List<Message>)); FileStream fs = new FileStream("messages.xml", FileMode.Create); ser.Serialize(fs, messages); fs.Close(); } 9. Now start the program again and click on the second tab.

10. Click on the button. That will bring up a multiple-selection list box. Select both names by holding down the Control key.

Page 80: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 222 All Rights Reserved

11. Click OK. Examine the combobox in the main form, which will now contain these names as the email addresses.

12. Now enter an email address in the From text box (or go with the supplied [email protected]) and a brief message.

13. Click the Send button.

Page 81: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 223 All Rights Reserved

14. Examine the file messages.xml that is created in the bin\Debug folder of the Host project.

<?xml version="1.0"?> <ArrayOfMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Message> <To>[email protected]</To> <From>[email protected]</From> <Text>Hello there!</Text> </Message> <Message> <To>[email protected]</To> <From>[email protected]</From> <Text>Hello there!</Text> </Message> </ArrayOfMessage> 15. Back in the main form, click on the Result tab. The text box provided to display

results is empty, as the Send operation is oneway and currently there is no channel for sending information back from the service to the client. Terminate the program.

Part 2: Implement a Callback 1. In ContactManager.cs in the service library project add a specification of a callback

interface that will return response text back to the client. The operation contract should be oneway.

public interface IEmailManagerCallback { [OperationContract(IsOneWay = true)] void SendCallback(string response); } 2. Add a specification of the CallbackContract property to the ServiceContract

attribute on the IEmailManager interface.

[ServiceContract(Namespace = "http://oi.com", SessionMode = SessionMode.Required, CallbackContract = typeof(IEmailManagerCallback)) ] public interface IEmailManager { [OperationContract(IsOneWay = true)] void Send(string text, DistributionList list, string from); } 3. At the top of the Send() method provide code to send an immediate

acknowledgement back to the client via the callback.

public void Send(string text, DistributionList list, string from) { // Send immediate acknowledgement to client

Page 82: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 224 All Rights Reserved

// You may send multiple messages through the channel string NL = Environment.NewLine; string response = "Send request has been received" + NL; IEmailManagerCallback callback = OperationContext.Current.GetCallbackChannel <IEmailManagerCallback>(); callback.SendCallback(response); 4. Examine the App.config file in the Host project. We use NetTcp binding, which

supports duplex. (We needed such a binding anyway, because we had separate endpoints for the IContactManager and IEmailManager contracts, a feature not supported by HTTP.)

5. In the Windows client project delete the current service reference.

6. Start the Host project. In the client project add back in the service reference, using http://localhost:8000 as the Service URL. Terminate the host.

7. In Form1.cs in the client project, make Form1 inherit also from the IEmailManagerCallBack interface.

namespace ContactWin { public partial class Form1 : Form, IEmailManagerCallback { ... 8. In Form1_Load() create a context object and pass it to the constructor of the

EmailManagerClient proxy.

private void Form1_Load(object sender, EventArgs e) { InstanceContext context = new InstanceContext(this); proxy = new ContactManagerClient(); emailproxy = new EmailManagerClient(context); ShowContacts(lstContacts); } 9. Implement the SendCallback() method of the IEmailManagerCallback interface.

Note that by default this method is called on UI thread, so it is safe to update controls on the thread.

public void SendCallback(string response) { txtResult.Text += response; } 10. Import the System.ServiceModel namespace, which is needed because we’ve used

the InstanceContext class.

Page 83: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 225 All Rights Reserved

11. Build the solution. Start the host project and then the client project. Select some names into the distribution list and try sending a small test message. Click on the Result tab. You should see the acknowledgement message displayed. Terminate the program.

12. In ContactManager.cs add code to the end of the Send() method to sleep for a couple of seconds to simulate the delay from actually sending out the email. You will need to import the System.Threading namespace for the Sleep() method.

// Simulate time for sending out emails Thread.Sleep(2000); 13. Add code to create a response string that lists the email addresses that were sent to.

Pass this response string to the client through the callback.

// Create response string and pass to callback function response = "Message was sent to distribution list:" + NL; foreach (string to in list.EmailList) { response += " " + to + NL; } callback.SendCallback(response); 14. In the client add code to the callback function to switch to the Result tab after the

response string has been received.

public void SendCallback(string response) { txtResult.Text += response; TabControl1.SelectTab(2); } 15. Provide a handler for the Clear button that will set the result text box to the empty

string. Build and run, exercising sending out various email distributions.

Page 84: Introduction to WCF Using C# - ITCourseware · WCF Services • WCF supports the WS-* family of Web service protocols. − The WS-* family of protocols have been developed by various

WcfCs Chapter 6

Rev. 1.1 Copyright © 2008 Object Innovations Enterprises, LLC. 226 All Rights Reserved