0

我希望服务器使用 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);
            }
        }

不幸的是,有时在评论指定的点上,我得到一个中止的 Udp 频道:

通信对象 System.ServiceModel.Channels.UdpChannelFactory+ClientUdpDuplexChannel 不能用于通信,因为它已被中止。

有没有人知道为什么?

4

3 回答 3

0

由于它是异步操作,线程在执行 FindAsync(criteria) 方法后终止。只是在方法调用后写了 Console.Readline() 或使用 Autoreset 事件保持线程。

于 2014-08-19T06:27:12.830 回答
0

好吧,这并不能回答您的问题,但是我对您的代码有些警惕。这似乎从根本上正确,但感觉你的发现可能运行得非常快。我会在一个单独的线程中实现重复发现,并有一些睡眠时间,只是为了让网络更快乐。只是想清理代码。抱歉,如果这没有帮助。

  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);
    }
  }
于 2011-08-18T19:19:46.507 回答
0

可能是您办公室的某些网络基础设施断开了连接。

您应该编写代码来检查中止的通信,并从中恢复。

要恢复,您可以关闭已中止的频道并创建一个新频道。

于 2011-03-18T10:22:36.703 回答