0

您好,提前感谢您的帮助。我正在使用 WCF 服务处理 silverlight 应用程序。我有一个包含三个操作合同的服务合同:GetModuleObjects()、GetServerObjects() 和 GetMonitorGroupObjects(),它们分别从数据库中检索模块、服务器和服务器组。我有将三个事件添加到 Main 中的 Service Client 实例的代码。前两个事件正确触发,但由于某种原因,第三个事件中的代码永远无法到达,这是一个问题。我在 Main.xaml.cs 中的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightTreeviewTest.ServiceReference1;
using System.Collections.ObjectModel;

namespace SilverlightTreeviewTest
{
    public partial class MainPage : UserControl
    {
        static public ObservableCollection<ModuleObject> TableModuleObjects = new ObservableCollection<ModuleObject>();
        static public ObservableCollection<ServerObject> TableServerObjects = new ObservableCollection<ServerObject>();
        static public ObservableCollection<ServerGroupOCollection> TableMonitorGroupObjects = new ObservableCollection<ServerGroupOCollection>();

        public MainPage()
        {

            InitializeComponent();
            Service1Client MyWebService = new Service1Client();


            MyWebService.GetModuleObjectsCompleted += new EventHandler<GetModuleObjectsCompletedEventArgs>(MyWebService_GetModuleObjectsCompleted);
            MyWebService.GetModuleObjectsAsync();
        }

        void MyWebService_GetModuleObjectsCompleted(object sender, GetModuleObjectsCompletedEventArgs e)
        {
            dataGrid1.ItemsSource = e.Result;
            foreach (DataTest_ModuleObject item in e.Result)
            {
                ModuleObject module = new ModuleObject((string)item.ModuleName,(int)item.ModuleStatus,(int)item.ServerID,(int)item.ID);
                TableModuleObjects.Add(module);
            }

            Service1Client WebServiceForServers = new Service1Client();
            WebServiceForServers.GetServerObjectsCompleted += new EventHandler<GetServerObjectsCompletedEventArgs>(WebServiceForServers_GetServerObjectsCompleted);
            WebServiceForServers.GetServerObjectsAsync();
        }

        void WebServiceForServers_GetServerObjectsCompleted(object sender, GetServerObjectsCompletedEventArgs e)
        {
            //get each server entry from table and create ServerObject for each
            //find the modules from the module collection then add server to server collection
            foreach (DataTest_ServerObject item in e.Result)
            {
                ServerObject server = new ServerObject((string)item.ServerName,(int)item.ID,(int)item.Group_Id);
                server.FindModules(TableModuleObjects);
                TableServerObjects.Add(server);

            }

            //fire event to retrieve Monitor groups from db and assign servers to them
            Service1Client WebServiceForGroups = new Service1Client();
            WebServiceForGroups.GetMonitorGroupObjectsCompleted += new EventHandler<GetMonitorGroupObjectsCompletedEventArgs>(WebServiceForGroups_GetMonitorGroupObjectsCompleted);
            WebServiceForGroups.GetServerObjectsAsync();
        }

        void WebServiceForGroups_GetMonitorGroupObjectsCompleted(object sender, GetMonitorGroupObjectsCompletedEventArgs e)
        {
            //get each MonitorGroup entry from table and create MonitorGroupObject(ServerGroupOCollection) for each
            //find associated servers and then add the group to the collection of groups
            foreach (DataTest_MonitorGroupObject item in e.Result)
            {
                ServerGroupOCollection mGroup = new ServerGroupOCollection((string)item.MonitorGroup_Name, (int)item.ID, (int)item.MonitorGroup_Type);
                mGroup.FindServers(TableServerObjects);
                TableMonitorGroupObjects.Add(mGroup);
            }
        }

    }
}

我不明白为什么第三个事件永远不会被触发并且它的代码永远不会到达。请帮忙!

4

1 回答 1

4

在 WebServiceForServers_GetServerObjectsCompleted 中,您正在为 GetMonitorGroupObjectsCompleted 注册一个 EventHandler,然后调用 GetServerObjectsAsync 方法。

当然,您的意思是调用 GetMonitorGroupObjectsAsync (或类似的)。

于 2012-08-09T20:00:07.000 回答