Before starting with WCF programming, lets discuss about the base terms used very oftenly in this article .

A service  is a construct that exposes one or more endpoints, each of which exposes one or more service operations.

A endpoint  of a service specifies an address where the service can be found.

A binding  contains the information that a client must communicate with the service.

Consider a scenario say, I am creating a service that has to be used by two type of client. One of the client will access SOAP using http and other client will access Binary using TCP. How it can be done? With Web service it is very difficult to achieve, but in WCF its just we need to add extra endpoint in the configuration file. 

 <system.serviceModel>
    <services>
      <service name="MathService"
        behaviorConfiguration="MathServiceBehavior">
      <endpoint address="https://localhost:8090/MyService/MathService.svc"
        contract="IMathService"
          binding="wsHttpBinding"/>
<endpoint address="net.tcp://localhost:8080/MyService/MathService.svc"
contract="IMathService"
          binding="netTcpBinding"/>

      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MathServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

 

 

A contract that defines the functionality provided by the service to its clients.

 

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

 

WCF service can be configured  using CODE, can also be hosted under Internet Information Services (IIS) and also be configured within a CONFIGURATION FILE. (In detail explanation is given later)

An example for configuring a service from code as follows..

Uri baseAddr = new Uri("https://localhost:8000/WcfConsoleApp");ServiceHost localHost = new ServiceHost(typeof(CalculatorService), baseAddr);try{typeof(ICalculator), new WSHttpBinding(), "CalculatorService");ServiceMetadataBehavior smb = new ServiceMetadataBehavior();true;Console.WriteLine("Service initialized.");Console.WriteLine("Press the ENTER key to terminate service.");Console.WriteLine();Console.ReadLine();catch (CommunicationException ex){Console.WriteLine("Oops! Exception: {0}", ex.Message);

Click here to know how to host WCF service in IIS. (You can use IIS hosting only with an HTTP transport)

Click here to know how to configure a WCF service using configuration file.

 

 What is a Proxy?

Answer:
The proxy is a CLR class that exposes a single CLR interface representing the service contract. The proxy provides the same operations as service's contract, but also has additional methods for managing the proxy life cycle and the connection to the service. The proxy completely encapsulates every aspect of the service: its location, its implementation technology and runtime platform, and the communication transport.

The proxy can be generated using Visual Studio by right clicking Reference and clicking on Add Service Reference. This brings up the Add Service Reference dialog box, where you need to supply the base address of the service (or a base address and a MEX URI) and the namespace to contain the proxy.

Proxy can also be generated by using SvcUtil.exe command-line utility. We need to provide SvcUtil with the HTTP-GET address or the metadata exchange endpoint address and, optionally, with a proxy filename. The default proxy filename is output.cs but you can also use the /out switch to indicate a different name.

SvcUtil https://localhost/MyService/MyService.svc /out:Proxy.cs

When we are hosting in IIS and selecting a port other than port 80 (such as port 88), we must provide that port number as part of the base address:

SvcUtil https://localhost:88/MyService/MyService.svc /out:Proxy.cs

 

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

WCF - Creating and Implementing a Service in C#

 

 

 

 

 

In WCF, all services are exposed as contracts. Contract is a platform-neutral and standard way of describing what the service does. Mainly there are four types of contracts available in WCF

Service Contract

Service contracts describe the operation that service can provide. For Eg, a Service provide to know the temperature of the city based on the zip code, this service is called as Service contract. It will be created using Service and Operational Contract attribute.

To know more on Service contract see Service contract tutorial.

Data Contract

Data contract describes the custom data type which is exposed to the client. This defines the data types, that are passed to and from 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.

To know more on DataContract see DataContract tutorial.

Message Contract

Default SOAP message format is provided by the WCF runtime for communication between Client and service. If it is not meeting our requirements then we can create our own message format. This can be achieved by using Message Contract attribute.

To know more on Message Contract see Message contract tutorial.

Fault Contract

Suppose the service I consumed is not working in the client application. I want to know the real cause of the problem. How I can know the error? For this we are having Fault Contract. Fault Contract provides documented view for error occurred in the service to client. This helps us to easy identity, what error has occurred.

 

 

 

Search site

© 2010 All rights reserved.