WCF - Introduction

WCF Introduction
  • WCF is set of Microsoft's implementation of a set of industry standards
  • Defining service interactions
  • Type conversion
  • Marshalling
  • Various protocols' management

Building Blocks - ABC of WCF

  • Address - Location + Transport protocol / schema
  • Binding - Binding information of the address and contracts with protocols, transports, and message encoders
  • Contracts - What the service does
OK ... Now lets see a simple example in Visual Studio 2008.

Simple Service

  • Create New Project - > WCF -> WCF service Library in Visual Studio 2008
  • Run the project from Visual Studio (F5)
  • You will see the WCF Service host in the windows system tray and WCF Test Client opened.

WCF Test Client



Your service is self hosted and ready to serve.
  • WCF Test Client, nice tool from Microsoft to test small to medium level applications. You can give test inputs and see the result. The tool is self explanatory so I am leaving it to you.
OK ... Now lets explore what this service contains



  • The project added 2 library references. System.Runtime.Serialization and System.ServiceModel
  • There are 3 main files added into the project. IService1.cs, and Service1.cs, App.config
    • IService1.cs - Contains Contracts
    • Service1.cs - Contains Implementation
    • App.config - Contains Hosting configuration details
OK .. Now lets explore what IService1.cs contains.

There are 2 additional namespaces used.

using System.Runtime.Serialization;
using System.ServiceModel;

Rule #1. The namespaces System.Runtime.Serialization and using System.ServiceModel should be added in order to access the WCF libraries.

It has a Interface IService1 which is marked as [ServiceContract]

[ServiceContract]
public interface IService1

Rule #2. Contract (Interface) should be marked as [ServiceContract] to expose as a service.

The interface has 2 methods and methods and are marked as [OperationContract].

Rule #3. Methods should be marked as [OperationContract] to expose as a service method.

It has a public class "CompositeType" with set of properties and the class is marked as [DataContract]

Rule #4. Custom types (class/structure) marked as [DataContract] to exchange data between Server (WCF Service) and Client

When you specify [DataContract], WCF uses the DataContractSerializer to serialize your types.

The DataContract class contains 2 properties with no implementation and are marked as [DataMember].

Rule #5. Data contract properties should be marked as [DataMember] to serialize. If you don't mark then it wont be serialized.

OK dude, now lets explore the Service1.cs file

This implements the IService1 interface nothing else special in there. So the implementation is up to yours.

Ok.. lets explore the App.config file. I removed the comments and make it simple.


<configuration>
  <system.serviceModel>
    <services>
      <service name="MyServiceLibrary.Service1" behaviorConfiguration="MyServiceLibrary.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8731/Design_Time_Addresses/MyServiceLibrary/Service1/" />
          </baseAddresses>
        </host>
        <endpoint address ="" binding="wsHttpBinding" contract="MyServiceLibrary.IService1" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceLibrary.Service1Behavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

The root node is

<configuration></configuration>

The service information is placed under


<configuration>
  <system.serviceModel></system.serviceModel>
</configuration>

It contains services and service behaviors.


<configuration>
  <system.serviceModel>
    <services></services>
    <behaviors></behaviors>
  </system.serviceModel>
</configuration>

Service contains one service.


    <services>
      <service name="MyServiceLibrary.Service1" behaviorConfiguration="MyServiceLibrary.Service1Behavior">
      </service>
    </services>

The service contains hosting details as, where you have one host and the host specifies one baseAddress.


      <service name="MyServiceLibrary.Service1" behaviorConfiguration="MyServiceLibrary.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8731/Design_Time_Addresses/MyServiceLibrary/Service1/" />
          </baseAddresses>
        </host>
      </service>

It contains 2 endpoints. The formula is WCF = EndPoints = Address + Binding + Contract


        <endpoint address ="" binding="wsHttpBinding" contract="MyServiceLibrary.IService1" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

Then there is a service behavior which says enable the metadata.


    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceLibrary.Service1Behavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

Let me stop the article here... take a while read this again.. you may want to explore further details. Though there are plenty available in the internet, I will also add some more postings for the continuation.

No comments:

Post a Comment