WCF

Q# 1: What is WCF? 

Ans:
WCF is a platform for building distributed businesses and deploying services among various endpoints in Windows. WCF was initially called “Indigo” and we can build service-oriented applications and provide interoperability.

WCF or Windows Communication Foundation is a programming model to create service oriented applications. It is used to create and deploy the service that is accessible to lots of different clients. It provides an environment where you can create a service which can be accessible to Windows clients as well as Linux clients or any others. It provides more features compared to web services.

WCF is a Microsoft technology to create service oriented application. Before the WCF, the Web Service was used to create services but that type of service is only accessible to Windows client hosted on HTTP protocol. But WCF services are accessible with different protocols like http, tcp, msmq, etc. A few sample scenarios include:

Q# 2: Explain WCF Architecture and also explain its Fundamentals?

Ans:
Windows Communication Foundation (WCF) is a framework for building service-oriented applications by which we can send asynchronous message/data from one service endpoint to another service endpoint.

WCF is a runtime and a set of APIs for creating systems that sends messages between services and clients. The same infrastructure and APIs are used to create applications that communicate with other applications on the same computer system or on a system that resides in another company and is accessed over the Internet.

The WCF fundamentals are as follows:

1-Unification
  • COM+ Services
  • Web Services
  • .NET Remoting
  • Microsoft Message Queuing


2-Interoperability
3-Service Orientation

Q# 3: Why Should We Use WCF Service?

Ans:
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Most of WCF functionality is included in a single assembly called System.ServiceModel.dll, located in the System.ServiceModel namespace.

Why WCF
  • A web service to exchange messages in XML format using HTTP protocol for interoperability.
  • A remoting service to exchange messages in binary format using TCP protocol for performance.
  • A secure service to process business transactions.
  • A service that supplies current data to others, such as a traffic report or other monitoring service.
  • A chat service that allows two people to communicate or exchange data in real time.
  • A dashboard application that polls one or more services for data and presents it in a logical presentation.
  • A Silverlight application to poll a service for the latest data feeds.

Q# 4: What are the features and advantage of WCF?

Ans:
Features of WCF
Windows Communication Foundation (WCF) is a secure, reliable, and scalable messaging platform for the .NET Framework 3.0,

  1. Service Orientation
  2. Interoperability
  3. Multiple Message Patterns
  4. Service Metadata
  5. Data Contracts
  6. Security
  7. Multiple Transports and Encodings
  8. Reliable and Queued Messages
  9. Durable Messages
  10. Transactions
  11. AJAX and REST Support
  12. Extensibility

Advantages of WCF
  1. Service Oriented
  2. Location Independent
  3. Language Independent
  4. Platform Independent
  5. Support Multiple operation
  6. WCF can maintain transaction like COM+ Does
  7. It can maintain state
  8. It can control concurrency
  9. It can be hosted on IIS, WAS, Self hosting, Windows services.
  10. It has AJAX Integration and JSON (JavaScript object notation) support.

  • WCF can be configured to work independently of SOAP and use RSS instead.
  • WCF is one of the fastest communication technologies and offers excellent performance compared to other Microsoft specifications.
  • To improve communication, transmission speed needs to be optimized. This is done by transmitting binary-coded XML data instead of plain text to decrease latency.
  • Object life-cycle management and distributed transaction management are applicable to any application developed using WCF.


Q# 5: What is the difference between WCF and Web services?

Ans: 
WCF:Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another.
Web Services: A Web Service is programmable application logic accessible via standard web protocols. One of these web protocols is the Simple Object Access Protocol (SOAP). SOAP is a W3C submitted note (as of May 2000) that uses standards based technologies (XML for data description and HTTP for transport) to encode and transmit application data.

Features Web Service WCF
  • Hosting It can be hosted in IIS It can be hosted in IIS, windows activation service, Self-hosting, Windows service
  • Programming [WebService] attribute has to be added to the class [ServiceContraact] attribute has to be added to the class
  • Model [WebMethod] attribute represents the method exposed to client [OperationContract] attribute represents the method exposed to client
  • Operation One-way, Request- Response are the different operations supported in web service One-Way, Request-Response, Duplex are different type of operations supported in WCF
  • XML System.Xml.serialization name space is used for serialization System.Runtime.Serialization namespace is used for serialization
  • Encoding XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom XML 1.0, MTOM, Binary, Custom
  • Transports Can be accessed through HTTP, TCP, Custom Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
  • Protocols Security Security, Reliable messaging, Transactions



Q#6: Explain what is SOA? Are web-services SOA?

Ans: 

SOA : SOA stands for service oriented architecture. Service Oriented Architecture is an architectural approach in software development where the application is organized as "Services". Services are a group of methods that contain the business logic to connect a DB or other services. For instance you go to a hotel and order food. Your order first goes to the counter and then it goes to the kitchen where the food is prepared and finally the waiter serves the food.

