Dot Net Training Wcf Dot Net35

28
© Copyright GlobalLogic 2007 1 Microsoft .Net Framework 3.x The Windows Communication Foundation

description

 

Transcript of Dot Net Training Wcf Dot Net35

Page 1: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

1

Microsoft .Net Framework 3.x

The Windows Communication Foundation

Page 2: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

2

Agenda

• .Net 3.x

• The WCF- Developers perspective

• The WCF Architecture

• ABC …

• Fault Tolerance

• Demos / Hands on

• Questions and Answers

Page 3: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

3

• .Net 3.0:

– Net combines the power of the .NET Framework 2.0 with new technologies for building applications that have visually compelling user experiences, seamless communication across technology boundaries, and the ability to support a wide range of business processes.

– NET Framework 3.0 is a superset of the .NET Framework, designed to expose the new functionality in Windows Vista® to the developer through managed classes.

Overview to .Net 3.0

Windows Vista (Longhorn) is tightly integrated with .net 3.0 framework. The Look and feel of Vista is enhanced using the WPF.

Page 4: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

4

What's new in .Net 3.0 – A developer perspective

Programming model for Windows .NET Framework 3.0 consists of these basic components:

•.NET Framework 2.0

•Microsoft® Windows® Presentation Foundation (formerly Avalon)

•Microsoft® Windows® Communication Foundation (formerly Indigo)

•Microsoft® Windows® Workflow Foundation

•Windows CardSpace® (formerly code named "Infocard")

Page 5: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

5

Windows Communication Foundation

• The WCF is an incremental, evolutionary technology that brings all the formerly distinct and separate Microsoft connectivity technologies together under a single umbrella within the System.ServiceModel namespace. Included in WCF are Web services (ASMX), the Web service Extensions (WS*), Microsoft Message Queuing (MSMQ), Enterprise Services, COM+, and .NET Remoting using Windows Communication Foundation's System.ServiceModel namespace

• Having a single namespace that subsumes all of these into a coherent package is enormously useful, and makes designing, developing, and deploying applications that require connectivity far simpler.

• Whether the application connects via loosely coupled Web services, or tightly coupled Enterprise Services, the coding model will be consistent and the transition between different communication types will be smoother

Page 6: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

6

WCF in a Nutshell

Three main design goals of WCF are:• Unification of existing technologies • Interoperability across platforms • Service-oriented development

WCF offers immensely valuable support for reliability, transactions, concurrency management, security, and instance activation

WCF comes with a unified programming model providing:• A single API for building connected systems • Built-in support for a broad set of web service protocols • Implicit use of service-oriented development principles

WCF native messaging protocol is SOAP, which as an open standard provides the opportunity for the WCF service to interact with different technologies running on different platforms and non-Windows operating systems. It also can exist peacefully with its predecessors such as COM+ and Enterprise Services

Page 7: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

7

WCF in a Nutshell

Page 8: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

8

WCF in a Nutshell

The WCF stack maps almost on a one-to-one basis to all the SOA principles

Page 9: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

9

WCF Communication

With WCF, the client never interacts with the service directly, even when dealing with a local, in-memory service. Instead, the client always uses a proxy to forward the call to the service. The proxy exposes the same operations as the service, plus some proxy-management methods.

Same-machine communication Cross-machine communication

Page 10: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

10

The WCF architecture

Page 11: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

11

WCF

Page 12: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

12

WCF – Contracts

Contracts and DescriptionsContracts define various aspects of the message system. The

contract is a platform-neutral and standard way of describing what the service does. WCF defines four types of contracts.

• Service Contracts - Describes the operations a service can perform. Maps CLR types to WSDL.

• Data Contracts - Describes a data structure. WCF defines implicit contracts for built-in types such as int and string. Maps CLR types to XSD.

• Message Contracts - Defines the structure of the message on the wire. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format to comply with. Maps CLR types to SOAP messages.

• Fault contracts - Define which errors are raised by the service, and how the service handles and propagates errors to its clients.

Page 13: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

13

WCF

Service Runtime• The service runtime layer contains the behaviors that occur

only during the actual operation of the service, that is, the runtime behaviors of the service.

• Throttling controls how many messages are processed, which can be varied if the demand for the service grows to a preset limit.

• An error behavior specifies what occurs when an internal error occurs on the service, for example, by controlling what information is communicated to the client.

• Metadata behavior governs how and whether metadata is made available to the outside world.

• Instance behavior specifies how many instances of the service can be run (for example, a singleton specifies only one instance to process all messages).

• Transaction behavior enables the rollback of transacted operations if a failure occurs. Dispatch behavior is the control of how a message is processed by the WCF infrastructure.

• Extensibility enables customization of runtime processes. For example, message inspection is the facility to inspect parts of a message, and parameter filtering enables preset actions to occur based on filters acting on message headers.

Page 14: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

14

The ABC of WCF

Page 15: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

15

The ABC of WCF

ABC means that writing (and configuring) a WCF service is always a three-step process

• Define a contract and implement it on a service • Choose or define a service binding that selects a transport along with

quality of service, security and other options • Deploy an endpoint for the contract by binding it (using the binding

definition, hence the name) to a network address. Every service is associated with an address that defines where

the service is, a binding that defines how to communicate with the service, and a contract that defines what the service does.

• "A" stands for Address—as expressed in the wsdl:service section and links wsdl:binding to a concrete service endpoint address.

• "B" stands for Binding—as expressed in the wsdl:binding section and binds a wsdl:portType contract description to a concrete transport, an envelope format and associated policies.

• "C" stands for Contract—as expressed in the wsdl:portType, wsdl:message and wsdl:type sections and describes types, messages, message exchange patterns and operations.

