WCF (Windows Communication Foundation) made simple


This article includes creating a simple WCF Service, WCF Service Contract along with hosting and consuming WCF Service. This article does not require you to have Visual Studio installed in your system. You can use Notepad to write your code and compile it from Command window. A Complete Source code is also included along with compilation steps that you can refer to understand the topic properly.


A Simple end-to-end WCF application




Writing Source Code

No need to use Visual Studio and No need to create WCF.config.
For WCF learning purpose just create a "SelfHost" folder and write following four files in the folder with Notepad.

1) Service Contract
2) The Service
3) Hosting Program
4) Test Client

We will take all these files one-by-one...


1) Service Contract:
This part, defined the contract that would be known by the WCF-clients. It can be an interface with some methods


IEMICalculator.cs




using System;
using System.ServiceModel;

[ServiceContract]
public interface IEMICalculator
{
[OperationContract]
double GetInterestComponent(double principalAmount, double interestRate);

[OperationContract]
double GetPrincipalComponent(double principalAmount, double interestRate, double EMIAmount);
}




2) The service:

This part implements the Service Contract interface and make WCF-clients able to call WCF-service methods.

EMIService.cs



using System;
using System.ServiceModel;

public class EMIService : IEMICalculator
{
public double GetInterestComponent(double principalAmount, double interestRate)
{
double interestComponent = 0;
interestComponent = ((principalAmount * interestRate) / 100 ) / 12;
return interestComponent;
}

public double GetPrincipalComponent (double principalAmount, double interestRate, double EMIAmount)
{
double interestComponent = 0;
interestComponent = ((principalAmount * interestRate) / 100 ) / 12;
double principalComponent = EMIAmount - interestComponent;
return principalComponent;
}
}





3) Hosting Program

This program creates an instance of ServiceHost class with Service Endpoint (Address, Binding and Contract), starts WCF service for clients to consume its methods.

HostingWCF.cs



using System;
using System.ServiceModel;

public class HostingWCF
{
public static void Main()
{
BasicHttpBinding binding = new BasicHttpBinding();
Uri address = new Uri ( "http://127.0.0.1:9000" ) ;
ServiceHost host = new ServiceHost ( typeof(EMIService) , address );
host.AddServiceEndpoint ( typeof(IEMICalculator), binding, "EMIService" );
host.Open();
Console.WriteLine("Service Is Started (press a key to stop)...");
Console.ReadKey();
host.Close();
}
}




4) Test Client

This program instentiates ChannelFactory class (with Address, Binding, Contract) and creates a service proxy. Once proxy is created, clients can call Service methods.

ConsumeWCF.cs



using System;
using System.ServiceModel;

public class ConsumeWCF
{
public static void Main()
{
try
{
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress ( "http://127.0.0.1:9000/EMIService" ) ;
ChannelFactory factory = new ChannelFactory(binding, address);
IEMICalculator proxy = factory.CreateChannel();
double intComp = proxy.GetInterestComponent(1000000, 8.25);
double prinComp = proxy.GetPrincipalComponent(1000000, 8.25, 20000);
Console.WriteLine("Interest Component: Rs." + intComp.ToString());
Console.WriteLine("Principal Component: Rs." + prinComp.ToString());
}
catch
{

}
}
}




Compilation

By now, your code is ready, what's remaining is the compilation of following Parts:

1. Service Part
2. Hosting Part
3. Test Client Part

We will take all these one-by-one...


1) Compile Service Part:



D:\SelfHost>csc /t:library /out:EMIService.dll EMIService.cs IEMICalculator.cs



Here,
/t:library determines that the target will be a library file (DLL)
/out: determines the name of library file
and remaining files (*.cs) are the the source files used for compilation

Now, your WCF service is ready as EMIService.dll in the folder.


2) Compile Hosting Part:



D:\SelfHost>csc /t:exe /r:EMIService.dll HostingWCF.cs



Here,
/t:exe determines that the target will be an executable file (EXE)
/r:EMIService.dll determines the name of library file to include in the compilation
and remaining file (*.cs) is the source file used for compilation

Now, your WCF Host is ready as HostingWCF.exe in the folder.


3) Compile Test Client Part:



D:\SelfHost>csc /t:exe /r:EMIService.dll ConsumeWCF.cs



Here,
/t:exe determines that the target will be an executable file (EXE)
/r:EMIService.dll determines the name of library file to include in the compilation
and remaining file (*.cs) is the source file used for compilation

Now, your WCF Client is ready as ConsumeWCF.exe in the folder.



Testing your Service

Now we want to test our service. For that follow these steps ...

1) Start one command windows ( Start -> Run -> cmd ), locate SelfHost folder and execute HostingWCF.exe
2) Start other commend windows ( Start -> Run -> cmd ), locate SelfHost folder and execute ConsumeWCF.exe.


Compare your outputs with attached snapshots. If you get same outputs means you are done!!


Hope this article helps you to start with WCF.

Thanks
Sharad


Attachments

  • Service Contract (42830-16655-IEMICalculator.cs.txt)
  • The Service (42830-16656-EMIService.cs.txt)
  • Hosting Program (42830-16656-HostingWCF.cs.txt)
  • Test Client (42830-16656-ConsumeWCF.cs.txt)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: