Understanding the Basics of WCF:
Well, while developing the WCF Service, there should be place where we specify the Service URL, mode of connectivity & list of accessible functionality of that services.
This placeholder is Endpoint. So in the service program, we expose a collection of Endpoints. Each Endpoint is a portal for communicating with the client.
i mean when a cleint refers any service from thier application, these endpoints data gets loaded in thier application configuration file.
All the WCF communications are take place through end point. End point consists of three components.
Address : Basically URL, specifies where this WCF service is hosted .Client will use this url to connect to the service. e.g http://localhost:8090/MyService/SimpleCalculator.svc
Binding : It will 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
|Place to specify all binding types
Contract : Collection of operation that specifies what the endpoint will communicate with the client.Usually name of the Interface will be mentioned in the Contract, so the client application will be aware of the operations which are exposed to the client.Each operation is a simple exchange pattern such as one-way, duplex and request/reply.
<dns value=“localhost“ />
<serviceMetadata httpGetEnabled=“true“ />
<dataContractSerializer maxItemsInObjectGraph=“2147483647“ />
<serviceDebug includeExceptionDetailInFaults=“true“ />
BehaviorConfiguration is a reference to a seperate section – ‘Service Behaviours’Now comes the “behaviorConfiguration” & “bindingConfiguration” && what are these & what for???
We can add “behaviorConfiguration” to Service tag, to specify further more configurations to service, ie..whether to allow MetaData to be published. This will essentially create a WSDL which describes the service.(<serviceMetadata httpGetEnabled=”true”/>)
BindingConfiguration is to add extra settings to binding. Ie MaxTime taken to bind, Max data allowed etc.
It is a also specified under seperate section – <Bindings>
- In WCF, all services are exposed as contracts.
- Contract is a platform-neutral and standard way of describing what the service does.
Service Contract :
- Service contracts describes the operation that service can provide
- It is basically a interface which specifies all the operations that service can provide.Internaly operations has to be specified using OperationContract attribute to qualify as a service member.
- This is been implemented by SVC file to define the concrete implementation of the operations.
Operation Contract : The operations exposed by the service has to be specified using this attribute.
- Data contract describes the custom data type which is exposed to the client. basically inputs & outputs to service.
- Data types like int, string are identified by the client because it is already mention in XML schema definition language document, but custom created class or data types cannot be identified by the client e.g. Employee data type. By using DataContract we can make client to be aware of Employee data type that are returning or passing parameter to the method.
- So whoever DataContract is specified, be part of metadata in WSDL.
- Default SOAP message format is provided by the WCF runtime for communication between Client and service. If it is not meeting your requirements then we can create our own message format. This can be achieved by using Message Contract attribute.
- When we go for this???
Fault Contract :
- Suppose there could be a exceptional scenario in a service side, i would like to throw a exception.But Exception should be a platform neutral. So when u throw a .net Exception, client may/may not be aware of the Exception. So in order to communicate the Exception to client, we go for Fault Contract.
- Step 1: Create a Class which stores a exception details and ur are going to throw this to client. So this has to be attributed as Data Contract.
- Step 2: Operation Contract which suppose to throw the Exception has to be specified with Fault Contract & has to specify the type of fault(this is the class which u throw)
Throw will be like (throw new FaultException(ex,”Reason: Testing the Fault contract”) 😉
- Step 3: Since client knows the method with Fault Contract, and there they can Catch the exception using (FaultException<MyCalculatorService.CustomException>)