here is an example of a basic duplex communication setup using the WCF (Windows Communication Foundation) framework in C#:
Server-side:
c#
using System;
using System.ServiceModel;
[ServiceContract(CallbackContract = typeof(IServerCallback))]
interface IServer
{
[OperationContract]
void ProcessRequest(string request);
}
interface IServerCallback
{
[OperationContract]
void SendResponse(string response);
}
class Server : IServer
{
public void ProcessRequest(string request)
{
// Do some processing on the request
string response = "Coloring of the product 2123";
// Send the response back to the client
var callback = OperationContext.Current.GetCallbackChannel<IServerCallback>();
callback.SendResponse(response);
}
}
class Program
{
static void Main(string[] args)
{
// Set up the server
var binding = new NetTcpBinding();
var address = new Uri("net.tcp://127.0.0.1:8029/Server");
var host = new ServiceHost(typeof(Server));
host.AddServiceEndpoint(typeof(IServer), binding, address);
host.Open();
Console.WriteLine("Server running...");
Console.ReadLine();
}
}
Client-side:
C#
using System;
using System.ServiceModel;
[CallbackBehavior(UseSynchronizationContext = false)]
class Client : IServerCallback
{
public void SendResponse(string response)
{
// Handle the response
Console.WriteLine("Response: " + response);
}
static void Main(string[] args)
{
// Set up the client
var binding = new NetTcpBinding();
var address = new Uri("net.tcp://127.0.0.1:8029/Server");
var factory = new DuplexChannelFactory<IServer>(new Client(), binding, address);
var client = factory.CreateChannel();
// Send a request to the server
client.ProcessRequest("Process request for product 2123");
Console.ReadLine();
}
}