简体   繁体   English

我的WCF服务方法多次调用

[英]My WCF Service methods called multiple times

I have updated my questions with a short and direct questions as mentioned below: I am aware of basic knowledge on MVVM, but I do not know its implementation. 我已经用如下所述的简短直接问题更新了我的问题:我了解MVVM的基本知识,但我不知道它的实现。 Problems:-- 问题: -

  • For the first time all the methods such as private void UserControl_Loaded(),void serviceClient_GetDeptIDNameCompleted(),private void cbDeptName_SelectionChanged(), void serviceClient_GetSectionsListCompleted(), private void cbDeptSections_SelectionChanged(), void serviceClient_GetSectionDetailCompleted() are calling once and sequentially one after another. 第一次所有方法,例如private void UserControl_Loaded(),void serviceClient_GetDeptIDNameCompleted(),private void cbDeptName_SelectionChanged(),void serviceClient_GetSectionsListCompleted(),private void cbDeptSections_SelectionChanged(),void serviceClient_GetSectionDetailCompleted(),一次又一次地依次调用。
  • If I am changing any selection from any of the combobox then each and every methods are calling several(variable) times. 如果我从任何组合框中更改任何选择,则每个方法都调用几次(可变)次。

I think its because of bad implementation or low standard coding. 我认为这是因为执行不良或标准编码低。 Can any body please suggest me with a good implementation. 任何人都可以建议我做好实施。 Thanks in advance. 提前致谢。

UPDATE:- my a.xml 更新: - 我的a.xml

<Departments>

    <Department>

        <deptID>
            1
        </deptID>

        <deptName>
            Arts
        </deptName>

        <Sections>

            <Section>
                <sectionID>A</sectionID>
                <sectionName>Political Science</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>B</sectionID>
                <sectionName>Economics</sectionName>
                <NoOfLecturers>3</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>C</sectionID>
                <sectionName>Social Studies</sectionName>
                <NoOfLecturers>7</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>D</sectionID>
                <sectionName>History</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>E</sectionID>
                <sectionName>Geography</sectionName>
                <NoOfLecturers>2</NoOfLecturers>
            </Section>

        </Sections>

    </Department>

    <Department>

        <deptID>
            2
        </deptID>

        <deptName>
            Sciences
        </deptName>

        <Sections>

            <Section>
                <sectionID>A</sectionID>
                <sectionName>Physics</sectionName>
                <NoOfLecturers>10</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>B</sectionID>
                <sectionName>Mathematics</sectionName>
                <NoOfLecturers>2</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>C</sectionID>
                <sectionName>Chemistry</sectionName>
                <NoOfLecturers>6</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>D</sectionID>
                <sectionName>Botany</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>E</sectionID>
                <sectionName>Zeology</sectionName>
                <NoOfLecturers>4</NoOfLecturers>
            </Section>

        </Sections>

    </Department>

    <Department>

        <deptID>
            3
        </deptID>

        <deptName>
            Commerce
        </deptName>

        <Sections>

            <Section>
                <sectionID>A</sectionID>
                <sectionName>Accounting</sectionName>
                <NoOfLecturers>3</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>B</sectionID>
                <sectionName>Marketing</sectionName>
                <NoOfLecturers>2</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>C</sectionID>
                <sectionName>Human Resources</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>D</sectionID>
                <sectionName>Finance</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

        </Sections>

    </Department>

    <Department>

        <deptID>
            4
        </deptID>

        <deptName>
            Library
        </deptName>

        <Sections>

            <Section>
                <sectionID>A</sectionID>
                <sectionName>Incharge</sectionName>
                <NoOfLecturers>3</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>B</sectionID>
                <sectionName>Clerk</sectionName>
                <NoOfLecturers>3</NoOfLecturers>
            </Section>

        </Sections>

    </Department>

</Departments>