Page 16: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

16

WCF

Endpoints - All communications with the WCF service will happen via the endpoints. The endpoint is composed of 3 parts (collectively called as ABC's of endpoint) as defines below:

• Address: The endpoints specify a Address that defines where the endpoint is hosted.

• Contract: The endpoints specify a Contract that defines which

methods of the Service class will be accessible via the endpoint; each endpoint may expose a different set of methods.

• Binding: The endpoints also define a binding that specifies how a client will communicate with the service and the address where the endpoint is hosted.Various components of the WCF are depicted in the figure below.

Page 17: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

17

WCF-Address

In WCF, every service is associated with a unique address. The address provides two important elements: the location of the service and the transport protocol or transport schema used to communicate with the service. Provides information on WHERE the service endpoint is.

scheme://<machinename>[:port]/path1/path2

WCF supports the following transport schemas:• HTTP• TCP• Peer network• IPC (Inter-Process Communication over named pipes)• MSMQhttp://localhost:8001 http://localhost:8001/MyService net.tcp://localhost:8002/MyService net.pipe://localhost/MyPipe net.msmq://localhost/private/MyService net.msmq://localhost/MyService

Page 18: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

18

WCF-Binding

A binding provides a consistent, canned set of choices regarding the transport protocol, message encoding, communication pattern, reliability, security, transaction propagation, and interoperability.

The service publishes its choice of binding in its metadata, enabling clients to query for the type and specific properties of the binding because the client must use the exact same binding values as the service. A single service can support multiple bindings on separate addresses.

The binding controls the following:·   The transport (HTTP, MSMQ, Named Pipes, TCP)·   The channels (one-way, duplex, request-reply)·   The encoding (XML, binary, MTOM…)·   The supported WS-* protocols (WS-Security, WS-Federation, WS-

Reliability, WS-Transactions)

Page 19: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

19

WCF-Binding

Binding Configuration Security Default Session Transactions Duplex

basicHttpBinding Basic Profile 1.1 None No    

wsHttpBinding WS Message Optional Yes  

wsDualHttpBinding WS Message Yes Yes Yes

wsFederationHttpBinding WS-Federation Message Yes Yes No

netTcpBinding .NET Transport Optional Yes Yes

netNamedPipeBinding .NET Transport Yes Yes Yes

netMsmqBinding .NET Transport Yes Yes No

netPeerTcpBinding Peer Transport     Yes

msmqIntegrationBinding MSMQ Transport Yes Yes  

Page 20: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

20

WCF-Choosing a Binding

Page 21: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

21

WCF – Fault Tolerance

In raw .NET programming, any unhandled exception immediately terminates the process it took place in. That is not the WCF behaviour, however.

If a service call on behalf of one client causes an exception, it must not be allowed to take down the hosting process. Other clients accessing the service, or other services hosted by the same process, should not be affected.

As a result, when an unhandled exception leaves the service scope, the dispatcher silently catches and handles it by serializing it in the returned message to the client. When the returned message reaches the proxy, the proxy throws an exception on the client side.

The service should throw an instance of the FaultException<T> class

[FaultContract(typeof(DivideByZeroException))] double Divide(double number1,double number2); //More methods

Page 22: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

22

WCF - Looking ahead

• SOAP and WS-* and the enterprise

• Web (or REST), the enterprise, and the rest

XML SOAPWS-*

Web

WCF in .NET Fx 3.0 WCF in .NET Fx 3.5

(Orcas)

Rss & Atom

Page 23: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

23

A Performance Comparison of WCF with Existing Distributed Communication Technologies

• ASP.NET Web Services (ASMX) : WCF is 25%—50% faster than ASP.NET Web Services

• Web Services Enhancements (WSE) : The throughput of WCF is nearly 4 times better than WSE. The main reason for this is that WSE uses the System.Xml.XmlDocument class to do message level parsing, thereby loading the full message into memory at once, while WCF uses a streaming System.Xml.XmlReader class that improves the performance significantly

• .NET Enterprise Services (ES) : .NET Enterprise Service is load dependant, as in one case WCF is nearly 100% faster but in another scenario it is nearly 25% slower.

• .NET Remoting: approximately 25% faster than .NET Remoting

http://msdn2.microsoft.com/en-us/library/bb310550.aspx

Page 24: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

24

DEMO-WCF

• Hello world• Using svcutil• Logging and debugging• Multiple hosting environments• Using VS2005 to generate proxy• Fault Tolerance• Web WCF Service• Atom / RSS WCF Service• Hosting WF in Service

Page 25: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

25

Q & A

• How are messages dispatched to an object?• How is the lifetime of that object controlled?• How does WCF work?• How do I extend WCF?• How do I control the threading model?

Page 26: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

26

Summary

[ServiceContract]

public interface IMyInterface

{

[FaultContract(typeof(MyFault)]

[OperationContract]

public void MyMethod(MyDataContract);

}

[ServiceBehavior(InstanceContextMode=Single]

public class MyService: IMyInterface

{

public void MyMethod(MyDataContract){…}

}

<service>

<endpoint name=“MyService”

address=“MyAddress”

binding=“netTcpBinding”

contract=“IMyInterface” />

Address +

Binding +

Contract =

Endpoint

Service

Contract

Definition

Service Type

[DataContract]

public class MyDataContract

{ [DataMember]

public string MyField;

}

Data

Contract

Definition

Page 27: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

27

References

• http://msdn2.microsoft.com/en-us/netframework/default.aspx• http://www.gotdotnet.com/

• Email: [email protected]• Blog: • Downloads @ blog

Page 28: Dot Net Training Wcf Dot Net35

© Copyright GlobalLogic 2007

28

Questions and Answers