Posts Tagged ‘Windows Communication Foundation’
What Is Windows Communication Foundation(WCF)
Windows Communication Foundation (WCF) is a .NET framework for developing, configuring and deploying services. It is introduce with .NET Framework 3.0.It is service oriented technology used for exchanging information. WCF has combined feature of .NET Remoting, Web Services and few other communications related technology.
Key Feature of WCF:
- Interoperable with other Services
- Provide better reliability and security compared to ASMX web services.
- No need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements.
Difference between WCF and Web service
Web Service | WCF |
---|---|
It can be hosted in IIS | It can be hosted in IIS, windows activation service, Self-hosting, Windows service. |
[WebService] attribute has to be added to the class and [WebMethod] attribute represents the method exposed to client
Example. [WebService] public class myService: System.Web.Services.Webservice { [WebMethod] }
|
[ServiceContract] attribute has to be added to the class and [OperationContract] attribute represents the method exposed to client
Example: [ServiceContract] public Interface InterfaceTest { [OperationContract] public class myService:InterfaceTest { public string Test() return “Hello! WCF”; }
|
Can be access through HTTP | Can be access through HTTP, TCP, Named pipes. |
One-way, Request- Response are the different operations supported in web service. | One-Way, Request-Response, Duplex are different type of operations supported in WCF. |
System.Xml.Serialization name space is used for serialization. | System.Runtime.Serialization namespace is used for serialization. |
Can not be multi-threaded. | Can not be multi-threaded. |
For binding it uses SOAP or XML | Support different type of bindings (BasicHttpBinding, WSHttpBinding, WSDualHttpBinding etc ) |
WCF Services has three important companent.
- Service Class – A WCF service class implements some service as a set of methods.
- Host Environment- It can be a application or a Service or a Windows Forms application or IIS as in case of the normal asmx web service in .NET.
- Endpoints –All the WCF communications are take place through end point.
Endpoints consist of three component Address, Binding and contract. They collectively called as ABC’s of endpoints.
Address: It is basically url address where WCF services is hosted.
Binding: Binding will describes how client will communicate with service.
Binding supported by WCF
Binding |
Description |
BasicHttpBinding |
Basic Web service communication. No security by default |
WSHttpBinding |
Web services with WS-* support. Supports transactions |
WSDualHttpBinding |
Web services with duplex contract and transaction support |
WSFederationHttpBinding |
Web services with federated security. Supports transactions |
MsmqIntegrationBinding |
Communication directly with MSMQ applications. Supports transactions |
NetMsmqBinding |
Communication between WCF applications by using queuing. Supports transactions |
NetNamedPipeBinding |
Communication between WCF applications on same computer. Supports duplex contracts and transactions |
NetPeerTcpBinding |
Communication between computers across peer-to-peer services. Supports duplex contracts |
NetTcpBinding |
Communication between WCF applications across computers. Supports duplex contracts and transactions |
BasicHttpBinding |
Basic Web service communication. No security by default |
WSHttpBinding |
Web services with WS-* support. Supports transactions |
Contract: The endpoints specify a Contract that defines which methods of the Service class will be accessible via the endpoint; each endpoint may expose a different set of methods. It is standard way of describing what the service does.
Mainly there are four types of contracts available in WCF.
- Service Contract: describe the operation that service can provide.
- Data Contract: describes the custom data type which is exposed to the client.
- Message Contract: WCF uses SOAP message for communication. Message Contract is used to control the structure of a message body and serialization process. It is also used to send / access information in SOAP headers. By default WCF takes care of creating SOAP messages according to service DataContracts and OperationContracts.
- Fault Contract: Fault Contract provides documented view for error occurred in the service to client. This help as to easy identity the what error has occurred, and where. By default when we throw any exception from service, it will not reach the client side.
Creating WCF service in visual studio 2013:
- First open Visual Studio 2013. Create a new project and select WCF Service Application and give it the name WcfService1.
- Delete default created IService1.cs and Service1.svc file.
- Right-click on the project and select “Add” | “New Item…”
- From the Web tab choose WCF Service to add.
- give the service the name “HELLO.svc”
- Open IHELLO.cs and remove the “void DoWork()”.
IHELLO.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// NOTE: You can use the “Rename” command on the “Refactor” menu to change the interface name “IHELLO” in both code and config file together.
[ServiceContract]
public interface IHELLO
{
[OperationContract]
string sayHello();
}
}
- Configure Endpoints with Metadata
To do this open the Web.config file. We are going to create one Endpoint with basicHttpBinding. We are adding a Endpoint also to configure the metadata of the service.
<services>
<service behaviorConfiguration=”WcfService1.HELLOBehavior” name=”WcfService1.HELLO”>
<endpoint address=”” binding=”basicHttpBinding” contract=”WcfService1.IHELLO”/>
<endpoint address=”mex” binding=”mexHttpBinding” contract=”IMetadataExchange” />
</service>
</services>
- Implement Service. HELLO.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
public class HELLO : IHELLO
{
public string sayHello()
{
return “Hello! welcome to WCF”;
}
}
}
Now we have created the Service and configured the Endpoint.To host it press F5 in Visual Studio.
In the browser you will see the Service as in the following. To view the metadata of the Service click on the WSDL URL.
In Windows / Web application we can consume this WCF service by using “Add Service Reference”->Add Service Address “http://localhost:57091/HELLO.svc” -> Click on Go.
I had created console application and added service reference.
Program.cs
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
HelloServiceReference.HELLOClient oHell = new HelloServiceReference.HELLOClient();
string str= oHell.sayHello();
Console.WriteLine(str);
}
}
}
Related helpful links
https://msdn.microsoft.com/en-us/library/ms731082(v=vs.110).aspx
I hope this will help you.Your valuable feedback and comments are important for me.