windows communication foundation and web services

49
Windows Communication Foundation and Web Services

Upload: metta

Post on 25-Feb-2016

61 views

Category:

Documents


2 download

DESCRIPTION

Windows Communication Foundation and Web Services. WCF (Introduction). It’s Microsoft’s current inter-machine communication foundation used to integrate HTTP requests and responses Web Services Messaging Lower-level TCP/IP Ajax / REST services And everything else. WCF Fundamentals. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Windows Communication Foundation and Web Services

Windows Communication

Foundation and Web Services

Page 2: Windows Communication Foundation and Web Services

Slide 2

A Word About Attributes The statements inside the brackets are

considered attributes Attributes are declarative information

about c# code Don’t confuse the term Attribute here

with an HTML attribute

Page 3: Windows Communication Foundation and Web Services

Slide 3

A Word About Interfaces An interface contains the signatures of 

methods, properties, events or indexers A class that implements the interface

must implement the members of the interface that are specified in the interface definition

The interface name typically start with the letter I (capitalized)

Page 4: Windows Communication Foundation and Web Services

Slide 4

A Word About Interfaces The class named ImplementationClass

implements ISampleInterface

Page 5: Windows Communication Foundation and Web Services

Slide 5

WCF (Introduction) It’s Microsoft’s current inter-machine

communication foundation used to integrate HTTP requests and responses Web Services Messaging Lower-level TCP/IP Ajax / REST services And everything else

Page 6: Windows Communication Foundation and Web Services

Slide 6

WCF (Introduction)And I’ll start out by saying it’s complicated

Page 7: Windows Communication Foundation and Web Services

Slide 7

WCF Fundamentals Simply put, it’s a set of APIs used to

send messages between services and clients

