WCF

WCF MSDN Docs

1. Web Service, WCF, Web API and WCF REST

Web Service
1. It is based on SOAP and return data in XML form.
2. It supports only HTTP protocol.
3. It is not open source but can be consumed by any client that understands xml.
4. It can be hosted only on IIS.

WCF
1. It is also based on SOAP and return data in XML form.
2. It is the evolution of the web service (ASMX) and supports various protocols like TCP, 3. HTTP, HTTPS, Named Pipes, MSMQ.
4. The main issue with WCF is, its tedious and extensive configuration.
5. It is not open source but can be consumed by any client that understands xml.
6. It can be hosted with in the applicaion or on IIS or using window service.

WCF Rest
1. To use WCF as WCF Rest service you have to enable webHttpBindings.
2. It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
3. To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
4. Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified
5. It support XML, JSON and ATOM data format.

Web API
1. This is the new framework for building HTTP services with easy and simple way.
2. Web API is open source an ideal platform for building REST-ful services over the .NET Framework.
3. Unlike WCF Rest service, it use the full featues of HTTP (like URIs, request/response headers, caching, versioning, various content formats)
4. It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
5. It can be hosted with in the application or on IIS.
6. It is light weight architecture and good for devices which have limited bandwidth like smart phones.
7. Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.

To whom choose between WCF or WEB API
1. Choose WCF when you want to create a service that should support special scenarios such as one way messaging, message queues, duplex communication etc.
2. Choose WCF when you want to create a service that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transport channels are unavailable.
3. Choose Web API when you want to create a resource-oriented services over HTTP that can use the full features of HTTP (like URIs, request/response headers, caching, versioning, various content formats).
4. Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.

2. Mention what are the main components of WCF?
Main components of WCF are
  • Service:  The working logic
  • Host: The path where the data is saved. E.g., .exe, process, windows service
  • Endpoints: The way the service is exposed to the outside world
3. Mention what is the endpoint in WCF and what are the three major points in WCF?
Every service must have an address that determines where the service is located, contract that defines what the service does and binding that tells how to communicate with the service.
  • Address: It specifies the location of the service which will be like http://Myserver/Myservice.  To communicate with our service client it will use this location
  • Contract: It specifies the interface between the server and client. It’s a simple interface with some attribute
  • Binding: It decides how two parties will communicate with each other in terms of transport and encoding and protocols
4. Explain how many types of contract does WCF defines?

Wcf contract specify the service and its operations. WCF has five types of contracts: service contract, operation contract, data contract, message contract and fault contract.

Service Contract
A service contract defines the operations which are exposed by the service to the outside world. A service contract is the interface of the WCF service and it tells the outside world what the service can do. It may have service-level settings, such as the name of the service and namespace for the service.
[ServiceContract]
interface IMyContract
{
 [OperationContract]

 string MyMethod();

}


class MyService : IMyContract

{

 public string MyMethod()

 {

 return "Hello World";

 }

}



Operation Contract

An operation contract is defined within a service contract. It defines the parameters and return type of an operation. An operation contract can also defines operation-level settings, like as the transaction flow of the op-eration, the directions of the operation (one-way, two-way, or both ways), and fault contract of the operation.

 [ServiceContract]

 interface IMyContract

 {

 [FaultContract(typeof(MyFaultContract))]
 [OperationContract]
 string MyMethod();
 }

Data Contract
A data contract defines the data type of the information that will be exchange be-tween the client and the service. A data contract can be used by an operation contract as a parameter or return type, or it can be used by a message contract to define elements.
[DataContract]
class Person
{
 [DataMember]
 public string ID;
 [DataMember]
 public string Name;
}
[ServiceContract]
interface IMyContract
{
 [OperationContract]
 Person GetPerson(int ID);
}

Message Contract
When an operation contract required to pass a message as a parameter or return value as a message, the type of this message will be defined as message contract. A message contract defines the elements of the message (like as Message Header, Message Body), as well as the message-related settings, such as the level of message security.
Message contracts give you complete control over the content of the SOAP header, as well as the structure of the SOAP body.
[ServiceContract]
public interface IRentalService
{
 [OperationContract]
 double CalPrice(PriceCalculate request);
}
[MessageContract]
public class PriceCalculate
{
 [MessageHeader]
 public MyHeader SoapHeader { get; set; }
 [MessageBodyMember]
 public PriceCal PriceCalculation { get; set; }
}
[DataContract]
public class MyHeader
{
 [DataMember]
 public string UserID { get; set; }
}
[DataContract]
public class PriceCal
{
 [DataMember]
 public DateTime PickupDateTime { get; set; }
 [DataMember]
 public DateTime ReturnDateTime { get; set; }
 [DataMember]
 public string PickupLocation { get; set; }
 [DataMember]
 public string ReturnLocation { get; set; }
 }

