Tuesday, November 27, 2007

WCF

Windows Communication Foundation? WCF? Waatzeet?
I will discuss about what WCF is a little later on. But now we will have to summarise how two different Applications can interact with each other. We have remoting which allows two different applications in different application domains to interact with each other. We have MSMQ using which two different applications can pass messages between them. We have WebServices using which we can make two applications interact.but all these have some shortcomings. For exampleMSMQ will be able to run on only windows machines.two applications have to conform to some specific standards if they have to use web services. Remoting will be possible only if both the applications are written in dot net.so to over come all these shortcomings, microsoft has come up with a new set of APIs to enable communication. This is Windows Communication Framework.
This is similar to web services (Service Oriented Architecture) , but WCF is flexible and its core serivices can be extended to suit our needs.so as industry standards grow, we can extend the WCF core.
WCF can operate with various things like MSMQ,Pipes, HTTP etc. This will be released with dot net framework 3.0 and orcas.
So without further delay we will develop a sample to enable us to understand better.
Create a new project of type “WCF service Library”
A class and a interface are created by default, delete them off. Also we have to add the following two “using statements” and references to “System.ServiceModel”
Using System.ServiceModel
Using System.Runtime.Serialization

Now we will start coding our custom interface and then implement taht interface.
[ServiceContract]
public interface IMathLIB
{
[OperationContract]
int Add(int val1,int val2);

}
Here, the ServiceContract attribuite specifies that the interface is the contract for the service. Not getting it? Ok ,WCF works on 3 basic principles : ABC, address,binding ,contract. If you know COM, you will know that we have an interface to specify the service provided. And then we use QueryInterface on the Factory class to get that interface (type casted object). This is nothing but contract. So IMathLIB is the service contract which we will be exposing (C of WCF). Now the other Attribute is “OperationContract”- this signifies that the operation is being exposed by the service.now we have to implement this interface :
public class MathService : IMathLIB
{
public int Addint(int val1,int val2)
{
return val1+val2;
}
}

Now we have a MathService class which implements IMathLIB.build this , a DLL is generated..lets name it as MathService.
Now is this enuf? No not at all..all we have doen is just created a service. Now we have to host that service in some other application and run that host application. This forms the server part of WCF. Then we have t ocraete a client to consume this service.
Creating the server is simple :
Create a simple cnsole application projecct and name it as MathServiceHost, add a refrence to “System.ServiceModel”, “System.IdentityModel”, and “System.Runtime.Serialization”. now add a reference to “MathService.dll”.
Now we will start coding for the host
internal class MathServiceHost
{
internal static ServiceHost myServiceHost = null;

internal static void StartService()
{
//Consider putting the baseAddress in the configuration system
//and getting it here with AppSettings
Uri baseAddress = new Uri("http://localhost:8000/MathService");

//Instantiate new ServiceHost
myServiceHost = new ServiceHost(typeof(MathService), new Uri( "http://localhost:8000/MathService"));

//Open myServiceHost
myServiceHost.Open();
}

internal static void StopService()
{
//Call StopService from your shutdown logic (i.e. dispose method)
if (myServiceHost.State != CommunicationState.Closed)
myServiceHost.Close();
}
}
Now add config file :




address="http://localhost:8000/MathService"
contract="MathServiceLIB.IMathLIB"
binding="basicHttpBinding"/>






Look at the address, binding and contract in the config file.it completes the jigsaw puzzle,does i t not?

Build this and it builds without any errors. So this is fine.
Now to develop a client :
Just create a simple console application named “MathClient” and then add a “service refrence “ this the URL specified in the address in the config file (shown above) and then we can use the service just like we use web services.  bingo-- that does it.
But what if we want to add a new class in the service ? can we have operations on that class?
Well, we can!!!!
How? I will tell you .
Change your IMathLIB as shown below
[ServiceContract]
public interface IMathLIB
{
[OperationContract]
int Addint(int val1,int val2);
[OperationContract]
DataContract1 Add(DataContract1 dataContractValue,DataContract1 dataContractValue2);
}
And then ad the new class type which you want

[DataContract]
public class DataContract1
{
string firstName;
string lastName;

[DataMember]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
[DataMember]
public string LastName
{
get { return lastName; }
set { lastName = value; }
}

public DataContract1 Add(DataContract1 data)
{
DataContract1 dc = new DataContract1();
dc.FirstName = this.firstName + data.FirstName;
dc.LastName = this.LastName + data.LastName;
return dc;
}
}

The attribute DataContract specifies that it is a datatype and the atribute “Datamember” specifies that it is a field.
Now w can code for our MathService class and implement the IMathLIB interface.
The rest is all as we did before 

Hope this helps u.
Happy Coding !!!
Regards,
nandan

No comments: