Who's Online
21 visitors online now
2 guests, 19 bots, 0 members
Support my Sponsor
  • An error has occurred, which probably means the feed is down. Try again later.

Posts Tagged ‘Web services’

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:

  1. Interoperable with other Services
  2. Provide better reliability and security compared to ASMX web services.
  3. 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]
public string Test()
{
return “Hello! Web Services”;
}

}

 

 

 

 

 

[ServiceContract] attribute has to be added to the class and [OperationContract] attribute represents the method exposed to client

Example:

[ServiceContract]

public Interface InterfaceTest

{

[OperationContract]
string Test();
}

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.

  1. Service Class – A WCF service class implements some service as a set of methods.
  2. 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.
  3. EndpointsAll 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.

  1. Service Contract: describe the operation that service can provide.
  2. Data Contract: describes the custom data type which is exposed to the client.
  3. 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.
  4. 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.

wcf service

  • 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”


wcf service2

 

  • 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. wcf service3To view the metadata of the Service click on the WSDL URL. wcf service4

 

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);
}
}
}

wcf service5

  • Press F5 to run the Console Application and we got below output.wcf service6

 

Related helpful links

https://msdn.microsoft.com/en-us/library/ms731082(v=vs.110).aspx

http://www.wcftutorial.net/

I hope this will help you.Your valuable feedback and comments are important for me.