Some important characteristics of Service Oriented Architecture
  • SOA services should be independent of other services. 
  • Altering a service should not affect the client calling the service. 
  • Services should be self-contained. 


  1.  Services should be able to define themselves (in the Web Service Description Language (WSDL)). Services should be able to describe what they do. It should be able to tell the client what all of the operations it does, what are all the data types it uses and what kind of value it will return.
  2. SOA Overview: A Service Oriented Architecture is based on four key abstractions.
  3. An Application Front End
  4. A Service
  5. A Service Repository
  6. A Service Bus
  7. Application Front End: The application front end is decoupled from the services. Each service has a contract that defines what it will do and one or more interfaces to implement the contract.
  8. A Service: It has the methods with the defined contracts and the implementation of the business logic to connect the DB or other service.

  9. A Service Repository: The service repository provides a home for the services and the service bus provides an industry-standard mechanism for connecting to and interacting with the services.

Are web-services SOA?

SOA is thinking, it's an architectural concept and web service is one of the technical approach to complete it. Web services are the preferred standards to achieve SOA.

In SOA we need the services to be loosely coupled. A web service communicates using SOAP protocol which is XML based which is very loosely coupled. SOA services should be able to describe themselves. WSDL describes how we can access the service.
SOA services are located in a directory. UDDI describes where we can get the web service. This nothing but implementation of SOA registry.

Q# 7:What is Service Contract in WCF?
Ans: Service contract means the collective mechanisms by which a service's capabilities and requirements are specified for its consumers. We must say that it defines the operations that a service will perform when executed. It tells more things about a service, like message data types, operation locations, the protocols the client will need in order to communicate with the service.

To create a service contract you define an interface with related methods representative of a collection of service operations, and then decorate the interface/class with the ServiceContract Attribute to indicate it is a service contract. Methods in the interface that should be included in the service contract are decorated with the OperationContract Attribute.

How to define a Service Contract

Service Contract

ServiceContractAttribute: It is used to declare the type as a Service Contract. It can be declared without any parameters but it can also take named parameters.

[ServiceContract(Name="MyService", Namespace="http://tempuri.org")]
public interface IMyService
{
   [OperationContract]
   int AddNum(string numdesc, string assignedTo);
}
OperationContractAttribute: It can only be applied on methods. It is used to declare methods which belong to a Service Contract. It controls the service description and message formats.

Q# 8: What are Contracts in WCF? How many Contracts are in WPF?

Ans: The main use of contracts is to allow the client and services agree as to the types of operations and structures they will use during the communication process. It also shows the formal agreements between client and service to define a platform-neutral and standard for describing that what the service does.

contracts in WCF

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.

Operation Contract:

An Operation Contract defines the method exposed to the client to exchange the information between the client and server. An Operation Contract describes what functionality is to be given to the client, such as addition, subtraction and so on.

It can be defined as in the following:
public interface IService1
{
    [OperationContract]
    string GetData(int value);
    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}
Data Contract:

Data Contracts define the data type for variables that are the same as get and set properties but the difference is that a Data Contract in WCF is used to serialize and deserialize the complex data. It defines how data types are serialized and deserialized. Using serialization, you can convert an object into a sequence of bytes that can be transmitted over a network. Using de-serialization, you reassemble an object from a sequence of bytes that you receive from a calling application.

Example:

DataContract
public class Student
{
    private string _Name;
    private string _City;
    [DataMember]
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
        }
    }
}
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.

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.

The following is the syntax to raise the custom error in WCF:
[ServiceContract]
public interface IGetDetailsService
{
    [OperationContract]
    [FaultContract(typeof (Student))]
    Student GetDetails(string Name);
}
[DataContract]
public class Student
{
    private string _Name;
    private string _City;
    [DataMember]
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
        }
    }
    [DataMember]
    public string City
    {
        get
        {
            return _City;
        }
        set
        {
            _City = value;
        }
    }
}

Q# 9: What is Data Contract in WCF?
Ans:A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.

Data contract can be explicit or implicit. Simple type such as int, string etc has an implicit data contract. User defined object are explicit or Complex type, for which you have to define a Data contract using [DataContract] and [DataMember] attribute.

A data contract can be defined as follows:
  • It describes the external format of data passed to and from service operations.
  • It defines the structure and types of data exchanged in service messages.
  • It maps a CLR type to an XML Schema.
  • It defines how data types are serialized and deserialized. Through serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Through deserialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
  • It is a versioning system that allows you to manage changes to structured data.

data contract 
[DataContract] 
public class CuboidInfo 

}


Q# 10: What is Message Contract in WCF?

Ans:
A Message Contract is used to control the structure of a message body and serialization process. It is also used to send / access information in SOAP headers. By default WCF takes care of creating SOAP messages according to service DataContracts and OperationContracts.

SOAP  