Fault Contract
A fault contract defines errors raised by the service, and how the service handles and propagates errors to its clients. An operation contract can have zero or more fault contracts associated with it.
[ServiceContract]
interface IMyContract
{
 [FaultContract(typeof(MyFaultContract1))]
 [FaultContract(typeof(MyFaultContract2))]
 [OperationContract]
 string MyMethod();
 [OperationContract]
 string MyShow();
 }

4. What are the transport schemas does WCF supports?
It supports
  • HTTP
  • TCP
  • Peer network
  • IPC ( Inter Process Communication)
  • MSMQ
5. Mention what are the ways of hosting a WCF service?
The ways of hosting a WCF service are
  • IIS
  • Self-Hosting
  • WAS (Windows Activation Service)
6. Mention what are the different instance modes in WCF?
To a particular service instance WCF binds an incoming message request, so the available modes are
7. What are the types of Data Contracts in WCF?
There are two types of Data Contracts
  • Data Contract: Attribute used to define the class
  • Data Member: Attribute used to define the properties
8. List out the types of binding available in WCF?
The types of binding available in WCF are
9. Name the namespace that is used to access WCF service?
System.ServiceModel is used to access WCF service
10. Explain what are the MEPs available in WCF?
MEP stand for Message Exchange Pattern, three types of message exchanged patterns are allowed.

11. List out the difference between XMLSerializer and the DataContractSerializer?

DataContractSerializer                           

For WCF, DataContractSerializer is the default serializer

Compare to XMLSerializer it is faster

It is used for simple schemes     

    

XMLSerializer

XMLSerializer is not a default serializer

XMLSerializer is slower

It is used for complex schemes
12. What is REST and what is the problem with WCF REST and how it can be resolved?
REST stands for Representational State Transfer, for designing network application REST is used. It relies on a stateless, client server, cacheable communications protocol.  The reason behind creating WCF is to support SOA and not REST. It requires a lot of configuration in order to create HTTP REST service using WCF.  To overcome this tedious task, ASP.NET web API was introduced.
13. Explain what is a Service Proxy in windows Communication Foundation?
In WCF, a service proxy enables applications to interact with WCF service by sending and receiving messages.  It’s a class that covers service details like service path, service implementation technology, platform and communication protocol and so on.  So, when the application interact the service through proxy, it gives the impression that it’s communicating a local object.
14.  Explain what is SOA?
SOA (Service Oriented Architectural) is a collection of services that determines how two computing entities will communicate with each other to achieve certain business functionality and also how one entity can work on behalf of another entity.

15. Transfer Security Mode in Wcf.


WCF offers the following transfer security modes to ensure a secured communication between a client and a server.

a. None

b. Transport

c. Message 

d. Mixed

e. Both 
example
<wsHttpBinding>
   <binding name="WCFSecurityExample">
      <security mode="None"/>
   </binding>
</wsHttpBinding>




16. Client credentials for message level security in WCF
a. Windows
b. Username
c. Certificate
d. issued Token
e. None
example
<netTcpBinding>
   <binding name="WCFMessageSecurityExample">
      <security mode="Message">
         <message clientCredentialType="None"/>
      </security>   
   </binding>
</netTcpBinding>

17. What are soap services? / What is soap service?

Simple Object Access Protocol (SOAP) is a XML based structured protocol specification to be used in the implementation of web service. In simple words SOAP web services uses XML as a mechanism for exchanging of information (one way) between the two programs (end points) over any transport layer protocol.
Though for message negotiation and transmission soap is not relies on any particular transport protocol but HTTP (Hyper Text Transfer Protocol) or SMTP (Simple Mail Transfer Protocol) are the two most popular transport protocols used with SOAP. Similarly SOAP is not relying on any particular programming language or operating system as far as they understand XML and SOAP message.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.