I want to create a duplex NetTcp service. I am not sure what is the best approach. I have a client that periodically sends it's status to the server and I have a server that has to send periodically data that is independent of any client request. Because i want avoid two connections and don't know anything about the clients at the server, I have to use the connection that is opened by the client. So like I said the client sends periodically status information. But how do I use the connection channel established from the client independantly for sending data to the client. Also the data send from server to client does need a response.
[ServiceContract(CallbackContract = typeof(IStatusServiceCallBack))]
public interface IStatusService
{
[OperationContract]
void SendStatus(int statusCode, string statusMessage);
}
public interface IStatusServiceCallBack
{
// I know IsOneWay=true cannot work, but how to return value????
[OperationContract(IsOneWay = true)]
int SendPayTransaction(PayTransaction payTransaction);
}
public class StatusService : IStatusService
{
public IStatusServiceCallBack Proxy
{
get
{
return OperationContext.Current.GetCallbackChannel
<IStatusServiceCallBack>();
}
}
public void SendNotification(int statusCode, string statusMessage)
{
Console.WriteLine($"\nClient status : {(statusCode)} {statusMessage}");
}
// Is this possible???
public int SendPayTransaction(PayTransaction payTransaction){
return Proxy.SendPayTransaction(payTransaction)
}
}
class Program
{
static void Main(string[] args)
{
var svcHost = new ServiceHost(typeof(NotificationService));
svcHost.Open();
bool closeService = false;
do{
string command = Console.ReadLine();
if(command == "Send"){
// Is this possible???????
int result = svcHost.SendPayTransaction(new PaymentTransaction(){Amount = 5.50});
Console.WriteLine($"Result of payment : {result}");
} else if (command == "exit"){
closeService = true;
}
}while(!closeService);
}
}