我面临着一个非常奇怪的问题,我希望有人会有解决方案。我通过Owin用Owin和WebApi创建了一个ASP.NET Web应用程序。
我创建了一个显示问题的Git仓库:https://github.com/robbaman/smtp-error-example
Startup.cs
public class Startup {
public void Configuration(IAppBuilder app) {
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
}
}
TestController.cs
public class TestController : ApiController {
[HttpGet, Route("test")]
public void Test() {
using (var client = new SmtpClient()) {
var message = new MailMessage();
message.To.Add("test@example.com");
message.Body = "haha";
message.Subject = "ohhh";
client.Send(message);
}
}
[HttpGet, Route("testasync")]
public async Task TestAsync() {
using (var client = new SmtpClient()) {
var message = new MailMessage();
message.To.Add("test@example.com");
message.Body = "haha";
message.Subject = "ohhh";
await client.SendMailAsync(message);
}
}
[HttpGet, Route("test2")]
public async Task Test2() {
throw new NotImplementedException();
}
}
<system.net>
<mailSettings>
<smtp from="support@example.com">
<network host="mailserver" enableSsl="true" userName="support@example.com"
port="587" password="password" />
</smtp>
</mailSettings>
</system.net>
使用上面的配置(或检查GitHub repo)并运行应用程序时,会发生一些奇怪的事情:
导航到/test
时,您会收到mailserver
主机名无效的预期异常。导航到/test2
时,您会获得预期的NotImplementedException
。
但是导航到/testasync
时,会发生相同的主机名异常,但是没有响应发送到浏览器。它将继续加载和加载...我不知道为什么或如何解决它。
任何帮助将不胜感激!
答案 0 :(得分:0)
您可以像这样打电话给TestAsync()
-
[HttpGet, Route("testasync")]
public string CallTestAsync()
{
Task t = TestAsync();
string response = "";
if (t.IsFaulted)
{
response = t.Exception.InnerException.Message;
}
else
{
response = "Success";
}
return response;
}
从[HttpGet, Route("testasync")]
TestAsync()