【问题标题】:Client call Web Service Timeout客户端调用 Web 服务超时
【发布时间】:2017-12-08 04:42:40
【问题描述】:

我在谷歌上搜索了很多时间,但没有运气,请你们帮帮我吗?

我有一个程序可以生成 PDF 格式的财务报告,然后调用 SMTP Web 服务来发送电子邮件。

请在下面找到我的图解代码:

using (SMTPServiceSoapClient client = new SMTPServiceSoapClient())
{
    for (no. of customers)
    {
        try
        {
          //step 1: generate_report
          generate_pdf();

          //step 2: call SMTP web service and send email
          client.SendEmail();
        }
        catch(Exception e)
        {
        }
    }
}

第一封邮件总是成功发送。

并且第二封邮件总是会抛出异常(“请求通道在 00:24:59.9969997 之后等待回复时超时。增加传递给 Request 调用的超时值或增加 Binding 上的 SendTimeout 值。分配给此操作的时间可能是较长超时的一部分。")

而且,成功/失败模式是这样的

  1. 成功

  2. 失败

  3. 成功

  4. 失败

...

即使我设置了一个很长(例如 2 小时)的 SendTimeOut 值。程序只会等待几秒钟然后抛出异常。

下面是我的 app.config

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="SMTP ServiceSoap" sendTimeout="00:25:00" maxBufferSize="536870912" maxBufferPoolSize="536870912" maxReceivedMessageSize="536870912" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://smtpsrvhk/SMTPService/SMTPService.asmx"
        binding="basicHttpBinding" bindingConfiguration="SMTP ServiceSoap"
        contract="SMTPService.SMTPServiceSoap" name="SMTP ServiceSoap" />
    </client>
  </system.serviceModel>

请帮忙谢谢。

以下是我发送电子邮件的代码。

        // Email Sending 
        public void sendEmail()
        {
            int result = -999;
            //If Validated
            if (CompareHash() == true)
            {
                MailMessage email = new MailMessage();    

                SmtpClient emailserv = new SmtpClient(SMTPserver);
                emailserv.SendCompleted += new SendCompletedEventHandler(emailserv_SendCompleted);

                //From / To / CC/ Bcc Handling
                MailAddress sendfrom = returnEmailAddress(senderAddr, senderName);
                email.Sender = sendfrom;
                email.From = sendfrom;

                foreach (var to in receipent)
                {
                    email.To.Add(to);
                }
                foreach (var cc in copyToReceipent)
                {
                    email.CC.Add(cc);
                }
                foreach (var bcc in blankcopyToReceipent)
                {
                    email.Bcc.Add(bcc);
                }
                foreach (var reply in replytoReceipent)
                {
                    email.ReplyToList.Add(reply);
                }

                if (ccToSender == true)
                {
                    email.CC.Add(sendfrom);
                }

                if (bccToSender == true)
                {
                    email.Bcc.Add(sendfrom);
                }

                //email.To.Add(sendto);
                email.Subject = EmailTitle;
                email.IsBodyHtml = useHTMLFormat;
                email.Body = EmailContent;

                //Add Attachment If found
                if (attachmentPath.Count > 0)
                {
                    for (int i = 0; i < attachmentPath.Count; i++)
                    {
                        string fileName = attachmentPath[i];
                        Attachment attachment = new Attachment(fileName);
                        ContentDisposition disposition = attachment.ContentDisposition;
                        disposition.CreationDate = File.GetCreationTime(fileName);
                        disposition.ModificationDate = File.GetLastWriteTime(fileName);
                        disposition.ReadDate = File.GetLastAccessTime(fileName);
                        email.Attachments.Add(attachment);
                    }
                }
                emailserv.Credentials = null;

                try
                {
                    //emailserv.SendAsync(email, null);
                    emailserv.Send(email);
                    email.Dispose();
                    result = 1;
                    LogDown(result, "Email request has been sent to the SMTP server.");
                }
                catch (Exception ex)
                {
                    result = -1;
                    LogDown(result, String.Format("Exception during Send, Message:{0}", ex.Message));
                }

            }
            else
            {
                result = -2;
                LogDown(result, "Hash Check Failed");
            }            
            sendResult = result;
        }

【问题讨论】:

  • 可以发邮件的代码吗?
  • 您需要为每封电子邮件创建新的客户端。 Net 4.0 的常见问题。它认为这个错误已在 Net 4.5 中修复,但不确定。
  • @jdweng 是的,我尝试为每封电子邮件创建新客户端。问题依然存在。
  • @ZweiJames 我贴出了发送邮件的代码,谢谢!
  • 您在创建新邮件客户端之前是否已处理邮件客户端?自从我看到这个问题以来已经有几年了。在您的 SMTP 类中,创建一个 Dispose() 方法并确保调用了 dispose。如果我没记错的话,使用 Net 4.0 处理客户端的唯一方法是将其放入一个类中,然后再处理该类。

标签: c# web-services


【解决方案1】:

据我所知,可能是客户端和 Web 服务之间的一些网络问题。

这是因为我在不同的位置设置了相同的 Web 服务,它们运行良好。

【讨论】:

    猜你喜欢
    • 2014-09-04
    • 2012-10-30
    • 2012-11-24
    • 1970-01-01
    • 2019-01-29
    • 2013-08-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    相关资源
    最近更新 更多