Friday, June 26, 2009

Difference between Web Services and WCF

Last time when I talked about WCF and told to upgrade from Web Services to WCF, many of my friends asked me about the difference between the Web Services and WCF and why to upgrade. So now this blog entry explain why we need to upgrade from Web Services to WCF.

Reach ability

SOA initiatives using ASMX services and trying to reach out to all the stakeholders on internet using SOAP/http clients was the natural option, where as to connect with employees, internal users or partner’s it would have used TCP IP based .Net Remoting. Thus to serve different user base, one has to use different transport option and each transport needed independent implementation.

Unlike ASMX services which were primarily Http based, WCF Services can be reached using multiple protocols like Http, TCP IP, Named Pipe and MSMQ. Other clients like internet/intranet or partners can use any of the above mentioned preferred channels to hook on to these services without IT having to maintain separate code base to serve different client class.

Interoperability

Large Enterprises having mix of technologies, Interoperability requirements with Non Microsoft clients is one of the common scenarios in any SOA initiatives. With ASMX WS-*, XML Serialization is not standardized and hence it is difficult to achieve true interoperability. With WCF, it generates standard SOAP based XML to be consumed by clients. Additionally by using data contracts and message contracts, information exchange with non Microsoft clients’ become easy and improves interoperability.

Performance

Most often sharpness of underlying technology contributes in meeting performance based business SLAs (e.g. In banking gateway for Money transfer, Number of clients served per second). ASMX services were part of .net framework 2.0, since then with .net 3.5 and 4.0 around, there has been continuous improvements in core .net framework to improve performance.

One of the key reasons WCF performance is better than ASMX services is due to the improved serialization techniques in the .net framework. WCF services provide 25 to 40% improvement in performance over ASMX services and hence become natural preference for high performance application.

Extensibility

If ASMX services were to be delivered to clients, only possible way to host them was on to IIS (Internet Information Server). With WCF services, one can host the services into Windows services, Windows Activation Service (WAS) or IIS. WCF also supports claims based authorization which is more granular than existing mechanisms. With WCF starting from application runtime, security, binding, serialization, channel system to service host everything is extensible.

ASMX with WSE 3.0 supports security and http buffering/caching/sessions but still lacks support for Session management, Concurrency, Security, Reliable messaging, Transaction, Throttling, Exception handling and faults. In applications explicitly using WSE to support some of these features is difficult and cumbersome, where as with WCF WSE is inbuilt.

With .Net 4.0, the argument to migrate becomes even stronger because of the benefit from platform features like CLR enhancements, Improved Garbage collection, Parallel programming, Distributed caching, and improved support for REST, and Workflow Services.

With above benefits making way to WCF migration, one of the ways to solve service migration problem is by overlaying WCF attribute over ASMX attributes. e.g. With ServiceContract() attribute on ASMX Service class, and OperationContract() on service methods/operations and then making appropriate changes in .config file. This wrapper based approach may make asmx services work with asmx and wcf clients. But with this approach, since the underlying code is still asmx, it will not benefit from the underlying framework advantages that WCF provides as discussed above.



kick it on DotNetKicks.com

Thursday, June 18, 2009

WCF - Understanding the Service.

This post is an introduction to Windows Communication Foundation (WCF) and will illustrate the ease at which a WCF service can be built. This article is written with the assumption that the reader has no prior knowledge of WCF.

In this article I will just give you a small demo and allow you to get confidence on WCF especially if you are a novice.

In this post we will just see how to create a simple WCF service application without going into more theory on WCF.

In general WCF as the name Windows Communications Foundation implies that the WCF is the concept released in .NET Framework 3.0 in order to provide effective communication between different applications.

There is myth that WCF is only for windows applications. However, it is wrong to think that WCF is only for windows. This can be thought as the replacement(strictly speaking enhancement) of the Web Services in .NET 1.X and .NET 2.0 frameworks.

I am using the Microsoft Visual Studio 2008 for this demo. Here are the simple steps to follow,

1. Open visual studio and create a new project.

2. In the project types under Visual C# or Visual Basic, select the Web and select the WCF Service Application from the right pane as shown in the figure.

New Project Template

3. Once we click on the OK button we will get file opened Service1.svc.cs or Service1.svc.vb based on the language.

Now let us examine a few details on this newly created project.

Firstly, in the Solution explorer files, we can see three new files have created.

IService.cs

Service1.svc

Service1.svc.cs

Once we open the IService.cs file we can see the a interface and a class. The interface looks something like this(in C#),

[ServiceContract]
public interface IService1
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here
}

If we examine the Interface code carefully, we find an attribute called ServiceContract. Well ServiceContract is something similar to a normal Service Contract in the real world. We some one agree to provide any service to their customers they need to tell the customers what the services they can offer. This is one service of such kind. This interface with ServiceContract attribute will have methods that will be exposed as the Service to the public. If you are not sure what does the expose to the public means, don’t panic, you can understand in the meanwhile.

So now, for whatever class we need to expose to the public will implement the interface that has the ServiceContract attribute. Well, then what about the attribute called OperationContract. This is nothing but the method that we need to expose to the public. For e.g., if we have 10 methods in our interface and out 10 we need to provide 5 methods to the public and the remaining 5 need not be. So now for the 5 methods which we planned to publish should have the attribute [OperationContract]. And the methods that do not have this attribute will never be exposed to the public.

Also note that this service contract(interface) has defined a using namespace called as System.ServiceModel

That’s all now for the Service Interface. Then what about the class that we see at the bottom which looks here,

[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

We can see two kind of attributes here, namely [DataContract] and [DataMember]. For e.g., we need to expose the CompositeType to the public as a data type, then we can go for this attribute for our CompositeType. Since, WCF will support the standard data types as well as the data types which are surrounded with the attribute [DataContract]. And the [DataMember] is the property that we are allowed to provide for the service. And now we can add this class as the parameters as well as the return type as this,

CompositeType GetDataUsingDataContract(CompositeType composite)

{

}

Now, coming to the file named Service1.svc.cs, where we can find the actual service class that is to be exposed to the public.

public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}

Well, this class is nothing but the implementation of the IService1 interface as we saw earlier. This may have some user defined logic or some database operations etc.

While we try to open the file named Service1.svc we can see that the file will never open. In order to open this file make a right click on the file and choose Open With.. In the Open With dialog box you can select the Web Service Editor(Default) and click on the open button.

Open With..

In that file you can find something similar to this,

<%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.Service1" CodeBehind="Service1.svc.cs" %>

This has the different attributes such as CodeBehind, Debug, Service and Language where we can customize or go for default.

Once we open the web.config file we can find an element called as system.serviceModel which will look as follow,

<system.serviceModel>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
<!-- Service Endpoints -->
<endpoint address="" binding="wsHttpBinding" contract="WcfService1.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService1.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

However, this is the main part of the WCF goes on where we need to define where we host the service and how what are the bindings and what are the data contracts which is generally called as the ABC’s of the WCF.

Where

A= Address we host our service

B= Binding method we use for the service

C= Service Contract that we use.

This is a simple way of creating the Service. In the upcoming posts we can see how to consume the service, the security issues, transactions etc.