my WCFService.cs class(Service1.svc.cs) 我的WCFService.cs类(Service1.svc.cs)

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        XmlDocument doc;

        string xmlPath = @"D:\WcfService1\WcfService1\Resources\a.xml";

        public Dictionary<string, string> GetDeptIDName()
        {
            try
            {
                if (!File.Exists(xmlPath))
                {
                    return null;
                }
                else
                {
                    Dictionary<string, string> depIDName = new Dictionary<string, string>();
                    doc = new XmlDocument();
                    doc.Load(xmlPath);
                    XmlNodeList nl = doc.GetElementsByTagName("Department");

                    foreach (XmlNode node in nl)
                    {
                        string id = (node.FirstChild.InnerXml).Replace("\r", "").Replace("\n", "").Replace("\t", "");
                        string name = (node.FirstChild.NextSibling.InnerXml).Replace("\r", "").Replace("\n", "").Replace("\t", "");
                        depIDName.Add(id, name);
                    }

                    return depIDName;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<string> GetSectionsList(string deptName)
        {
            try
            {
                if (!File.Exists(xmlPath))
                {
                    return null;
                }
                else
                {
                    List<string> lstSectionList = new List<string>();
                    doc = new XmlDocument();
                    doc.Load(xmlPath);
                    XmlNodeList nl = doc.GetElementsByTagName("Department");

                    foreach (XmlNode node in nl)
                    {
                        string name = (node.FirstChild.NextSibling.InnerXml).Replace("\r", "").Replace("\n", "").Replace("\t", "");
                        if (name == deptName)
                        {
                            UpdateSectionsList(ref lstSectionList, node.LastChild);
                        }
                    }

                    return lstSectionList;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void UpdateSectionsList(ref List<string> lstSectionList,
                                        XmlNode xmlNode)
        {
            XmlNodeList nl = xmlNode.ChildNodes;

            foreach (XmlNode xNode in nl)
            {
                foreach (XmlNode sectionNode in (XmlNodeList)xNode.ChildNodes)
                {
                    if ("sectionID" == sectionNode.Name)
                    {
                        lstSectionList.Add(sectionNode.InnerXml);
                    }
                }
            }
        }

        /// <summary>
        /// Get all the sections detail.
        /// </summary>
        /// <param name="deptName"></param>
        /// <param name="sectionID"></param>
        /// <returns></returns>
        public CompositeType GetSectionDetail(string deptName,
                                              string sectionID)
        {
            try
            {
                if (!File.Exists(xmlPath))
                {
                    return null;
                }
                else
                {
                    CompositeType compositeObj = new CompositeType();

                    doc = new XmlDocument();

                    doc.Load(xmlPath);

                    XmlNodeList nl = doc.GetElementsByTagName("Department");

                    foreach (XmlNode node in nl)
                    {
                        string name = (node.FirstChild.NextSibling.InnerXml).
                                       Replace("\r", "").
                                       Replace("\n", "").
                                       Replace("\t", "");

                        if (name == deptName)
                        {
                            UpdateSectionDetail(out compositeObj,
                                                GetSectionNodes(node.LastChild,
                                                                sectionID));
                        }
                    }

                    return compositeObj;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void UpdateSectionDetail(out CompositeType compositeObj,
                                         XmlNode xmlNode)
        {
            compositeObj =
                new CompositeType();

            compositeObj.SectionID = 
                xmlNode.FirstChild.InnerXml;

            compositeObj.SectionName = 
                xmlNode.LastChild.PreviousSibling.InnerXml;

            compositeObj.SectionLecturers =
                xmlNode.LastChild.InnerXml;
        }

        private XmlNode GetSectionNodes(XmlNode xmlNode,
                                        string sectionID)
        {
            try
            {
                XmlNodeList nl = xmlNode.ChildNodes;

                XmlNode returnNode = xmlNode;

                foreach (XmlNode xNode in nl)
                    foreach (XmlNode sectionNode in (XmlNodeList)xNode.ChildNodes)
                        if (("sectionID" == sectionNode.Name)
                                        &&
                            (sectionNode.InnerXml == sectionID))
                        {
                            returnNode = xNode;
                        }

                return returnNode;
            }
            catch(Exception)
            {
                return null;
            }
        }
    }

IService1.cs Interface IService1.cs接口

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        Dictionary<string, string> GetDeptIDName();

        [OperationContract]
        List<string> GetSectionsList(string deptName);

        [OperationContract]
        CompositeType GetSectionDetail(string deptName, string sectionID);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        string sectionID = string.Empty, sectionName = string.Empty, NoOfLectures = string.Empty;

        [DataMember]
        public string SectionID
        {
            get
            {
                return sectionID; 
            }
            set 
            {
                sectionID = value; 
            }
        }

        [DataMember]
        public string SectionName
        {
            get
            {
                return sectionName;
            }
            set
            {
                sectionName = value;
            }
        }
        [DataMember]
        public string SectionLecturers
        {
            get
            {
                return NoOfLectures;
            }
            set
            {
                NoOfLectures = value;
            }
        }
    }

MainPage.xaml.cs class MainPage.xaml.cs类

 public partial class MainPage : UserControl
    {
        Service1Client serviceClient = new Service1Client();

        public MainPage()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            serviceClient.GetDeptIDNameCompleted += new EventHandler<GetDeptIDNameCompletedEventArgs>(serviceClient_GetDeptIDNameCompleted);
            serviceClient.GetDeptIDNameAsync();
        }

        void serviceClient_GetDeptIDNameCompleted(object sender, GetDeptIDNameCompletedEventArgs e)
        {
            cbDeptName.ItemsSource = e.Result;
            cbDeptName.SelectedItem = cbDeptName.Items[0];
        }

        private void cbDeptName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //Remove square brackets from the List box item.
            string lstBoxItem = Regex.Replace(cbDeptName.SelectedValue.ToString(),
                                              @"[\[\]\s]",
                                              string.Empty);

            //Split the list box item into two parts by considering the (,)
            //to separate the DICOM ID and Patient name.
            string[] strItems = (lstBoxItem).Split(Convert.ToChar(","));

            serviceClient.GetSectionsListCompleted += new EventHandler<GetSectionsListCompletedEventArgs>(serviceClient_GetSectionsListCompleted);
            serviceClient.GetSectionsListAsync(strItems[1]);
        }

        void serviceClient_GetSectionsListCompleted(object sender, GetSectionsListCompletedEventArgs e)
        {
            cbDeptSections.ItemsSource = e.Result;
            cbDeptSections.SelectedItem = cbDeptSections.Items[0];
            //serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);
            //serviceClient.GetSectionDetailAsync(strItems[1], cbDeptSections.SelectedItem.ToString());
        }

        private void cbDeptSections_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (null == cbDeptSections.SelectedItem)
                return;

            lbSectionDetail.Items.Clear();

            //Remove square brackets from the List box item.
            string lstBoxItem = Regex.Replace(cbDeptName.SelectedValue.ToString(),
                                              @"[\[\]\s]",
                                              string.Empty);

            //Split the list box item into two parts by considering the (,)
            //to separate the DICOM ID and Patient name.
            string[] strItems = (lstBoxItem).Split(Convert.ToChar(","));

            serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);
            serviceClient.GetSectionDetailAsync(strItems[1], cbDeptSections.SelectedItem.ToString());
        }

        void serviceClient_GetSectionDetailCompleted(object sender, GetSectionDetailCompletedEventArgs e)
        {
            CompositeType compositeObj = e.Result;
            //lbSectionDetail.Items.Clear();
            lbSectionDetail.Items.Add(compositeObj.SectionID);
            lbSectionDetail.Items.Add(compositeObj.SectionName);
            lbSectionDetail.Items.Add(compositeObj.SectionLecturers);
        }
    }

MainPage.xaml MainPage.xaml中

<StackPanel>

        <StackPanel        
            x:Name      ="LayoutRoot"
            Background  ="White"
            Orientation ="Horizontal"
            >

            <ComboBox
                x:Name                      ="cbDeptID"
                MinWidth                    ="50"
                HorizontalContentAlignment  ="Center"
                DisplayMemberPath           ="Key"
                SelectedValue               ="{Binding ElementName=cbDeptName, Path=SelectedValue, Mode=TwoWay}"
                />

            <ComboBox
                x:Name              ="cbDeptName"
                MinWidth            ="50"
                DisplayMemberPath   ="Value"
                SelectionChanged    ="cbDeptName_SelectionChanged"
                />

            <ComboBox
                x:Name              ="cbDeptSections"
                MinWidth            ="100"
                SelectionChanged    ="cbDeptSections_SelectionChanged"
                />

        </StackPanel>

        <ListBox
            x:Name                                      ="lbSectionDetail"
            ScrollViewer.HorizontalScrollBarVisibility  ="Auto"
            ScrollViewer.VerticalScrollBarVisibility    ="Auto"
            />

clientaccesspolicy.xml clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers="SOAPAction">
                <domain uri="*"/>
            </allow-from>
            <grant-to>
                <resource path="/" include-subpaths="true"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>

I think the culprits to your multiple calls may be 我认为你多次通话的罪魁祸首可能是

serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);

Seems to me that you are registering to the completion event multiple times, but never unregister from it. 在我看来,您多次注册完成事件,但从未注销它。 The more times you call cbDeptName_SelectionChanged and cbDeptName_SelectionChanged , the more event handlers are being created. 调用cbDeptName_SelectionChangedcbDeptName_SelectionChanged的次数越多,创建的事件处理程序就越多。 When your service returns with a response, they're all invoked. 当您的服务返回响应时,它们都会被调用。

EDIT: The reason those methods are invoked the first time when your app loads is because when binding the combobox controls, they automatically change their selected item to the first item in your list, thus calling the SelectionChanged events. 编辑:第一次加载应用程序时调用这些方法的原因是因为绑定组合框控件时,它们会自动将所选项目更改为列表中的第一项,从而调用SelectionChanged事件。

EDIT 2: To avoid multiple invocations of the same event, you should register to that event only on your Loaded event, like you do to serviceClient.GetDeptIDNameCompleted 编辑2:为了避免多次调用同一事件,您应该在您的Loaded事件上注册该事件,就像您对serviceClient.GetDeptIDNameCompleted

Your Load event should look something like this: 您的Load事件应如下所示:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    // Put these here instead of where they are now
    serviceClient.GetSectionsListCompleted += new EventHandler<GetSectionsListCompletedEventArgs>(serviceClient_GetSectionsListCompleted);
    serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);

    serviceClient.GetDeptIDNameCompleted += new EventHandler<GetDeptIDNameCompletedEventArgs>(serviceClient_GetDeptIDNameCompleted);
    serviceClient.GetDeptIDNameAsync();
}

Don't forget to remove these lines from where they are now in your code. 不要忘记从代码中的这些行中删除这些行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM