首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Azure Webjob作业,KeyVault配置扩展,套接字错误

Azure Webjob作业,KeyVault配置扩展,套接字错误
EN

Stack Overflow用户
提问于 2020-09-23 21:08:03
回答 2查看 543关注 0票数 0

需要一些帮助才能确定这是我的代码中的错误还是配置库扩展中的错误。

我有一个基于netcore控制台的网络作业。直到几周前,我们才声明偶尔会出现一些启动错误,比如套接字错误10060 -套接字超时或“连接尝试失败是因为连接方在一段时间后没有正确响应,或者建立连接失败是因为连接主机没有响应”。

这些都与加载配置层(应用程序设置、env、命令行和密钥库)有关。一旦在主机构建器上执行构建,错误就产生于密钥库。

我最初添加了带有默认HttpStatusCodeErrorDetectionStrategy和指数后退的重试策略,但这不是在执行。

最后,我在自己的检测策略中添加了自己的重试策略(见下文)。还是没被解雇。

我已经把代码简化到了一个像示例一样的hello世界,并包含了来自网络作业的消息。

以下是代码摘要:

代码语言:javascript
运行
复制
    public static async Task<int> Main(string[] args)
    {
        
        var host = CreateHostBuilder(args)
            .UseConsoleLifetime()
            .Build();

        using var serviceScope = host.Services.CreateScope();

        var services = serviceScope.ServiceProvider;

        //**stripped down to logging just for debug

        var loggerFactory = host.Services.GetRequiredService<ILoggerFactory>();

        var logger = loggerFactory.CreateLogger("Main");

        logger.LogDebug("Hello Test App Started OK.  Exiting.");

        //**Normally lots of service calls go here to do real work**

        return 0;
    }

hostbuilder -为什么是主机建设者?我们使用了许多为webapi和webapp构建的组件,因此使用类似的服务模型非常方便。

代码语言:javascript
运行
复制
    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        var host = Host
            .CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((ctx, config) =>
            {
                //override with keyvault
                var azureServiceTokenProvider = new AzureServiceTokenProvider();   //this is awesome - it will use MSI or Visual Studio connection

                var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));

                var retryPolicy = new RetryPolicy<ServerErrorDetectionStrategy>(
                        new ExponentialBackoffRetryStrategy(
                            retryCount: 5,
                            minBackoff: TimeSpan.FromSeconds(1.0),
                            maxBackoff: TimeSpan.FromSeconds(16.0),
                            deltaBackoff: TimeSpan.FromSeconds(2.0)
                            )
                        );

                retryPolicy.Retrying += RetryPolicy_Retrying;

                keyVaultClient.SetRetryPolicy(retryPolicy);


                var prebuiltConfig = config.Build();

                config.AddAzureKeyVault(prebuiltConfig.GetSection("KeyVaultSettings").GetValue<string>("KeyVaultUri"), keyVaultClient, new DefaultKeyVaultSecretManager());

                config.AddCommandLine(args);

            })
            .ConfigureLogging((ctx, loggingBuilder) =>  //note - this is run AFTER app configuration - whatever the order it is in.
            {
                loggingBuilder.ClearProviders();

                loggingBuilder
                    .AddConsole()
                    .AddDebug()
                    .AddApplicationInsightsWebJobs(config => config.InstrumentationKey = ctx.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]);

            })
            .ConfigureServices((ctx, services) =>
            {
                services
                    .AddApplicationInsightsTelemetry();

                services
                    .AddOptions();


            });
        return host;
    }

事件-这从未被触发。

代码语言:javascript
运行
复制
    private static void RetryPolicy_Retrying(object sender, RetryingEventArgs e)
    {
        Console.WriteLine($"Retrying, count = {e.CurrentRetryCount}, Last Exception={e.LastException}, Delay={e.Delay}");
    }

重试策略-只触发非MSI试图联系密钥库。

代码语言:javascript
运行
复制
public class ServerErrorDetectionStrategy : ITransientErrorDetectionStrategy
{
    public bool IsTransient(Exception ex)
    {
        if (ex != null)
        {
            Console.WriteLine($"Exception {ex.Message} received, {ex.GetType()?.FullName}");
            HttpRequestWithStatusException httpException;
            if ((httpException = ex as HttpRequestWithStatusException) != null)
            {
                switch(httpException.StatusCode)
                {
                    case HttpStatusCode.RequestTimeout:
                    case HttpStatusCode.GatewayTimeout:
                    case HttpStatusCode.InternalServerError:
                    case HttpStatusCode.ServiceUnavailable:
                        return true;
                }
            }

            SocketException socketException;
            if((socketException = (ex as SocketException)) != null)
            {
                Console.WriteLine($"Exception {socketException.Message} received, Error Code: {socketException.ErrorCode}, SocketErrorCode: {socketException.SocketErrorCode}");

                if (socketException.SocketErrorCode == SocketError.TimedOut)
                {
                    return true;
                }
            }
        }
        return false;
    }
}

WebJob输出

代码语言:javascript
运行
复制
[SYS INFO] Status changed to Initializing
[SYS INFO] Run script 'run.cmd' with script host - 'WindowsScriptHost'
[SYS INFO] Status changed to Running
[INFO] 
[INFO] D:\local\Temp\jobs\triggered\HelloWebJob\42wj5ipx.ukj>dotnet HelloWebJob.dll  
[INFO] Exception Response status code indicates server error: 401 (Unauthorized). received,     Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException
[INFO] Exception A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. received, System.Net.Http.HttpRequestException
[ERR ] Unhandled exception. System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
[ERR ]  ---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
[ERR ]    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
[ERR ]    --- End of inner exception stack trace ---
[ERR ]    at Microsoft.Rest.RetryDelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
[ERR ]    at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
[ERR ]    at Microsoft.Azure.KeyVault.KeyVaultClient.GetSecretWithHttpMessagesAsync(String vaultBaseUrl, String secretName, String secretVersion, Dictionary`2 customHeaders, CancellationToken cancellationToken)
[ERR ]    at Microsoft.Azure.KeyVault.KeyVaultClientExtensions.GetSecretAsync(IKeyVaultClient operations, String secretIdentifier, CancellationToken cancellationToken)
[ERR ]    at Microsoft.Extensions.Configuration.AzureKeyVault.AzureKeyVaultConfigurationProvider.LoadAsync()
[ERR ]    at Microsoft.Extensions.Configuration.AzureKeyVault.AzureKeyVaultConfigurationProvider.Load()
[ERR ]    at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
[ERR ]    at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
[ERR ]    at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration()
[ERR ]    at Microsoft.Extensions.Hosting.HostBuilder.Build()
[ERR ]    at HelloWebJob.Program.Main(String[] args) in C:\Users\mark\Source\Repos\HelloWebJob\HelloWebJob\Program.cs:line 21
[ERR ]    at HelloWebJob.Program.<Main>(String[] args)
[SYS INFO] Status changed to Failed
[SYS ERR ] Job failed due to exit code -532462766
EN

Stack Overflow用户

发布于 2020-09-24 22:17:41

谢谢@HarshitaSingh-MSFT,虽然当我遇到问题却找不到它时,我还是找到了它。

作为一项工作,我添加了一些基本的重试代码到启动。

Main现在看起来是这样的:

代码语言:javascript
运行
复制
    public static async Task<int> Main(string[] args)
    {
        IHost host = null;
        int retries = 5;
        while (true)
        {
            try
            {
                Console.WriteLine("Building Host...");
                var hostBuilder = CreateHostBuilder(args)
                    .UseConsoleLifetime();

                host = hostBuilder.Build();

                break;
            }
            catch (HttpRequestException hEx)
            {
                Console.WriteLine($"HTTP Exception in host builder. {hEx.Message}, Name:{hEx.GetType().Name}");

                SocketException se;

                if ((se = hEx.InnerException as SocketException) != null)
                {
                    if (se.SocketErrorCode == SocketError.TimedOut)
                    {
                        Console.WriteLine($"Socket error in host builder.  Retrying...");

                        if (retries > 0)
                        {
                            retries--;

                            await Task.Delay(5000);

                            host?.Dispose();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }

        using var serviceScope = host.Services.CreateScope();

        var services = serviceScope.ServiceProvider;

        var transferService = services.GetRequiredService<IRunPinTransfer>();

        var result = await transferService.ProcessAsync();

        return result;

    }
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64036235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档