Sender.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using NamedPipe.Communication;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.ServiceModel;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace NamedPipe
  9. {
  10. public class Sender
  11. {
  12. private static Object _Lock = new Object();
  13. private static EndpointAddress _endpointAddress = new EndpointAddress(String.Format("{0}/{1}", PipeService.URI, Receiver.DefaultPipeName));
  14. /// <summary>
  15. /// Attempts to send the message to the proxy at the pre-configured endpoint
  16. /// </summary>
  17. /// <param name="message">The message to send</param>
  18. /// <returns>True, upon success</returns>
  19. public static Boolean SendMessage(String message)
  20. {
  21. var success = false;
  22. try
  23. {
  24. lock (_Lock) //ensure thread exclusivity when sending messages across the wire
  25. {
  26. var proxy = ChannelFactory<IPipeService>.CreateChannel(new NetNamedPipeBinding(), _endpointAddress);
  27. proxy.RecieveMessage(message);
  28. }
  29. success = true;
  30. }
  31. catch (Exception ex) //Most likely, there was nobody to send a message to.
  32. { } //TODO : Add some logging
  33. return success;
  34. }
  35. }
  36. }