Friday, 13 May 2016

WCF Basics :

WCF basic concepts :

SOAP

  • Simple Object Access Protocol
  • HTTP/HTTPS/SMTP
  • Only XML
  • web service security (ws-security)
  • WSDL (Web Services Description Language), XSD (XML Schema definition)

REST

  • Representational State Transfer
  • HTTP/HTTPS
  • text, JSON, XML
  • No ws-security
  • No WSDL, XSD

Interface has below annotation:
[ServiceContract]
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]

Only those methods are interacted outside which has annotation [OperationContract]
[OperationContract] => methods in interface
[OperationContract(IsOneWay=true)] => not wait for response

Below annotation is used to invoke methods:
[WebInvoke(Method="GET/POST/PUT/DELETE",
RequestFormat=WebMessageFormat.Json/Xml,
ResponseFormat=WebMessageFormat.Json/Xml,
UriTemplate="MethodName/{ParameterName}")]

To return fault, use fault contract:
[FaultContract(typeof(InvalidOperationException))]

Add below annotation on your data contract class:
[DataContracrt] => data class

Only those properties are interacted outside, which has [DataMember] annotation.
[DataMember] => property in class

Message contracts are rarely used.
[MessageContract] //Message Contract
[MessageHeader]
[MessageBodyMember]


Various binding protocols:

Example of different types of bindings supported by WCF.

<add binding="basicHttpsBinding" scheme="https" /> => For SOAP service
<add binding="wsHttpsBinding" scheme="https" /> => For SOAP service (Web service security)
<add binding="webHttpBinding" scheme="http" /> => For REST service

Custom Binding configuration

Service endpoint contains 3 parameters:

  1. address: URL address of service
  2. binding: binding type
  3. contract: Interface name
Here we have created custom binding configuration "LargeWeb" for webHttpBinding.
And assign that "LargeWeb" binding configuration in services section.

<bindings>
  <webHttpBinding>
    <binding name="LargeWeb"
             maxBufferPoolSize="1500000"
             maxReceivedMessageSize="1500000"
             maxBufferSize="1500000"
    openTimeout="00:10:00"
             closeTimeout="00:10:00"
             sendTimeout="00:10:00"
             receiveTimeout="00:10:00">
      <readerQuotas
            maxArrayLength="656000"
            maxBytesPerRead="656000"
            maxDepth="32"
            maxNameTableCharCount="656000"
            maxStringContentLength="656000"
            />
    </binding>
  </webHttpBinding>
</bindings>

<services>
    <service name="--service name--" // service name
      behaviorConfiguration="longTimeoutBehavior">
      <endpoint address="http://localhost:8080/people" // service address using which service can be accessed
        binding="webHttpBinding"
Contract="MyService.Contracts.Ipeople" // Interface address with namespace
bindingConfiguration="LargeWeb" /> // bindingConfiguration name defined in <bindings> section
    </service>
</services>

=====================================================

<system.web>
  <httpRuntime maxRequestLength=”4000″
    enable = “True”
    requestLengthDiskThreshold=”512
    shutdownTimeout=”90″
    executionTimeout=”110″
    versionHeader=”1.1.4128″/>
</system.web>

=====================================================
In web.config file , make includeExceptionDetailInFaults="false" to hide exception details
from user.

<serviceDebug includeExceptionDetailInFaults="false"/> // true for devloper's build and false for production build

SOAP Service Message Example

POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 299
SOAPAction: "http://www.w3.org/2003/05/soap-envelope"

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <m:GetStockPrice xmlns:m="http://www.example.org/nilav">
      <m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>

No comments:

Post a Comment

Parent-Child class declaration and initialization

using System; namespace CSharpDemo {     public class A     {         public void print()         {             Console.Wr...