【问题标题】:.Net Core Async Method for Azure ServicesAzure 服务的 .Net Core 异步方法
【发布时间】: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


    【解决方案1】:

    登录到托管 IIS 的服务器并尝试远程登录到 443 上的 40.112.254.71。如果您没有收到响应,这意味着流量被阻止(网络规则)并且不依赖于您的应用程序。

    如果响应确实发生但在一段时间后,请确保您的响应超时设置充分:

    <aspNetCore requestTimeout="00:20:00" processPath=“%LAUNCHER_PATH%” arguments=“%LAUNCHER_ARGS%” stdoutLogEnabled=“false” stdoutLogFile=“.\logs\stdout”     />
    

    【讨论】:

    • 我试过了,没用。这是针对传出请求还是传入请求?我发现它设置了对 IIS 的传入请求而不是传出并等待响应的时间。
    • @g4b0.88 你第一次尝试远程登录吗?如果您不能 telnet,那么响应超时配置的多少都无关紧要。
    • 在我的开发机器(我使用 IIS express 运行项目的地方)中,我安装了 IIS 7,在 IIS Express 中我没有遇到任何问题,但是当部署到 IIS7 时出现了该错误。不,我没有尝试远程登录,该怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多