Showing posts with label Creating First WCF Methods. Show all posts
Showing posts with label Creating First WCF Methods. Show all posts

June 17, 2010

WCF Vs Web Services

WCF/ASP.Net Web Services are used for developing web services. Here is a list of differentiation between these two.

Feature

ASP.NET Web Service

WCF

Data Transformation

To and from Data Transition is done through XML Serializer

DataContractSerializer is used for data transition

File Extension

File extension is asmx

File extension is .svc

Webmethods vs    DataContract Attributes

ASP.NET WebService uses Webmethods to translate .NET FW types in to XML.</SPAN< td>

The WCF uses the DataContractAttribute and DataMemeberAttribute to translate .NET FW types in to XML.

Limitations

Only Public fields or Properties of .NET types can be translated into XML.Only the classes which implement IEnumerable interface. ICollection interface can be serializable

Public/Private fields or properties of .NET types can be translated.

IDictionary Interface  class

Classes that implement the IDictionary interface, such as Hash table can not be serialized.

The DataContractSerializer can translate the Hash Table into XML. Hence using WCF we can even translate HashTable into XML

Security

WCF is more secured than WebService due to -> It is based on WS Standards. capable to run in any .NET executable, so it needs independent security capabilities.

Transfer security  Responsible for providing message confidentiality, data integrity, and authentication of communicating parties.
Authorization Responsible for providing a framework for making authorization decisions.
Auditing Responsible for logging security-related events to the audit log

Binding

Web service supports only HTTP.

WCF supports multiple bindings HTTP,TCP,MSMQ,WS-HTTP etc

Messaging

ASP.Net web service uses only SOAP (Simple Object Access Protocol) for sending and receiving data. It uses Xml Schema  to  defines structure of message.

Windows Communication Foundation (WCF) can send message in any format. It uses SOAP for communication by default. It can use any other transport protocol for message transport .

Performance

Slower compared to WCF

The main advantage of the design of the DataContractSerializer is better performance over XML serialization

Fields / Properties

XMLSerialization does not indicate the which fields or properties of the type are serialized into XML

DataContratSerializer Explicitly shows the which fields or properties are serialized into XML

Exception handling

In ASP.NET Web services, Unhandled exceptions are returned to the client as SOAP faults.

In WCF Services, unhandled exceptions are not returned to clients as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.

Example

[WebService]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string Demo(string strDemog)
{
return strDemo;
}
}

[ServiceContract]
public interface ITest
{
[OperationContract]
string ShowMessage(string strDemo);
}
public class Service : ITest
{
public string Demo(string strDemo)
{
return strDemo;
}
}

After you know the importance of the WCF on WebServices, to create and explore on Your first WCF application

Getting Started with your first WCF Application

The basics
WCF stands for Windows Communication Foundation with the code name “Indigo”. It is Application User Interface used to create distributed applications using .NET Framework. System.ServiceModel is the name space that is responsible to utilize WCF Concepts.

WCF application is a service, collection of end points in order to communicate with client or service applications. In order to serialize data we make use of System.Runtime.Serialization name space.

WCF Terminology
Addresses The location of service in the form of URI. These are the expose points of services.
Bindings How they communicate with the protocol For ex: The type of network protocol used in WCF.
Contracts the methods exposed by WCF defines the contract.
Service Contract
This attribute that is applied to interfaces in WCF service.
Operation Contract
This is the contract that is applied to methods that are implementing WCF interface.
Channel If client and server wanted to interact with each other . They need to go through the channel that has input message at one end and the other end as results message.
Intermediaries
Intermediaries as the name suggests these are programs that act as "middle-man”. They are commonly invisible to the client and services.ex: a firewall ,gateway

Creating WCF Service

Open VS2008 or VS2010 and Create a New Project->WCF->WCF Service Library Project as shown below.

New WCF Project Creation

You have the Service1.svc.cs and IService1.svc.cs in WCF Project
IService.cs is the WCF interface and Service is where exactly you will implement the interface.
You already had methods as GetDataUsingDataContract(), GetData() as methods.

Let us implement one method called GetEmpID()in WCF Service.cs that gets the GUID .
We need to add an operational contract and define a method in IService interface.
Operational contract is mandatory to create WCF service or use WCF method

Create OperationContract in IService1.cs
[OperationContract]
string GetEmpID(string empId);

Append the below lines of code in the Service1.svc.cs that implements IService interface

public string GetEmpID(string EmpId)
{
EmpId = Guid.NewGuid().ToString();
return EmpId;
}


Running WCF Service using Visual Studio 2008


Press f5


Visual Studio itslef creates WCF Test client for theservice where we can test the demo service working or not.
WCF TestClient

Press invoke button

WCF Application Invoke

You can also examine the XML File that contains soap headers in the with xml tabs
And also config file in the config section.
XML and config tabs in WCF Test client