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.

1 comment:

  1. Good article for beginers like me....

    Hoping to get more posts on WCF.....

    keep it up!!! cheers... :)

    ReplyDelete