A MessageContract controls the structure of a message body and serialization process. A MessageContract can be typed or untyped and are useful in interoperability cases and when there is an existing message format we must comply with.

The SOAP header is implemented in the namespace system.web.services.protocol.
[MessageContract]
public class AutherRequest
{
   public string AutherId;
}
Message Header

A Message Header is applied to a member of a message contract to declare the member within the message header.
[MessageContract]
public class AutherRequest
{
   [MessageHeader]
   public string AutherId;
}
Message Body Member

A Message Body Member is applied to the member of a message contract to declare the members within the message body.
[MessageContract]
public class AuthorResponse
{
    [MessageBodyMember]
    public Auther Obj;
}

Q# 11: What is Fault Contract in WCF?
Ans:
Fault Contract provides documented view for error accorded in the service to client. This help as to easy identity the what error has occurred, and where. By default when we throw any exception from service, it will not reach the client side. The less the client knows about what happened on the server side, the more dissociated the interaction will be, this phenomenon (not allowing the actual cause of error to reach client) is known as error masking. By default all exceptions thrown on the service side always reach the client as FaultException, as by having all service exceptions indistinguishable from one another, WCF decouples the client from service.

Syntax

[OperationContract]
[FaultContract(typeof(MyException))]
string getDetails(int value);

Q# 12: What is End Points and how many types of End points?

Ans:
Endpoints provide the configuration required for the communication and create the complete WCF service application.

An Endpoint is a piece of information that tells WCF how to build the runtime communication channels to send and receive messages. An endpoint consists of the three things address, binding and contract.

Fault Contract in WCF

All communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service. Endpoints provide clients access to the functionality offered by a WCF service.

Each endpoint consists of four properties:

  • An address that indicates where the endpoint can be found.
  • A binding that specifies how a client can communicate with the endpoint.
  • A contract that identifies the operations available.



Each endpoint consists of the following:

A set of behaviors that specify local implementation details of the endpoint.

Address: The address uniquely identifies the endpoint and tells potential consumers of the service where it is located. It is represented in the WCF object model by the EndpointAddress class. An EndpointAddress class contains:


  • A Uri property, which represents the address of the service.
  • An Identity property, which represents the security identity of the service and a collection of optional message headers. The optional message headers are used to provide additional and more detailed addressing information to identify or interact with the endpoint.


Binding: The binding specifies how to communicate with the endpoint. This includes:

  • The transport protocol to use (for example, TCP or HTTP).
  • The encoding to use for the messages (for example, text or binary).
  • The necessary security requirements (for example, SSL or SOAP message security).


Contracts: The contract outlines what functionality the endpoint exposes to the client. A contract specifies:

  • What operations can be called by a client.
  • The form of the message.
  • The type of input parameters or data required to call the operation.
  • What type of processing or response message the client can expect.



Q# 13: What is Address in WCF?

Ans: Address - Where - Where to send messages

An Address is a unique Uniform Resource Locator (URI) that identifies the location of the service. It defines the network address for sending and receiving the messages. It is divided into four parts:

What is Address in WCF

In WCF every service are associated with unique address. The address provided two imported elements the location of services and the transport protocol which will help to communicate with the services .WCF support the following transport schemes. Here is the answer for what are the supported transport protocols in WCF.

  • HTTP/HTTPS
  • TCP
  • IPC
  • Peer Network
  • MSMQ
  • Service BUS

[transport]://[machine or domain][:optional port].
ex: https://localhost:82/MyService

Q# 15: What is Binding in WCF?What are the types of binding?

Answer: Binding describes how client will communicate with service. There are different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on your requirements.

A binding has several characteristics, including the following:

Transport - Defines the base protocol to be used like HTTP, Named Pipes, TCP, and MSMQ are some type of protocols.

Encoding (Optional) - Three types of encoding are available-Text, Binary, or Message Transmission Optimization Mechanism (MTOM). MTOM is an interoperable message format that allows the effective transmission of attachments or large messages (greater than 64K).

Protocol(Optional) - Defines information to be used in the binding such as Security, transaction or reliable messaging capability.
The following table gives some list of protocols supported by WCF binding.

  • Binding Description
  • BasicHttpBinding Basic Web service communication. No security by default.
  • WSHttpBinding Web services with WS-* support. Supports transactions.
  • WSDualHttpBinding Web services with duplex contract and transaction support.
  • WSFederationHttpBinding Web services with federated security. Supports transactions.
  • MsmqIntegrationBinding Communication directly with MSMQ applications. Supports transactions.
  • NetMsmqBinding Communication between WCF applications by using queuing. Supports transactions.
  • NetNamedPipeBinding Communication between WCF applications on same computer. Supports duplex contracts and transactions
  • NetPeerTcpBinding Communication between computers across peer-to-peer services. Supports duplex contracts
  • NetTcpBinding Communication between WCF applications across computers. Supports duplex contracts and transactions



No comments:

Post a Comment