Messages are sent between endpoints using a transport protocol (such as (such as HTTP / TCP and others)

Page 8: Windows Communication Foundation and Web Services

Slide 8

WCF Model (1)

Page 9: Windows Communication Foundation and Web Services

Slide 9

WCF Model (2)

Page 10: Windows Communication Foundation and Web Services

Slide 10

WCF Model (Contracts) Service contracts describe the precise

format of messages This is done using HTTP or another protocol The service contract defines the interface to

the outside world Operation contracts belong to a

service contract These are really the methods of the contract

Page 11: Windows Communication Foundation and Web Services

Slide 11

WCF Model (Contracts) We are talking about the format of

Function calls Parameters Return types These are message signatures

The programming model defines how we write the code to do all of this

Page 12: Windows Communication Foundation and Web Services

Slide 12

WCF Model (Contracts)

Page 13: Windows Communication Foundation and Web Services

Slide 13

WCF Model (Contract)Request-Reply Request-Reply services

This is the default type of service Clients make a request for service

(synchronously or asynchronously) Clients receive a response

Duplex services

Page 14: Windows Communication Foundation and Web Services

Slide 14

WCF Model (Contract)One Way In a One-Way service, the client sends a

request but does not expect a response To detect errors, create two One-Way

service contracts

Page 15: Windows Communication Foundation and Web Services

Slide 15

WCF Model (Contract)Duplex Both endpoints can send messages

independently These are implemented using the idea of a callback

To implement, create two interfaces The first is the one-way client interface The second is the callback contract

Refer to http://msdn.microsoft.com/en-us/library/ms731064(v=vs.110).aspx

Page 16: Windows Communication Foundation and Web Services

Slide 16

WCF Model (Service Runtime) This layer defines the behavior of the

runtime service (server configuration) We configure the service runtime using behaviors

There is much more detail here beyond the scope of this course

Page 17: Windows Communication Foundation and Web Services

Slide 17

WCF Model (Service Runtime) How many messages are processed at a

time InstanceContextMode / ConcurrencyMode

How many instances of a server can run (singleton) InstanceContextMode

Threading and reentrancy ConcurrencyMode

Page 18: Windows Communication Foundation and Web Services

Slide 18

WCF Model (Messaging) Transport channels are of two types

Transport channels send and receive messages

TCP, HTTP, MSMQ Protocol channels implement the

messaging protocols HTTP

Page 19: Windows Communication Foundation and Web Services

Slide 19

WCF Model (Hosting) Simply put, it’s where the application

runs (is hosted) Might be IIS or WAS

Page 20: Windows Communication Foundation and Web Services

Slide 20

WCF Model (Hosting)

Page 21: Windows Communication Foundation and Web Services

Slide 21

WCF Model (Hosting)

Page 22: Windows Communication Foundation and Web Services

Slide 22

WCF Model (Hosting - IIS) This is the easiest way to host your

service IIS treats the service as an ASP.NET Web

Site If you are using IIS6, then only HTTP and

HTTPS are supported You can create a self-hosted console (or

forms) application that listens for requests at a particular port

We can also host through Azure Services

Page 23: Windows Communication Foundation and Web Services

Slide 23

WCF and Web Services WCF has changed the .net picture of

Web Services They used to be a standalone product type

etc… They are now just another service within

the context of WCF

Page 24: Windows Communication Foundation and Web Services

Slide 24

The Goal of Web Services Supply a standard means for e-

commerce applications to communicate using different hardware and software platforms Supply a way for legacy applications to

communicate Provide a universal way to discover the

available services and the methods supplied by a particular service so that any consumer can call those methods

Page 25: Windows Communication Foundation and Web Services

Slide 25

Logical Web Service Model

Page 26: Windows Communication Foundation and Web Services

Slide 26

Web Service Vendors IBM – WebSphere Studio Application Developer SAP – SAP Web Application Server SUN – Sun ONE Web Services Platform

Developer Edition And of course Microsoft and Visual Studio .NET Others are likely to follow No matter the vendor, Web services will

always work the same way Any consumer should work with any provider

Page 27: Windows Communication Foundation and Web Services

Slide 27

Web Service Protocols Web services provide a hardware and

software agnostic way to call remote functions and return data from those remote functions

Protocols HTTP is the message transfer agent Simple Object Access Protocol (SOAP) Web Service Description Language (WSDL)

Page 28: Windows Communication Foundation and Web Services

Slide 28

Creating A Web Service Create a new Web side using the WCF

Service project type

Page 29: Windows Communication Foundation and Web Services

Slide 29

Web Service (Steps to Create) Design the service contract Implement the service contract

This is done via attributes Configure the service endpoints Host the service

Build client applications

Page 30: Windows Communication Foundation and Web Services

Slide 30

Web Service Implementation (Steps) Declare the members of the interface

that will be implemented in the .svc file Implement those members in the

corresponding class

Page 31: Windows Communication Foundation and Web Services

Slide 31

The Role of App.Config App.Config operates similarly to

Web.Config Here, it’s used to configure a Web Service All of this appears inside of <system.serviceModel>

We could also hardcode this

Page 32: Windows Communication Foundation and Web Services

Slide 32

App.config (Bindings) Bindings define the transport protocol

details needed for a client to communicate with a service

Predefined (provided) bindings <BasicHttpBinding> <NetTcpBinding>

Custom bindings

Page 33: Windows Communication Foundation and Web Services

Slide 33

App.config (Bindings) Here we bind to three different services

Page 34: Windows Communication Foundation and Web Services

Slide 34

App.config (Endpoints) Endpoints allow clients to access a Web

service Consists of

An address (URL that points to the service) Name of a binding (how to communicate

with the endpoint) The contract defining the methods

available

Page 35: Windows Communication Foundation and Web Services

Slide 35

App.config (Endpoints) We communicate with three services

Page 36: Windows Communication Foundation and Web Services

Slide 36

Service Contract A service contract exposes one or more

service operations A ServiceContract defines the types of

messages used in a conversation A ServiceOperation defines the

messages themselves

Page 37: Windows Communication Foundation and Web Services

Slide 37

Service Contract (Example 1)

Page 38: Windows Communication Foundation and Web Services

Slide 38

Service Contract (Example 2) Use a class instead of creating an

interface and implementing that interface

Page 39: Windows Communication Foundation and Web Services

Slide 39

Data Contract Simply put, simple types do not require

a data contract. Complex types do Serializable parameters do not require a

data contract

A DataContract describes a type that will be serialized by the service

A DataMember describe the members of the DataContract type

Page 40: Windows Communication Foundation and Web Services

Slide 40

Data Contract (Example 1)

Page 41: Windows Communication Foundation and Web Services

Slide 41

Configure Service Endpoints (1) Communication occurs through service

endpoints and four properties Address of the endpoint A binding that specifies how a client can

communicate with the endpoint A contract the defines the allowable

operations Local implementation details

Page 42: Windows Communication Foundation and Web Services

Slide 42

Configure Service Endpoints (2) We can configure endpoints via

Code In the config (web.config) file

Page 43: Windows Communication Foundation and Web Services

Slide 43

Configure Service Binding (1) Most bindings are provided with WCF Use BasicHttpBinding for most Web

services

Page 44: Windows Communication Foundation and Web Services

Slide 44

Host the Service IIS must be installed on the server

Page 45: Windows Communication Foundation and Web Services

Slide 45

Compiling the Service The service is compiled the same way

as any other .NET app. If you run it, you see a “special” test

client window The code is generated by .NET itself

Page 46: Windows Communication Foundation and Web Services

Slide 46

Test Service (Illustration 1)

Page 47: Windows Communication Foundation and Web Services

Slide 47

Test Service (Illustration 2)

Page 48: Windows Communication Foundation and Web Services

Slide 48

Creating the Test Client Create a WPF or Windows forms

application Insert the template code created by

Svcutil.exe into this project While the service is running locally, add

a service reference In a production app, you will have this URL

Change the name of the endpoint in app.config

Page 49: Windows Communication Foundation and Web Services

Slide 49

Adding the Service Reference (Example)