我在C#和EWS中有时间问题 [英] I have timing issues in C# and EWS

查看:181
本文介绍了我在C#和EWS中有时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码片段中,我尝试将(通过重命名)电子邮件的附件保存到磁盘位置。



大部分时间这种方式正常但是偶然它在fAttachment.Load(filename)语句中失败并且一个空文件被保存到磁盘。

当发生这种情况时我得到这个例外: -



In the following code snippet i attempt to save (with rename) an attachment to an email to a disk location.

Most of the time this works correctly but occasionaly it fails in the fAttachment.Load(filename) statement and an empty file is saved to disk.
When this occurs I get this exception:-

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.0.0.1:443
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) By System On BMXS04





要到达使用fAttachment.Load的位置需要那个

a)我已经建立了一个有效的ExchangeService连接

b)我已经成功获得了附件的电子邮件列表

c)特定的电子邮件我正在工作已成功装载

d)我能够检查附件的属性以验证它是我要保存的类型



所以,当发出fAttachment.Load时,一切都应该是好的。



交换服务器位于Exchange 2010的内部SBS2011盒子上SP3。所有完全修补。

机器(实时)或开发都是域计算机。



附件永远不会超过4kb并且是纯文本。



我们将非常感激地接受任何建议。



(PS首先发布在这里,所以我希望我做到了正确)



我已经清理了以下代码:



To get to the positionn where fAttachment.Load is being used requires that
a) I have already established a valid ExchangeService connection
b) I have sucessfully obtained a list of emails with attachments
c) The particular email I am working on has been sucessfully loaded
d) I am able to check the properties of the attachment to validate that it is of the type I want to save

so, when it comes to issue the fAttachment.Load everything should be good.

The exchange server is sitting on an internal SBS2011 box with Exchange 2010 SP3. All fully patched.
The machine (live) or development are both domain computers.

The attachments never exceed 4kb and are plain text.

Any advice would be gratefully accepted.

(P.S. First post here so I hope I did it correctly)

I have sanitised the following code follows:

public static void HandleWeborder(string mhSavePath, string mhEmail)
{

    //
    try
    {
        var service = GetExchangeService();
        //
        if (service != null)
        {
            //
            var StartTime = DateTime.Now;
            bool wasOrder;
            // Allow a maximum of 150 emails per run.
            var view = new ItemView(150);
            var lookUp = mhEmail;
            var searchFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true);
            var results = service.FindItems(new FolderId(WellKnownFolderName.Inbox, new Mailbox(lookUp)), searchFilter, view);
            if (results.TotalCount > 0)
            {
                foreach (EmailMessage email in results)
                {
                     wasOrder = false;
                     email.Load(new PropertySet(ItemSchema.Attachments));
                     foreach (Microsoft.Exchange.WebServices.Data.Attachment attachment in email.Attachments)
                     {
                         FileAttachment fAttachment = attachment as FileAttachment;
                         var test = fAttachment.Name.Substring(attachment.Name.Length - 3);
                         if (test == "xtx")
                         {
                            wasOrder = true;
                            var newName = fAttachment.Name.Substring(0, fAttachment.Name.IndexOf(".", StringComparison.Ordinal));
                            if (newName.Substring(0, 5) == "Order")
                            {
                                newName = newName.Substring(6);
                                newName = mhSavePath + "W" + newName;
                                //
                                var suf = ".txt";
                                if (File.Exists(newName + suf)) suf = ".dup";
                                if (File.Exists(newName + suf + ".bak")) suf = ".dup";
                                 newName += suf;
                                 bool dountiltrue = false;
                                 int attempts = 0;
                                 while (!dountiltrue)
                                 {
                                    try
                                    {
                                        fAttachment.Load(newName);
                                        wasOrder = true;
                                        dountiltrue = true;
                                    }
                                    catch (Exception ex)
                                    {
                                       if (attempts < 5)
                                       {
                                           attempts += 1;
                                           var innerEx = ex.InnerException.ToString();
                                           RunParms[PosLogMsg] = "Attempt " + attempts + " - Error " + newName + " May Have timed out - " + innerEx;
                                           RunParms[PosLogType] = LogError;
                                           AddActivity();
                                        }
                                     }
                                  }
                               }
                            }
                         }
                     }
                     if (wasOrder)
                     {
                        email.Delete(DeleteMode.HardDelete);
                     }
                }
            }
        }
    }
    catch (Exception ex)
    {
        //
        //TODO need to log error somehow
        //
        // might disable this logging because it may be pointless
        var innerEx = ex.InnerException.ToString();
        RunParms[PosLogMsg] = "Error 264-9 - " + innerEx;
        RunParms[PosLogType] = LogError;
        AddActivity();
    }
}





我尝试了什么:



我添加了所有这些Try / Catch并手动逐步执行但步进时没有错误,只有在完全运行模式下:-(



注意:编辑删除所有错误记录,因为Try / Catch处理使一些评论者感到困惑。创建Try / Catch只是为了获得异常事件的物理记录,因为它需要一个每天单步执行代码以等待间歇性故障(故障仅发生3-5%的代码被调用,并且始终发生在fAttachment.Load(newName)语句



What I have tried:

I have added all these Try/Catch and manually stepped through but it does not error when stepping, only when in full run mode :-(

Note: Edited to remove all error recording as the Try/Catch handling was confusing some commenters. The Try/Catch were created simply to get a physical recording of the exception occurrence because it would take a day to step through the code to wait for the intermittent failure (the failure only occurs 3-5% of the times the code is called and always occurs at the fAttachment.Load(newName) statement

推荐答案

只是另一个想法,不知道应用程序和存储服务器是否在彼此的远程位置,或者在同一台服务器上,它可能只是一个导致暂时性问题的网络滞后问题。 />


如果两者相互遥远,那么你可能需要更好的重试机制。这是一个 RetryOperationHelper [ ^ ]可以根据您的要求进行调整。我还建议实施指数退避 [ ^ ]策略,以提高耐用性。
Just another thought, without knowing if the app and the storage server are in remote locations to each other, or are on the same server, it could be simply a network lag issue causing a transient issue.

If the two are remote to each other, then you may need a better retry mechanism. Here is a RetryOperationHelper[^] that could be adapted to your requirements. I would also recommend implementing an Exponential backoff[^] strategy to allow for greater durability.


这篇关于我在C#和EWS中有时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