articles

Home / DeveloperSection / Articles / WCF (Windows Communication Foundation) – Overview and Sample Code in C# .NET

WCF (Windows Communication Foundation) – Overview and Sample Code in C# .NET

WCF (Windows Communication Foundation) – Overview and Sample Code in C# .NET

Uttam Misra 24543 27-Aug-2010
What Is Windows Communication Foundation (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. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data. A few sample scenarios include:
  • 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.
  • Exposing a workflow implemented using Windows Workflow Foundation as a WCF service.
  • A Silverlight application to poll a service for the latest data feeds.
While creating such applications was possible prior to the existence of WCF, WCF makes the development of endpoints easier than ever. In summary, WCF is designed to offer a manageable approach to creating Web services and Web service clients.
  • It combines functionalities of Asp.Net Web services, remoting, MSMQ and Enterprise Services.
  • It’s more reliable and secured then Web services.
  • All the major changes can be done in the configuration file instead of code file.
What are the differences between WCF and Web Service Attributes
  • WCF has [ServiceContract] attribute attached with the class.
  • WS has [WebService] attribute attached with the class. 
  • WCF has [OperationContract] attribute attached with methods. 
  • WS has [WebMethod] attribute attached with methods.
  • WCF can be hosted on IIS, WAS, of self-hosting.
  • WS can be hosted on IIS.
XML :
  • WCF uses using System.Runtime.Serialization; namespace for serialization. 
  • WS uses using System.Xml.Serialization; namespace for serialization.
Transports :
  • WCF support HTTP, TCP, MSMQ, P2P transport protocols. 
  • WS support Http protocol.
Encoding :
  • WCF supports XML, MTOM (Messaging Transmission Optimization Mechanism), Binary. 
  •  WS supports XML, MTOM, DIME (Direct Internet Message Encapsulation).
Security :
  • WCF provides reliable security for messaging and transactions over net.
  • WS doesn’t provide method level security & message delivery isn’t assured & can be lost without acknowledgement.
Few Important points in WCF
Address :
WCF service location that client can use to expose its features
Binding
Configuration for Transport protocol, security and encoding mechanism.
Contract
It defines type of data and type of operations, a client application can use from WCF service.
Service Contract in WCF
It has 2 attributes : 
[ServiceContract] and [OperationContract] [ServiceContract]     public interface IService1     {         [OperationContract]         string GetData(int value);         [OperationContract]         CompositeType GetDataUsingDataContract(CompositeType composite);         // TODO: Add your service operations here     }
Data Contract in WCF
It has 2 attributes:
 [DataContract]and [DataMember]
// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";
        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }
        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
WCF configurations in config file
It declares information about endpoint name, address, binding and contract.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1"behaviorConfiguration="WcfServiceLibrary1.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">
          <!--
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceLibrary1.Service1Behavior">
          <!-- To avoid disclosing metadata information,
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes,
          set the value below to true.  Set to false before deployment
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
 

wcf wcf 
Updated 13-May-2020
More than 18 years of working experience in IT sector. We are here to serve you best.

Leave Comment

Comments

Liked By