If you find this article useful, consider making a small donation to show your support for this web site and its content.
Free app Developer Interview available here.

Available on the iPhone App Store
Available on the Google Play
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.



The ABC of WCF

by Farooq Kaiser 16. September 2009 07:25

Microsoft's next-generation programming platform and runtime system for building, configuring and deploying service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing investments. Most of the WCF functionality is included in a single assembly called System.ServiceModel.dll, located in the System.ServiceModel namespace.

What are the ABCs of WCF?

ABC stands for address, binding, and contract, they are also called WCF endpoint.

ABC-endpoint

  • A - stands for Address: Where is the service?
  • B - stands for Binding: How do I talk to the service?
  • C - stands for Contract: What can the service do for me?

Address:  The address provides two important elements: the location of the service, and the transport protocol.

WCF supports the following transport schemas:

  • HTTP

  • TCP

  • Peer network

  • IPC (Inter-Process Communication)

  • MSMQ

Here are few sample addresses.

http://localhost:8001

http://localhost:8001/TestService

net.tcp://localhost:8004/TestService

net.pipe://localhost/TestPipe

net.msmq://localhost/private/TestQueue

Binding: Bindings are objects that are used to specify the communication details that are required to connect to the endpoint of a WCF service. It is the primary extension point of the ABCs of WCF. Bindings describe the transport protocol, message-encoding format and other messaging protocols for the communication channel. Here are complete list of the standard bindings that come with WCF.

Binding type Description
BasicHttpBinding The BasicHttpBinding is able to communicate with ASMX-based Web services and clients such as ASP.NET web services (ASMX).
WSHttpBinding The WSHttpBinding is similar to the BasicHttpBinding but provides more Web service features such as distributed transactions and secure, reliable sessions.
WSDualHttpBinding The WSDualHttpBinding provides the same support for Web Service protocols as the WSHttpBinding, but it provides two-way communication.
WSFederationHttpBinding

A secure and interoperable binding that supports federated security. Federation is the ability to share identities across multiple systems for authentication and authorization.

NetNamedPipeBinding A connection-oriented binding for communications on the same machine over named pipes.
NetTcpBinding

A secure, reliable binding suitable for cross-machine communication.

NetPeerTcpBinding

Provides a secure binding for peer-to-peer network applications.

NetMsmqBinding

Represents a queued binding that is suitable for cross-machine communication.

MsmqIntegrationBinding This binding can be used to enable WCF applications to send and receive messages to and from existing MSMQ applications that use legacy MSMQ components.

Bindings can be defined in two ways:

  • Declaratively, using service model configuration settings, Here is a declaratively sample:

    <endpoint address="http://localhost:8001/TestService"
    
    binding="basicHttpBinding" contract="ITestService" />
  • Programmatically, by adding endpoints to the ServiceHost, Here is a programmatically sample:

    BasicHttpBinding binding = new BasicHttpBinding();
    
    binding.Security.Mode = BasicHttpSecurityMode.None;
    
    Uri baseAddress = new Uri("http://localhost:8001/TestService");
    
    Uri address = new Uri("http://localhost:8001/TestService/TestService");
    
    ServiceHost serviceHost = new ServiceHost(typeof(TestService), baseAddress);
    
    serviceHost.AddServiceEndpoint(typeof(ITestService), binding, address);
    In programmatically, As you can see we construct a binding instance and assign its endpoints.

 Contract: A contract is a description of the messages that are passed to and from service endpoints.

Service-ABC

There are three types of contracts in WCF:

  • Service contracts: Describe which operations the client can perform on the service. It defines some properties about the service, and a set of actions called Operation Contracts. Operation Contracts are equivalent to web methods in ASMX technology. Here is a sample of service contract.

    <%\@ ServiceHost Language="C#" Service="TestService" %>
    
    using System.ServiceModel;
    
    [ServiceContract]
    
    public interface ITestInterface
    
    {
    
     [OperationContract]
    
     string Test ( string message);
    
    }
    
    public class TestService : ITestInterface
    
    {
    
     public string Test ( string message)
    
     {
    
       return "Hello: " + message;
    
     }
    
    }
  • Data contracts: Data contracts describe data structures that are used by the service to communicate with clients. A data contract maps CLR types to XML Schema Definitions (XSD) and defines how they are serialized and deserialized. Data contracts describe all the data that is sent to or from service operations.

    [DataContract]
    
    class TestDataContract
    
    {
    
     [field:DataMember]
    
     public event EventHandler TestEvent;
    
    }
  • Message contracts: Message contracts describe the structure of SOAP messages sent to and from a service and enable you to inspect and control most of the details in the SOAP header and body.

        [MessageContract]
    
        public class BidPrice
    
        {
    
            [MessageHeader]
    
            public DateTime CurrentTime;
    
            [MessageBodyMember]
    
            public Price Price;
    
        }

References:

Summary

In this article, we examined WCF as a very rich technology platform for building distributed systems. WCF Services expose a collection of Endpoints where each Endpoint is a portal for communicating with the world. Each Endpoint has an Address, a Binding, and a Contract (ABC). The Address is where the Endpoint resides, the Binding is how the Endpoint communicates, and the Contract is what the Endpoint communicates.

Currently rated 3.2 by 40 people

  • Currently 3.175/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,


comments powered by Disqus
Jobs Autos Real estate Videos Power by Google