首页 > 解决方案 > 为什么 PropertyDataCollection 对象将多条记录保存到数据库

问题描述

我有一个实用程序可以读取 MicrosoftBizTalk Server 资源的状态……特别是 ReceiveLocation 组件。我的问题是程序正在提交每个项目的多个条目,即返回的数据中的每个项目都被乘以 25,这样,被持久化的数据不是只保留 5 行,而是 125。例如,而不是只有 1 行我返回的第一行我有 25 个。

这是我的程序:

public List<BizTalk> GetBizTalkServicesStatistics()
    {

    
        List<BizTalk> model = new List<BizTalk>();

        try
        {
            //Create the WMI search object.
            ManagementObjectSearcher Searcher = new ManagementObjectSearcher();

            ConnectionOptions options = new ConnectionOptions
            {

                Username = "+username+",
                Password = "+password+",
                Authority = "+domain+"
            };

            
            var server = "+server+";
            // create the scope node so we can set the WMI root node correctly.
            ManagementScope Scope = new ManagementScope("\\\\" + server + "\\root\\MicrosoftBizTalkServer", options);
            Searcher.Scope = Scope;

            // Build a Query to enumerate the MSBTS_ReceiveLocation instances if an argument
            // is supplied use it to select only the matching RL.

                //if (args.Length == 0)
                SelectQuery Query = new SelectQuery();               
                Query.QueryString = "SELECT * FROM MSBTS_ReceiveLocation";
                //          else
                //Query.QueryString = "SELECT * FROM MSBTS_ReceiveLocation WHERE Name = '" + args[0] + "'";


            // Set the query for the searcher.
            Searcher.Query = Query;

            // Execute the query and determine if any results were obtained.
            ManagementObjectCollection QueryCol = Searcher.Get();

            // Use a bool to tell if we enter the for loop
            // below because Count property is not supported
            bool ReceiveLocationFound = false;

            // Enumerate all properties.
            foreach (ManagementBaseObject envVar in QueryCol)
            {
                // There is at least one Receive Location
                ReceiveLocationFound = true;

                PropertyDataCollection envVarProperties = envVar.Properties;
               
                foreach (PropertyData envVarProperty in envVarProperties)
                {
                    BizTalk bizTalk = new BizTalk();
                    bizTalk.Name = Convert.ToString(envVar["Name"]);
                    bizTalk.TransportType = Convert.ToString(envVar["AdapterName"]);
                    bizTalk.Uri = Convert.ToString(envVar["InboundTransportURL"]);
                    bizTalk.Status = Convert.ToString(envVar["Name"]);
                    bizTalk.ReceiveHandler = Convert.ToString(envVar["HostName"]);
                    bizTalk.ReceivePort = Convert.ToString(envVar["ReceivePortName"]);
                    bizTalk.RunDate = DateTime.Now;
                    bizTalk.ApplicationId = 24;
                    bizTalk.ServerId = 8;
                    bizTalk.InstanceName = "FBCZOP";                                          
                    model.Add(bizTalk);
                    
                }
            }

            if (!ReceiveLocationFound)
            {
                Console.WriteLine("No receive locations found matching the specified name.");
            }
        }

        catch (Exception excep)
        {
            ExceptionLogger.SendErrorToText(excep);
        }

        return model;
    }

保存功能

public void SaveStatistics(BizTalk entity)
    {
        List<BizTalk> ServerInfo = new List<BizTalk>();
        ServerInfo = GetBizTalkServicesStatistics();
        foreach (var di in ServerInfo)
        {
            entity.RunDate = di.RunDate;
            entity.Name = di.Name;
            entity.Status = di.Status;
            entity.Uri = di.Uri;
            entity.InstanceName = di.InstanceName;
            entity.ReceivePort = di.ReceivePort;
            entity.TransportType= di.TransportType;
            entity.RunDate = DateTime.Now;
            entity.ReceiveHandler = di.ReceiveHandler;                
            entity.ServerId = entity.ServerId;
            entity.ApplicationId = entity.ApplicationId;
            appEntities.BizTalk.Add(entity);
            appEntities.SaveChanges();
        }

    }

当我单步执行代码变量 envVarProperties 时,在 envVarProperties << ResultsView 下显示记录计数为 125: 链接 1

而 QueryCol 变量显示计数为 5 :

链接 2

标签: c#collectionswmibiztalk

解决方案


看起来您在GetBizTalkServicesStatistics()方法中迭代了额外的时间。

删除以 This 开头的 foreach 循环,foreach (PropertyData envVarProperty in envVarProperties). 该循环遍历对象具有的每个属性(所有 25 个属性),每个实例(5 个实例)... 25 * 5 = 125 个您正在检索的值。您只想遍历您的实例并提取所需的属性。这样一来,您的模型对象中就会有 5 个对象。

我建议可能是这样的(未经测试,因为我没有 BizTalk)

public List<BizTalk> GetBizTalkServicesStatistics()
        {

            List<BizTalk> model = new List<BizTalk>();

            try
            {
                //Create the WMI search object.
                ConnectionOptions options = new ConnectionOptions
                {
                    Username = "+username+",
                    Password = "+password+",
                    Authority = "+domain+"
                };

                var server = "+server+";

                // create the scope node so we can set the WMI root node correctly.
                ManagementScope Scope = new ManagementScope("\\\\" + server + "\\root\\MicrosoftBizTalkServer", options);

                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, new ObjectQuery("SELECT * FROM MSBTS_ReceiveLocation"));

                // Enumerate all properties.
                foreach (ManagementObject instance in Searcher.Get())
                {
                    {
                        BizTalk bizTalk = new BizTalk();
                        bizTalk.Name = instance.Properties["Name"]?.Value?.ToString();
                        bizTalk.TransportType = instance.Properties["AdapterName"]?.Value?.ToString();
                        bizTalk.Uri = instance.Properties["InboundTransportURL"]?.Value?.ToString();
                        bizTalk.Status = instance.Properties["Name"]?.Value?.ToString();
                        bizTalk.ReceiveHandler = instance.Properties["HostName"]?.Value?.ToString();
                        bizTalk.ReceivePort = instance.Properties["ReceivePortName"]?.Value?.ToString();
                        bizTalk.RunDate = DateTime.Now;
                        bizTalk.ApplicationId = 24;
                        bizTalk.ServerId = 8;
                        bizTalk.InstanceName = "FBCZOP";
                        model.Add(bizTalk);
                    }
                }

                // Determine  
                if (model.Count == 0)
                {
                    Console.WriteLine("No receive locations found matching the specified name.");
                }
            }

            catch (Exception excep)
            {
                ExceptionLogger.SendErrorToText(excep);
            }

            return model;
        }  

此外,如果您删除连接选项,这可以更加简化(除非您是硬编码凭据,强烈建议不要这样做)。如果您只是使用执行用户的身份,则不需要该数据。

-保罗


推荐阅读