You are on page 1of 19

Windows Communication

Foundation (WCF)

Agenda

Introduction of Remoting and Web services


Introduction of WCF
When you should use it.
What software you need to create a WCF service
Advantages of WCF
Architecture of WCF
Programming with WCF
Services (Service Execution Boundaries, WCF & Location Transparencies )
Contracts (Service, Data, Fault and Message)
Addresses (TCP, HTTP, IPC, MSMQ and P2P)
Hosting (IIS, Self, WAS,Windows)
Bindings
End Points
Example of WCF & Implementations

Introduction of WCF

WCF is a set of .NET technologies (Web services ,.Net


Remoting and enterprises services) for building and running
connected systems.

WCF provides secure, reliable, and transacted messaging


along with interoperability

WCF applications can be developed in any language which can


target the .NET runtime

System.ServiceModel is the assembly that contains core


functionality for WCF.

When you should use it.

When your business logic has to interact with a variety of client


applications.

When client apps, which are going to use your service, may be
written in Java or .Net.

You are targeting a distributed computing architecture.

Softwares required to create WCF

Visual studio Net Framework 3.0

Microsoft Windows Software Development Kit (SDK)

Visual studio extensions for WCF .

Advantages of WCF

Makes UI programming & distributed programming very easy.

Reduce complexity by allowing us to focus on single programming


model rather than learn multiple programming models.

Helps us talk to various applications written in various languages with


ease which means more probable revenues as various applications can
start using your core services with ease.

Helps us forget about interoperability between various underlying web


service technologies in the past, present and future.

With WCF, a single service can be defined and exposed over multiple
endpoints to support multiple protocols at the same time.

WCF Architecture Overview

Services in WCF
The WCF applications expose the functionality through services. A Service
encapsulate business functionality and exposes a set of methods that can be
accessed by remote clients.

When applied to a class, the class becomes a service type. When applied to an
interface, any class that implements the interface becomes a service type.

In either case, methods exposed by the class or interface must be decorated


with the OperationContractAttribute to be considered part of the
servicecontract. Methods with this attribute are considered service operations.

Addresses
The address provides two important elements: the location of the
service and the transport protocol or transport schema used to
communicate with the service.
a) TCP Addresses
Ex. net.tcp://localhost:8002/MyService.
b) HTTP Addresses
Ex. http://localhost:8001
c) IPC Addresses
Ex. net.pipe://localhost/MyPipe
d) MSMQ Addresses
Ex. net.msmq://localhost/MyService

Contracts
Types of contracts:

a) Service Contracts
Describe which operations the client can perform on the service.

b) Data Contracts
Define which data types are passed to and from the service.

c) Fault Contracts
Define which errors are raised by the service, and how the service handles and
propagates errors to its clients.

d) Message Contracts
Allow the service to interact directly with messages.

Hosting
WCF Service Hosting:

IIS Hosting
Hosting in IIS is very similar to hosting a classic ASMX web service. You need to
create a virtual directory under IIS and supply a .svc file.
Support only HTTP protocol.

Self Hosting
Self-hosting is the name for the technique used when the developer is responsible
for providing and managing the life cycle of the host process

Windows Activation Service


Windows Activation service is a system service available with Windows vista and
windows server 2008.
Supports Http, TCP and named pipes

Bindings
WCF groups together a set of communication aspects in bindings. A binding is
merely a consistent, canned set of choices regarding the transport protocol,
message encoding, communication pattern, reliability, security, transaction
propagation, and interoperability.
WCF defines nine standard bindings:

Basic Binding

TCP Binding

P2P Binding

IPC Binding

WS Binding

Federated WS Binding

Duplex WS Binding

MSMQ Binding

MSMQ Integration Binding

List of bindings :

Endpoints
The endpoint is the fusion of the address, contract, and binding (ABC).

Address :- The address is obviously the location of the service

Binding:- The binding specifies security options, encoding options, and


transport options.

Contract:- The contract is the actual interface that the service implements.

Every endpoint must have all three elements, and the host exposes the endpoint.
Logically

Every service must expose at least one business endpoint and each endpoint
has exactly one contract. All endpoints on a service have unique addresses, and
a single service can expose multiple endpoints. These endpoints can use the
same or different bindings and can expose the same or different contracts..

Sample WCF

Class
public class Employee
{
public int Id { get; set; }
public string Email {get;set;}
public List<Employee> GetEmpList()
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee() { Id = 1, Email="Arman@somemail.com"});
empList.Add(new Employee() { Id = 1, Email = "Arnab@somemail.com" });
return empList;
}
}

Interface
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
List<Employee> FetchEmailList(string mail);
}
Interface Implementation
public class EmployeeService : IEmployeeService
{
public List<Employee> FetchEmailList(string mail)
{
var emp = new Employee();
var fetchEmail = emp.GetEmpList()
.Where(m => m.Email.ToLower().StartsWith(mail.ToLower()));
return fetchEmail.ToList();
}
}

$(function () {
$(".tb").autocomplete({
source: function (request, response) {
$.ajax({
url: "EmployeeList.svc/FetchEmailList",
data: "{ 'mail': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
response($.map(data.d, function (item) {
return {
value: item.Email
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 2
});

<system.serviceModel>
<services>
<service name="Service">
<endpoint address="soap" contract="IService" binding="basicHttpBinding"/>
<endpoint address="rest" contract="IService" binding="webHttpBinding"
behaviorConfiguration="restBehavior"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

Thank You

You might also like