copyright ©2004 virtusa corporation | confidential introduction to windows communication foundation...
TRANSCRIPT
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Introduction toWindows Communication Foundation
Ruwan Wijesinghe
2Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Windows Communication Foundation
• Provide a unified communication model for all windows based distributed application development technologies (ASMX web services, .Net Remoting, Enterprise Services, MSMQ)
• Fully Support WS-I and hence interoperable with applications developed in other platforms
• Support transport mediums like • TCP – Efficient intranet communication• HTTP – For communication over the fire walls (Internet) • Named Pipes – For efficient inter process communication• MSMQ – Reliable messaging
• Support three messaging patterns• Simplex – Send a message and do not expect a reply• Duplex – Send a message with a callback interface. Service can replies
asynchronously using this callback interface, if required.• Request Reply – Send a message and expect a reply
3Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Hosting Services
• Internet Information Server (IIS) – • Supports, clustering, domain recycling, etc.
• IIS 6 supports only HTTP
• IIS7 (WAS) supports any communication protocol
• Windows Executables – Windows and Console applications can host services as s means of inter-process communication
• Windows Services – Windows services can be used to host WCF services. This can be used as a means of inter-process communication as well as for distributed application development
4Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Service Endpoint
ServiceEndpoints contain the ABCs of a service.
• EndpointAddress (A): A WCF service endpoint has a URI. EndpointAddress contains the URI, optional Address Headers and an optional Identity field (only useful when having multiple end points that shares the same URI)
• Binding (B): Bindings are a portion of the Policy that a service and consumer have to agree on if they want to exchange messages. This binding will contain the information to support the features being exposed by the service.
• ContractDescription (C): There are a number of different types of contracts with WCF. The primary contract that defines service is the service contract. This is analogous to the portType within WSDL today.
5Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Binding
• Binding contains following information
• Name – Name of the binding
• Namespace – The namespace that makes the binding name unique
• BindingElements – A list of binding elements that defines different aspects of the expected binding
6Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Exactly what does the binding tell us?
Bindings are capable of answering a number of questions about how a client and a service will communicate. Here are the key questions that are answered.
•What level of interoperability does this service have? • .NET to .NET or WS* specifications.
•What type of encoding is used? • Text, MTOM or Binary
•What type of transports can be used? • TCP, HTTP, Named Pipe, or MSMQ.
•What type of messaging patterns can be used? • Simplex, duplex, or request-reply.
•What type of security is supported? • Windows security, WS-Security, or transport level security.
•Is transaction flow supported? •Are reliable sessions supported?
7Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Binding Elements
• A binding is a set of Binding Elements
• Types of Binding Elements• Transport Bindings – Binding which determines the transport
type (e.g. TCP, HTTP, MSMQ, etc.)
• Protocol Binding – The binding which process SOAP headers and implement protocols like (WS-Security, etc.)
• Example Bindings
Security Binding Element
ReliableSession Binding Element
TcpTransport Binding Element
8Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Contract Description
• ContractDescription contains the following parts
• Name – Name of the service
• Namespace – The namespace that makes the service name unique
• OperationDescription – Descriptions of the operations (methods) of the service
• Message Description – Descriptions of the input and output messages of the service
• ContractBehavior – ContractBehaviors modifies or extend the behavior of the service contract
9Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Service Description
• Services are described using ServiceDescriptions
• ServiceDescripion contsins
• ServiceType – The type that provide the service
• IServiceBehavior – The modifications or extensions to the service
• E.g. ServiceMetadataBehavior determines if the service publishes its metadata
• ServiceEndpoint – A set of ServiceEndpoints as discussed in previous slides (EndpointAddress, Binding and Contract)
10Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Channels
• Channels are used by clients to communicate with services
• There are two types of channels• Transport Channels – Channels used to support transport mediums like TCP
• Protocol Channels – Channles used to support SOAP based protocols like WS-Security
• Generally a set of channels are used to access a given service endpoint
• Channels are described by ChannelDescriptions which contains,
• IChannelBehavior – Set of modifications or extensions to the channel behavior
• ServiceEndpoint – Service endpoints as discussed earlier
11Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Service Configuration
• A configuration file entry of a service hosted in IIS or WAS is given below
<configuration> <system.serviceModel> <services> <service type="ServiceLibrary.EchoService"> <endpoint address="svc" binding="basicHttpBinding" contract="ServiceLibrary.IEchoService"/> <endpoint address="net.tcp://localhost:8081/echo/svc" binding="netTcpBinding" contract="ServiceLibrary.IEchoService"/> </service> </services> </system.serviceModel> </configuration>
12Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Predefined Bindings
.Binding
Interop Encoding Transport Messaging Security Transaction Reliable
BasicProfile Basic Text HTTP, HTTPS
Request-reply, Simplex
Transport No No
WSProfile WS* Text, MTOM HTTP, HTTPS
Request-reply, Simplex
Transport, WS-Security
Yes Yes
WSProfileDualHTTP
WS* Text, MTOM HTTP Request-reply,
Simplex, Duplex
WS-Security Yes Yes
NETProfileTcp .NET Binary TCP Request-reply, Simplex
Transport, WS-Security
Yes Yes
NetProfileDualTcp .NET Binary TCP Request-reply,
Simplex, Duplex
WS-Security Yes Yes
NetProfileNamedPipe
.NET Binary Named pipe Request-reply,
Simplex, Duplex
Transport Yes No
NetProfileMsmq .NET Binary MSMQ Simplex Transport, WS-Security
Yes No
MsmqIntegration MSMQ Text MSMQ Simplex - Yes Yes
Intermediary - - HTTP, TCP, named pipe
- - - -
13Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Instance Modes
• Single – Use a single instance to serve all the requests
• PerSession – New instance will be created per session
• PerCall – New instance will be created per request (method call)
• Shared – Instances will be pooled and shared
14Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Service Interface
[ServiceContract(Name="EventsService)]
public interface IEventsService
{
[OperationContract(Name = "SaveEvent")]
void SaveEvent(LinkItem item);
[OperationContract(Name = "GetEvent")]
LinkItem GetEvent();
}
15Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Implementing Interface
public class EventsService: IEventsService{ private LinkItem m_linkItem;
public void SaveEvent(LinkItem item) {
m_linkItem = item; }
public LinkItem GetEvent() {
return m_linkItem; }}
16Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Data Contracts
[DataContract(Name="LinkItem")]public class LinkItem { [DataMember(Name="Id", IsRequired=false, Order=0)] private long m_id; [DataMember(Name = "Title", IsRequired = true, Order = 1)] private string m_title; [DataMember(Name="Desc", IsRequired = false, Order = 2)] private string m_desc;
public long Id { get { return m_id; }set { m_id = value; }
} public string Title { get { return m_title; } set { m_title = value; } } public string Description { get { return m_description; } set { m_description = value; } }}
17Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Message Contracts
[MessageContract]public class SaveEventRequest{ private string m_licenseKey; private LinkItem m_linkItem;
[MessageHeader(MustUnderstand=true)] public string LicenseKey { get { return m_licenseKey; } set { m_licenseKey = value; } }
[MessageBody(Order=0)] public LinkItem LinkItem { get { return m_linkItem; } set { m_linkItem = value; } }}
18Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Service Interface with Messages
[ServiceContract(Name="EventsService")]
public interface IEventsService
{
[OperationContract(Name = "SaveEvent")]
SaveEventResponse SaveEvent(SaveEventRequest message);
[OperationContract(Name = "GetEvent")]
GetEventResponse GetEvent(GetEventRequest message);
}
19Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Hosting a Service in an Executable
• Services can be hosted in any executable
• These services can be configured• Directly by code• Using a configuration file (recommended)
• Configuring using code
ServiceHost sh = new ServiceHost(typeof(MathService)); sh.AddServiceEndpoint(
typeof(IMath), //contract type
new WSHttpBinding(), //one of the built-in bindings "http://localhost/MathService/Ep1" //the endpoint's address);
sh.Open(); //create and open the service runtime
20Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
WCF Clients
WCF can be written by using two techniques
• Direct CodeChannelFactory<IMath> factory = new ChannelFactory<IMath>(
new WSHttpBinding(),new
EndpointAddress("http://localhost/MathService/Ep1"));IMath channel = factory.CreateChannel();int result = channel.Add(35,7);factory.Close();
• Using ProxyMathProxy proxy = new MathProxy();int result = proxy.Add(35, 7);
21Copyright ©2004 Virtusa Corporation | CONFIDENTIAL
Loggin Messages
• WCF can be configured to log its incoming and outgoing messages
• SvcTraceViewer tools can be used to analyze this data