内存问题事件接收器

时间:2018-01-22 08:23:23

标签: c# asp.net sharepoint sharepoint-2010

当我将文档上传到共享点列表时,我有一个事件接收器。 该事件调用将项目复制到两个不同位置的函数。

问题是性能非常糟糕,当我上传5个文件时,应用程序占用+/- 10 GB的内存,每个文件是+/- 400 KB。

没有上传,应用程序运行正常。

private static void AttachRoutingEventListener(SPWeb web)
    {

        web.AllowUnsafeUpdates = true;
        try
        {
            var uploadList =   web.Lists[SPUtility.GetLocalizedString("$Resources:MailRoom_List_UploadList_Title", "Matri", web.Language)];

            uploadList.EventReceivers.Add(SPEventReceiverType.ItemAdded, "Com.Gimi.Matri.Artifacts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=dad1f8c1ceb5d9440", "Com.Gimi.Matri.Artifacts.RouteScannedDocuments");
            uploadList.Update();
        }
        finally
        {

            web.AllowUnsafeUpdates = false;
        }
    }




[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void ItemAdded(SPItemEventProperties properties)
        {
            using (SPWeb web = properties.Web)
            {
                base.ItemAdded(properties);
                ListItemRouting.RouteItem(properties.ListItem);
            }
            properties.Dispose();
        }


 public static void RouteItem(SPListItem item)
        {
             SPWeb web = item.Web;
            SPList documents = web.Lists[SPUtility.GetLocalizedString("$Resources:Matri,MailRoom_List_MailDocuments_Title", "Matri", web.Language)];
            SPList metadata = web.Lists[SPUtility.GetLocalizedString("$Resources:MailRoom_List_LetterData_Title", "Matri", web.Language)];
            DateTime scanDate = GetScanDate();
            Regex mailExtension = new Regex(@"^.+\.(eml)$", RegexOptions.IgnoreCase);
            string fileDestinationUrl = CreateFullRelativePath(documents, scanDate) + "/" + item.File.Name;
            if (!mailExtension.IsMatch(item.File.Name))
            {
                try
                {
                    lock (lockObjects)
                    {
                        try
                        {
                            CopyFile(item, documents, fileDestinationUrl);
                        }
                        catch (DirectoryNotFoundException ex)
                        {
                            //In case the folder not created
                            EnsureFolders(documents, scanDate);
                            CopyFile(item, documents, fileDestinationUrl);
                        }

                        try
                        {
                            // copy to letterData
                            CopyMetadata(metadata, item, fileDestinationUrl, scanDate);
                        }

                        catch (DirectoryNotFoundException dnEx)
                        {
                            //In case the folder not created
                            EnsureListItemFolders(metadata, scanDate);
                            CopyMetadata(metadata, item, fileDestinationUrl, scanDate);
                        }
                    }
                }
                catch (Exception genEx)
                {
                    Log.Warn("ZZ ERROR error in eventhandler of upload lis ==> " + genEx.Message);
                    Log.Error("error in eventhandler of upload list", genEx);

                }
            }
        }


        private static DateTime GetScanDate()
        {
            return DateTime.Now;
        }

        private static void EnsureListItemFolders(SPList list, DateTime scanDate)
        {
            SPFolder dayFolder = list.ParentWeb.GetFolder(CreateFullRelativePath(list, scanDate));
            SPListItem folder = null;
            if (!dayFolder.Exists)
            {
                folder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, GetFolderName(scanDate));
                folder.Update();

            }

        }
        private static void EnsureFolders(SPList list,DateTime scanDate)
        {
            SPFolder dayFolder = list.ParentWeb.GetFolder(CreateFullRelativePath(list, scanDate));
            if (!dayFolder.Exists)
                list.RootFolder.SubFolders.Add(CreateFullRelativePath(list, scanDate));

        }

        private static void CopyMetadata(SPList metadata, SPListItem item, string fileDestinationUrl, DateTime scanDate)
        {
            SPListItemCollection metadataItems = metadata.Items;
            SPListItem destination = metadataItems.Add(CreateFullRelativePath(metadata, scanDate), SPFileSystemObjectType.File, null);
            destination["Title"] = item["Title"];
            destination["DateOfLetter"] = item["DateOfLetter"];
            destination["SenderName"] = item["SenderName"];
            destination["CompanyAddressee"] = item["CompanyAddressee"];
            destination["AddresseeName"] = item["AddresseeName"];
            destination["Recipient"] = item["Recipient"];
            destination["DateOfScan"] = DateTime.Now;
            destination["MailType"] = item["MailType"];
            destination["Priority"] = item["Priority"];
            destination["ScanLocation"] = item["ScanLocation"];
            destination["LetterURL"] = new Uri(new Uri(item.Web.Url), new Uri(fileDestinationUrl, UriKind.Relative)).AbsoluteUri;
            destination.Update();
        }


        private static void CopyFile(SPListItem item, SPList documents, string fileDestinationUrl)
        {
            using (Stream docStream = item.File.OpenBinaryStream())
            {
               documents.RootFolder.Files.Add(fileDestinationUrl, docStream, true);
                    docStream.Flush();
                    docStream.Close();

            }
        }

        private static string CreateFullRelativePath(SPList destination, DateTime scanDate)
        {
            return destination.RootFolder.ServerRelativeUrl + "/" + GetFolderName(scanDate);
        }

        private static string GetFolderName(DateTime date)
        {
            return date.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
        }

提前谢谢!

1 个答案:

答案 0 :(得分:0)

SpWeb.Lists["list name"]似乎是个问题 我用

替换了它
SpWeb.GetList("Url");

参考网址Best Practices: SharePoint Object Model for Performance Tuning