Professional Documents
Culture Documents
Foundation (WCF)
Agenda
Introduction of WCF
When client apps, which are going to use your service, may be
written in Java or .Net.
Advantages of WCF
With WCF, a single service can be defined and exposed over multiple
endpoints to support multiple protocols at the same time.
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.
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
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
List of bindings :
Endpoints
The endpoint is the fusion of the address, contract, and binding (ABC).
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