Service contract:
Create a project WcfServiceContract. Under the project WcfServiceContract
create a interface WcfServiceContractInterface
file name: WcfServiceContractInterface.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace Teacherone.WcfNetBinding
{
[ServiceContract]
public interface WcfServiceContractInterface
{
[OperationContract]
string ProcessCommand(string commandname);
}
}
Service contract implementation:
Create a project WcfServiceContractImplementation. Under the project WcfServiceContractImplementation
create a class WcfServiceContractImplementation
file name: WcfServiceContractImplementation.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Teacherone.WcfNetBinding;
namespace Teacherone.WcfNetBinding
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class WcfServiceContractImplementation : WcfServiceContractInterface
{
public string ProcessCommand(string commandname)
{
return "Processed >>>" + commandname + "<<< Processed";
}
}
}
Till now we have created the contract and implemented it. Now we need to host the service.
We will hosting the service from console
Hosting the service:
Create a project WcfServiceHost. Under the project WcfServiceHost
create a class WcfServiceHost
file name: WcfServiceHost.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace Teacherone.WcfNetBinding
{
public class WcfServiceHost
{
public static void Main(string[] args)
{
ServiceHost selfconsolehost =
new ServiceHost(typeof(Teacherone.WcfNetBinding.WcfServiceContractImplementation));
try
{
selfconsolehost.AddServiceEndpoint(
typeof(Teacherone.WcfNetBinding.WcfServiceContractInterface),
new NetTcpBinding(), "net.tcp://localhost:8000");
selfconsolehost.Open();
Console.WriteLine("Service that uses NetTcpBinding is ready to accept calls ...");
Console.WriteLine("press to stop service ...");
Console.ReadLine();
selfconsolehost.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
Now create a client program to test the service
Create the client program:
Create a project WcfClient. Under the project WcfClient
create a class Program
file name: Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Teacherone.WcfNetBinding;
namespace Teacherone.WcfClient
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*** Wcf demo using NetTcpBinding starts *** ");
try
{
ChannelFactory cf =
new ChannelFactory( new NetTcpBinding(), "net.tcp://localhost:8000");
WcfServiceContractInterface s = cf.CreateChannel();
//Call the service :
Console.WriteLine(s.ProcessCommand("command-1"));
Console.WriteLine(s.ProcessCommand("command-2"));
Console.WriteLine(s.ProcessCommand("command-3"));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Hit to exit...");
Console.Read();
}
}
}
How to run and test
Step-1: First start the service by executing "WcfNetBinding.exe".
("WcfNetBinding.exe" is created by building WcfServiceHost.cs)
It will appear as follow:
D:\demo\WcfNetBinding\WcfNetBindingHost\bin\Release>WcfNetBinding.exe
Service that uses NetTcpBinding is ready to accept calls ...
press enter to stop service ...
Step-2: Now run the client "WcfClient.exe".
("WcfClient.exe" is created by building Programt.cs)
It will appear as follow:
D:\demo\WcfNetBinding\WcfClient\bin\Release>WcfClient.exe
*** Wcf demo using NetTcpBinding starts ***
Processed >>>command-1<<< Processed
Processed >>>command-2<<< Processed
Processed >>>command-3<<< Processed
Hit enter to exit...
|