【发布时间】:2018-09-05 16:08:43
【问题描述】:
我已经使用 Azure 连接服务创建了一个 ASP Net Core (2.0) Web 应用程序,在 localhost 上测试时它运行良好,但是当发布到 IIS 时它会抛出以下错误:
处理请求时发生未处理的异常。 SocketException: 尝试连接时出错,因为连接方一段时间后没有正确响应,或者连接主机无法响应40.112.254.71:443导致连接建立错误
我将一个txt文件从视图发布到控制器并发送到Azure服务进行分析,这是发布方法:
[HttpPost("Analyze_v2")]
public JsonResult Analyze_v2(IFormFile archivo)
{
List<string> result = new List<string>();
using (var reader = new StreamReader(archivo.OpenReadStream()))
{
while (reader.Peek() >= 0)
result.Add(reader.ReadLine());
}
List<TextAnalyzeModel> data = new List<TextAnalyzeModel>();
for (int i = 0; i < result.Count; i++)
{
TextAnalyzeModel model = new TextAnalyzeModel();
model.TextStr = result.ElementAt(i);
if (!string.IsNullOrWhiteSpace(model.TextStr))
{
ITextAnalyticsAPI client = this.GetTextAnalyzeClient(new AsyncHandler());
model.LanguageAnalyzeResult = client.DetectLanguage(
new BatchInput(
new List<Input>()
{
new Input(i.ToString(),model.TextStr)
}));
model.SentimentAnalyzeResult = client.Sentiment(
new MultiLanguageBatchInput(
new List<MultiLanguageInput>()
{
new MultiLanguageInput(
model.LanguageAnalyzeResult.Documents[0].DetectedLanguages[0].Iso6391Name,
i.ToString(),
model.TextStr)
}));
data.Add(model);
}
}
return Json(data);
}
这是 GetTextAnalyzeClient 方法:
private ITextAnalyticsAPI GetTextAnalyzeClient(DelegatingHandler handler)
{
string key = configuration.GetSection("CognitiveServices")["TextAnalytics:ServiceKey"];
ITextAnalyticsAPI client = new TextAnalyticsAPI(handlers: handler);
client.SubscriptionKey = key;
client.AzureRegion = Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models.AzureRegions.Westus;
return client;
}
这是 AsyncHandler 类:
class AsyncHandler : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
// Call the inner handler.
var response = await base.SendAsync(request, cancellationToken);
return response;
}
}
我在不同的端口和相同的问题上进行了测试,在 localhost 上运行良好,当我发布它时抛出异常。 任何想法都会有所帮助。
【问题讨论】:
标签: c# azure asp.net-core async-await