Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

WCF Learning Series Part 1 - WCF Introduction

Article:
Viewed:  1309 
Posted On:  05/12/2014 20:02:07 
This is my WCF Learning Series Part 1. 

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.


WCF = WebService + .Net Remoting + MSMQ + COM+

Scenarios where WCF must be used

      1.    A secure service to process business transactions.

      2.    A service that supplies current data to others, such as a traffic report or other monitoring service.

      3.    A chat service that allows two people to communicate or exchange data in real time.

      4.    A dashboard application that polls one or more services for data and presents it in a logical presentation.

      5.    Exposing a workflow implemented using Windows Workflow Foundation as a WCF service.

      6.    A Silverlight application to poll a service for the latest data feeds.

Features of WCF

  •      Service Orientation
  •      Interoperability
  •      Multiple Message Patterns
  •      Service Metadata
  •      Data Contracts
  •      Security
  •      Multiple Transports and Encodings
  •      Reliable and Queued Messages
  •      Durable Messages
  •      Transactions
  •      AJAX and REST Support
  •      Extensibility

The advantages of WCF

  • WCF can be configured to work independently of SOAP and use RSS instead.
  •                WCF is one of the fastest communication technologies and offers excellent performance compared to other Microsoft specifications.
  •           To improve communication, transmission speed needs to be optimized. This is achieved by transmitting binary-coded XML data instead of plain text to       decrease latency. 
  •       Object life-cycle management and distributed transaction management are applicable on any application developed using WCF.
  •                WCF uses WS specifications to provide reliability, security and transaction management.
  •           Messages can be queued using persistence queuing. As a result, no delays occur, even under high traffic conditions.

 

Now we will learn basic of WCF:

·         The ABC of Windows Communication Foundation

"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?

  • "A" stands for Address—as expressed in the wsdl:service section and links wsdl:binding to a concrete service endpoint address.
  • "B" stands for Binding—as expressed in the wsdl:binding section and binds a wsdl:portType contract description to a concrete transport, an envelope format and associated policies.
  • "C" stands for Contract—as expressed in the wsdl:portType, wsdl:message and wsdl:type sections and describes types, messages, message exchange patterns and operations.

Address # The address specifies the location of the service which will be exposed for clients that will use it to communicate with the service. The address's protocol that WCF can provide:

  • HTTP
  • TCP
  • NamedPipe
  • Peer2Peer
  • MSMQ

Binding # Specifies how a service is accessible. In other words: how the two parties will communicate in terms of transport (HTTP, TCP, NamedPipe, Peer2Peer, MSMQ) ,encoding (text, binary etc.) and protocols (like transactional support or reliable messaging).

Bindings supported by WCF 4.0:

  • basicHttpBinding
  • wsHttpBinding
  • wsDualHttpBinding
  • wsFederationHttpBinding
  • netTcpBinding
  •  netNamedPipeBinding
  • netMsmqBinding
  • netPeerTcpBinding
  • msmqIntegrationBinding
  • basicHttpContextBinding
  • netTcpContextBinding
  • webHttpBinding
  •   wsHttpContextBinding


Contract #
A contract is an agreement between two parties. It defines how a client should communicate with your service.

Contracts in WCF:

  •  Service Contract
  •  Data Contract
  •  Fault Contract
  •  Message Contract

 

Now we will learn this by creating a simple HelloWCF program.

Open visual Studio -> File -> New Project

1.png

Image 1.

Select WCF Service Application. Give it a name.

2.png

Image 2.

Remove IService1.cs and Service1.svc file from here. Now Right click on solution explorer-> add new item.

3.png

Image 3.

Now open IMyService.cs and add below method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
 
namespace Hello_WCF_Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMyService" in both code and config file together.
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string AddText(string Name);
    }
}
 

Now open
MyService.cs File


using
System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
 
namespace Hello_WCF_Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MyService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select MyService.svc or MyService.svc.cs at the Solution Explorer and start
debugging.

    
public class MyService : IMyService
    {
        public string AddText(string Name)
        {
            return "Welcome " + Name;
        }
    }
}

Now run your service.

4.png

Image 4.

You can test your method.

5.png

Image 5.

You can view config file like below.

6.png

Image 6.

See your service in browser.

7.png

Image 7.

Now we will consume this service in our web application. So Right click on solution explorer and add new project.

8.png

Image 8.

Select Web -> ASP.NET Web Application.

9.png

Image 9.

Now add WCF Service reference. Right Click on your project-> Add Service reference.

10.png

Image 10.

11.png

Image 11.

Now open your Default.aspx page:

<table cellpadding="10" cellspacing="10" width="90%" style="border: solid 4px green;">
                <tr>
                    <td>Enter Name #
                    </td>
                    <td>
                        <asp:TextBox ID="txtName" runat="server" Width="250px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:Button ID="btnCallService" runat="server" Text="Click Me" OnClick="btnCallService_Click" />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:Label ID="lblMessage" runat="server" Font-Bold="true" Font-Size="16pt" ForeColor="Red"></asp:Label>
                    </td>
                </tr>
            </table>

Now open Default.aspx.cs page and call your WCF Service methid:


using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace MyProject
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void btnCallService_Click(object sender, EventArgs e)
        {
            MyWCFService.MyServiceClient myService = new MyWCFService.MyServiceClient();
            lblMessage.Text = myService.AddText(txtName.Text);
        }
    }
}

Now run you application.

12.png

Image 12.

13.png

Image 13.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us