Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Most of WCF functionality is included in a single assembly called System.ServiceModel.dll, located in the System.ServiceModel namespace.
Why WCF
WCF is a generic communication mechanism that allows you to set up a generic host communication between two clients. Let me explain what I am trying to say. Suppose I have three clients and we need to implement a service for them but all three clients have different implementations.
- The first client uses PHP/Java. So the client wants a response/message in XML format.
- The second is using .Net but the service execution time requirements are high so the client wants responses/messages in binary format and the protocol to be TCP.
- Third is using COM+.
For the first client we need to use a webservice, for the second we use .Net remoting and for the third we use COM+.
Webservice, .Net remoting and COM+ are different technologies. So the developer must learn these technologyies But WCF unites all the technologies under one umbrella.
WCF = Webservice + .Net remoting + MSMQ + COM+
WCF supports the following features:
- Service Oriented Architecture
- Interoperability
- Multiple Message Pattern
- Service Metadata
- Data Contracts
- Security
- Multiple Transport and Encodings
- Reliable and Queued Messages
- Durable Messages
- Transaction
- Ajax and REST support
- Extensibility
We will talk about these topics in later articles of this series.
ABC of WCF
ABC stands for Address, Binding and Contracts respectively.
Address
An address is a URL that points to the location of the service. In WCF every service has a unique address. The address provides two important things. First is the location of that service and the second is the transport protocol. WCF supports the following transports but we implement the HTTP transport.
- Http/Https
- TCP
- IPC
- Peer newtwork
- MSMQ
- Service bus
Suppose I made a WCF application and name it myfirstapp and using basicHttpBinding then the URL should be like http://localhost:8080/myfirstapp.
Bindings
Bindings expose a service that can be accessed using HTTP or MSMQ. There are multiple aspects of communication with any given service and there are many possible communication patterns. WCF 4.0 supports the following bindings:
- 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. In other words a contract is a platform and standard way of describing what the service does. There are the following four types of contracts in WCF:
- Service Contractt
- Data Contract
- Fault Contract
- Message Contract
Let's make a WCF application. In this application we are trying to add two numbers using a WCF service. I am using Visual Studio 2012. Open Visual Studio and go to "File" -> "New" -> "Project..." and select WCF Service Application and name it myFirstApp.
Visual Studio provides some auto-generated code. Let's delete the IService1.cs and Service1.svc files. Add a WCF service file and name it MyService.svc and provide it the following code. It adds the following two files.
- IMyService.cs
- MyService.svc
Delete all code of ImyService and write the following code.
- using System.ServiceModel;
-
- namespace myFirstApp
- {
- [ServiceContract]
- public interface IMyService
- {
- [OperationContract]
- int AddTwoNo(int intFirstNo, int intSecondNo);
- }
- }
ServiceContract describes which operation the client can perform on the service.
OperationContract is defined within a ServiceContract. It defines the parameters and return type of the operation.
Now implement the ImyService interface in MyService.svc.cs. Let's delete all the code first from MyService.svc.cs and write the following:
- namespace myFirstApp
- {
- public class MyService : IMyService
- {
- public int AddTwoNo(int intFirstNo, int intSecondNo)
- {
- return intFirstNo + intSecondNo;
- }
- }
- }
Hit F5 to run this application.
After running it successfully double-click on AddTwoNo() and fill in the value in intFirstNo and intSecondNo respectively. I am putting 5 and 6 and hitting the invoke button.
Output:
Note: The default binding in WCF is basicHttpBinding. We can see all details of this service at http://localhost:36246/myservice.svc that is shown in the preceding image.
Now I am calling this WCF service using an ASP.NET application.
Open a new Visual Studio instance and go to "File" -> "New" -> "Project..." and select ASP.NET Empty Web Application and name it WCFClientApp.
Add a webform and name it default.aspx and add the service reference.
Right-click on the project (WCFClientApp).
Click on Add service reference and fill in the address and click go. Then change the namespace as you want and click OK. I am keeping the WCFReference namespace.
In default.aspx
- <table>
- <tr><td>First No</td><td><asp:TextBox ID="txtFirst" runat="server"></asp:TextBox></td></tr>
- <tr><td>Second No</td><td><asp:TextBox ID="txtSec" runat="server"></asp:TextBox></td></tr>
- <tr><td colspan="2"><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></td></tr>
- <tr><td colspan="2"><asp:Label ID="lblResult" runat="server"></asp:Label></td></tr>
- </table>
In default.aspx.cs
- using System;
-
- namespace WCFClientApp
- {
- public partial class _default : System.Web.UI.Page
- {
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- int intFirstNo = 0, intSecNo = 0, intResult = 0;
- intFirstNo = Convert.ToInt16(txtFirst.Text);
- intSecNo = Convert.ToInt16(txtSec.Text);
- WCFReference.MyServiceClient client = new WCFReference.MyServiceClient();
- intResult = client.AddTwoNo(intFirstNo, intSecNo);
- lblResult.Text = "Result is :"+intResult;
- }
- }
- }
Run the application.
OutputI hope this article is helpful for you.
Next Article>>
Introduction to Endpoint in WCF: Part 2