Alexander Yezutov
Alexander Yezutov

Reputation: 3214

Why WCF Discovery Udp channel gets aborted

I want the server to constantly track for available clients using WCF Discovery.

        public void Start()
        {
            findCriteria = new FindCriteria(typeof(ITestRunnerAgent))
                               {
                                   Scopes = {new Uri(scope)},
                                   Duration = TimeSpan.FromMilliseconds(DiscoveryIntervalInMiliseconds)
                               };
            discoveryClient = GetInitilizedDisoveryClient();
            discoveryClient.FindAsync(findCriteria);
        }

        private DiscoveryClient GetInitilizedDisoveryClient()
        {
            var client = new DiscoveryClient(new UdpDiscoveryEndpoint());
            client.FindProgressChanged += OnFindProgressChanged;
            client.FindCompleted += OnFindCompleted;
            return  client;
        }

        private void OnFindCompleted(object sender, FindCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                // HERE! Sometimes e.Error is not null, but as described in question
                discoveryClient.FindAsync(findCriteria);
            }
        }

Unfortunately, sometimes at the point specified by comment i get an aborted Udp channel:

The communication object, System.ServiceModel.Channels.UdpChannelFactory+ClientUdpDuplexChannel, cannot be used for communication because it has been Aborted.

Has anyone ideas why?

Upvotes: 0

Views: 675

Answers (3)

Sai
Sai

Reputation: 1

As it is asynchronous operation the thread terminates after executing FindAsync(criteria) method. just wrote Console.Readline() after method call or use Autoreset event hold the thread.

Upvotes: 0

rocketsarefast
rocketsarefast

Reputation: 4170

Well, this doesn't answer your question, but I feel a little wary about your code. It seems fundamentally correct, but it feels like your discovery could be running very fast. I would implement recurring discovery in a separate thread with some sleep time just to make the network happier. Just a thought to clean up the code. Sorry if this doesn't help.

  public void Start()
  {
    var bw = new System.ComponentModel.BackgroundWorker();
    bw.DoWork += new System.ComponentModel.DoWorkEventHandler(DiscoveryThread);
    bw.RunWorkerAsync();
  }
  private void DiscoveryThread(object sender, System.ComponentModel.DoWorkEventArgs e)
  {
    var client = new DiscoveryClient(new UdpDiscoveryEndpoint());
    var findCriteria = new FindCriteria(typeof(ITestRunnerAgent))
                        {
                            Scopes = {new Uri(scope)},
                            Duration = TimeSpan.FromMilliseconds(DiscoveryIntervalInMiliseconds)
                        };
    while(true)
    {
      client.Find(findCriteria);
      // lock, clear, and add discovered endpoints to a global List of some sort
      System.Threading.Thread.Sleep(3000);
    }
  }

Upvotes: 0

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65451

It could be that some network infrastructure at your office is droping the connections.

You should write your code to check for aborted communication, and recover from it.

To recover you could close down the aborted channel and create a new one.

Upvotes: 0

Related Questions