【问题标题】:Timeouts not supported on this Stream此流不支持超时
【发布时间】:2018-06-29 04:32:59
【问题描述】:

我曾经有一个 API 用于连接到发送邮件的邮件服务。此邮件服务提供的参数之一是附件流。 sendMail 函数是同步的,所以我没有任何问题

除了我们正在尝试制作与上述类似的新 API。虽然原始版本是常规的 .NET,但我们使用的新版本使用的是 .NET Core 2,0。这意味着连接到该服务后,一切都变得异步了。

我想让我的服务像这样运行

        try
        {
            // Send the email.
            var result = mailService.SendMailWithMessageAsync(emailMessage, "DefaultMail").Result;
        }
        catch (Exception ex)
        {
            string error = ex.ToString();
        }

我得到了不支持的内存超时。如果我不使用任何附件,我的邮件服务将正常工作。

有什么建议吗?

【问题讨论】:

  • 为什么是“结果”?调用者不是异步的吗?
  • 我试图模仿非异步的原始 API 的行为。
  • 此外,.NET Core 包装了所有连接的服务以使它们异步。因此,如果您附加了一个名为 mail 的服务并具有一个名为 SendMail 的函数,那么您现在拥有的邮件服务有一个名为 SendMailAsync 的函数,而不是您的邮件服务中的原始 SendMail。

标签: .net .net-core


【解决方案1】:

似乎这是一个 .NET Core 2.0 问题,我使用 .NET Framework 4.6.1 创建了相同的控制台程序,但我对此没有任何问题。我连接的邮件服务是由另一个团队开发的,现在我发现我在使用 .NET Core 2.0 而不是 .NET Standard 4.6.1 时遇到了问题

using System;
using System.IO;
using System.Threading.Tasks;
using ConnectedMailService;

namespace MailSender
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MainAsync(args).GetAwaiter().GetResult();
                Console.WriteLine("Email Send End!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.ToString());
            }

            Console.ReadKey();
        }

        static async Task MainAsync(string[] args)
        {
            Console.WriteLine("Email Send Start!");

            MailServiceClient mailService = new ConnectedMailService.MailServiceClient();

            EMailMessage emailMessage = new EMailMessage();
            emailMessage.Subject = "Email Subject Test";
            emailMessage.To = new EmailRecipient[1];
            emailMessage.To[0] = new EmailRecipient();
            emailMessage.To[0].recipientEmail = "nino@mail.com";
            emailMessage.From = "test@mail.com";
            emailMessage.Body = "Email Body Test.";

            StreamAttachment streamAttachment = new StreamAttachment();
            streamAttachment.attachment = new MemoryStream(new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35 });
            streamAttachment.fileName = "12345.txt";
            emailMessage.streamAttachments = new StreamAttachment[1];
            emailMessage.streamAttachments[0] = streamAttachment;

            var retVal = await mailService.SendMailWithMessageAsync(emailMessage, "DefaultMail");
        }
    }
}

你们有遇到过这样的事情吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多