title: WCF NamedPipes Service and WPF Client
body: <p>I'm working with a windows service where i want to add a GUI too it. I made a proof of concept with my service creating a ServiceHost object and hosting the WCF Named pipes service and then i Console application to consume the WCF service and also getting callback responses from the service (messages sent from server to connected clients). This works great my console application runs and get responses from the service with no interruption or delays.</p>
  
  <p>However when doing the same thing in my WPF GUI application when clicking a button that then calls the WCF service it freezes the whole UI thread and then after a couple of minutes throws an exception and then the UI is updated with the message callback (server sends message to connected clients) but any return values from service is lost since the exception was thrown.</p>
  
  <p>The two exception messages i have gothen is theses (the most common is the first):</p>
  
  <p>1: <strong>The requesting action sent to net.pipe :/ / localhost / PipeGUI did not receive a response within the specified timeout (00:00:59.9989999). The time allotted to this operation may have been a portion of a longer timeout. This may be because the service is still processing the operation or because the service was unable to send a reply message. Raise the deadline for action (by entering the channel / proxy to Icon Text Channel and set the property Operation Timeout) and verify that the service can connect to the client.</strong></p>
  
  <p>2: <strong>Communication object System.ServiceModel.Channels.ServiceChannel, can not be used for communication because it has been canceled.</strong></p>
  
  <p>Anyone got any ideas why this is happeing ?
  I can post more code if neccessary.</p>
  
  <p><strong>UPDATE , added code for reference</strong></p>
  
  <pre><code>public interface IClientCallback
  {
      [OperationContract(IsOneWay = true)]
      void MessageRecived(string message);
  }
  
  [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IClientCallback))]
  public interface IPipeServiceContract
  {
      [OperationContract]
      string Hello();
  
      [OperationContract]
      void Message(string msg);
  
      [OperationContract(IsInitiating = true)]
      void Connect();
  
      [OperationContract(IsTerminating = true)]
      void Disconnect();
  }
  
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true, UseSynchronizationContext = false)]
  public class PipeService : IPipeServiceContract
  {
      List&lt;IClientCallback&gt; _clients = new List&lt;IClientCallback&gt;();
  
      public string Hello()
      {
          PublishMessage("Hello World.");
          return "Return from method!";
      }
  
      public void Connect()
      {
          _clients.Add(OperationContext.Current.GetCallbackChannel&lt;IClientCallback&gt;());
      }
  
      public void Disconnect()
      {
          IClientCallback callback = OperationContext.Current.GetCallbackChannel&lt;IClientCallback&gt;();
          _clients.Remove(callback);
      }
  
      void PublishMessage(string message)
      {
          for (int i = _clients.Count - 1; i &gt; 0; i--)
          {
              try
              {
                  _clients[i].MessageRecived(message);
              }
              catch (CommunicationObjectAbortedException coae)
              {
                  _clients.RemoveAt(i);
              }
              catch(CommunicationObjectFaultedException cofe)
              {
                  _clients.RemoveAt(i);
              }
          }
      }
  
  
      public void Message(string msg)
      {
          PublishMessage(msg);
      }
  }
  
  
  /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class MainWindow : Window, INotifyPropertyChanged, IClientCallback
  {
      public ServiceController Service { get; set; }
  
      protected IPipeServiceContract Proxy { get; set; }
      protected DuplexChannelFactory&lt;IPipeServiceContract&gt; PipeFactory { get; set; }
  
      public ObservableCollection&lt;ServerActivityNotification&gt; Activity { get; set; }
  
      public override void BeginInit()
      {
          base.BeginInit();
          PipeFactory = new DuplexChannelFactory&lt;IPipeServiceContract&gt;(this, new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/PipeGUI"));
      }
  
      public MainWindow()
      {
          InitializeComponent();
          Activity = new ObservableCollection&lt;ServerActivityNotification&gt;();
          Service = ServiceController.GetServices().First(x =&gt; x.ServiceName == "Server Service");
          NotifyPropertyChanged("Service");
          var timer = new DispatcherTimer();
          timer.Tick += new EventHandler(OnUpdate);
          timer.Interval = new TimeSpan(0, 0, 0, 0, 850);
          timer.Start();
  
          if (Service.Status == ServiceControllerStatus.Running)
          {
              Proxy = PipeFactory.CreateChannel();
              Proxy.Connect();
          }
      }
  
      void OnUpdate(object sender, EventArgs e)
      {
          Service.Refresh();
          NotifyPropertyChanged("Service");
  
          StartButton.IsEnabled = Service.Status != ServiceControllerStatus.Running ? true : false;
          StopButton.IsEnabled = Service.Status != ServiceControllerStatus.Stopped ? true : false;
  
          if (PipeFactory != null &amp;&amp; Service.Status == ServiceControllerStatus.Running)
          {
              Proxy = PipeFactory.CreateChannel();
              Proxy.Connect();
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      public void NotifyPropertyChanged(string name)
      {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(name));
      }
  
      private void OnStart(object sender, RoutedEventArgs e)
      {
          try
          {
              Service.Start();
          }
          catch
          {
              Service.Refresh();
          }
      }
  
      private void OnStop(object sender, RoutedEventArgs e)
      {
          try
          {
              if (Proxy != null)
              {
                  Proxy.Disconnect();
                  PipeFactory.Close();
              }
              Service.Stop();
          }
          catch
          {
              Service.Refresh();
          }
      }
  
      public void MessageRecived(string message)
      {
          Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =&gt;
          {
              ServerActivityNotification log = new ServerActivityNotification { Activity = message, Occured = DateTime.Now };
              Activity.Add(log);
              ListBoxLog.ScrollIntoView(log);
              NotifyPropertyChanged("Activity");
          }));
      }
  
      private void OnHello(object sender, RoutedEventArgs e)
      {
          try
          {
              Proxy.Message(txtSendMessage.Text);
          }
          catch (Exception ex)
          {
              Debug.WriteLine(ex.Message);
          }
      }
  }
  </code></pre>
  
  <p>}</p>