NET 5.0 WebJob 只想写入我的本地 Blob 存储

问题描述

我们已经过了 2021 年的一半,我找到的所有 WebJob 答案都已经很旧了..也许我错过了一个新的皱纹

这张来自我的 Microsoft Azure 存储资源管理器的图片说明了一切:

enter image description here

在我的机器上,我使用这个 appsettings.Development.json 文件 -- 允许我的 dotnet 控制台与我的 Azurite 存储实例对话(Azurite 从我的本地 Docker 运行)

{
  "ConnectionStrings": {
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true","AzureWebJobsStorage": "UseDevelopmentStorage=true","ContosoData": "Initial Catalog=Contoso;Integrated Security=True;Encrypt=False;Data Source=(localdb)\\MSSQLLocalDB;"
  }
}

在生产中,来自 Program.cs 的这些行将使用我的“真实”连接字符串覆盖上述内容...

.ConfigureAppConfiguration((hostingContext,conf) =>
{

    var env = hostingContext.HostingEnvironment;

    _configuration = conf
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json",optional: true,reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();
})

... 我已将其保存到我的 Web 应用服务的配置页面中:

enter image description here

我的 Functions.cs 中的奇异方法被成功检测并调用 - 我正在使用此输出属性写入 Blob 存储

public class Functions
{
    [Singleton]
    public async static Task CopyDates(
        [TimerTrigger("0 */5 * * * *",RunOnStartup = true)] TimerInfo myTimer,[Blob("tsl-updater-logs/log.txt",FileAccess.Write)] TextWriter logger)
    {

        logger.WriteLine($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

因为该函数标有 RunOnStartup = true,我的仪表板在我发布作业后很快就会显示该作业为 StartedRunning - 但我从未看到 log.txt 文件被写入进入我的 Blob 存储容器

我错过了什么?

哦..如果你必须知道..这些是我的金块:

enter image description here

更新:

添加:我的 Program.cs 的完整源代码

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Azure.WebJobs.Host;

 class Program
    {
        public static IConfiguration _configuration;
        public static IServiceCollection _services;

        static async Task Main(string[] args)
        {
            var builder = new HostBuilder();

            builder
                //.UseEnvironment("Development")
                .ConfigureWebJobs(b =>
                {
                    b.AddAzureStorageCoreServices();
                    b.AddAzureStorageBlobs();
                    b.AddTimers();
                    //b.AddDashboardLogging();

                })
                .ConfigureAppConfiguration((hostingContext,conf) =>
                {

                    var env = hostingContext.HostingEnvironment;

                    _configuration = conf
                        .SetBasePath(env.ContentRootPath)
                        .AddJsonFile($"appsettings.{env.EnvironmentName}.json",reloadOnChange: true)
                        .AddEnvironmentVariables()
                        .Build();
                })
                .ConfigureLogging((context,b) =>
                {
                    b.SetMinimumLevel(LogLevel.Information);
                    b.AddConsole();
                })
                .ConfigureServices(svc =>
                {
                    svc.AddDbContext<Models.DataWareHouseCtx>(opt =>
                        opt.UseSqlServer(_configuration.GetConnectionString("DataWareHouse"))
                            .EnableSensitiveDataLogging(true)
                            .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));

                    svc.AddDbContext<Models.TslDataContext>(opt =>
                        opt.UseSqlServer(_configuration.GetConnectionString("ThingStatusLocation"))
                            .EnableSensitiveDataLogging(true)
                            .UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll));

                    _services = svc;

                });
                //.UseConsoleLifetime();


            var host = builder.Build();

            
            using (host)
            {
                await host.RunAsync();
            }
        }

 
    }

更新 2:

日志仪表板输出

[07/13/2021 20:53:10 > ef3fc6: SYS INFO] Status changed to Stopped
[07/13/2021 21:24:12 > ef3fc6: SYS INFO] Detected WebJob file/s were updated,refreshing WebJob
[07/13/2021 21:24:12 > ef3fc6: SYS INFO] Status changed to Starting
[07/13/2021 21:24:12 > ef3fc6: SYS INFO] WebJob singleton setting is False
[07/13/2021 21:24:13 > ef3fc6: SYS INFO] Run script 'run.cmd' with script host - 'WindowsScriptHost'
[07/13/2021 21:24:13 > ef3fc6: SYS INFO] Status changed to Running
[07/13/2021 21:24:13 > ef3fc6: INFO] 
[07/13/2021 21:24:13 > ef3fc6: INFO] D:\local\Temp\jobs\continuous\My.WebJob.Updater\s02hk5ti.50s>dotnet My.WebJob.Updater.dll  
[07/13/2021 21:24:14 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Extensions.Storage.Blobs.Config.BlobsExtensionConfigProvider[0]
[07/13/2021 21:24:14 > ef3fc6: INFO]       registered http endpoint = 
[07/13/2021 21:24:14 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:24:14 > ef3fc6: INFO]       LoggerFilterOptions
[07/13/2021 21:24:14 > ef3fc6: INFO]       {
[07/13/2021 21:24:14 > ef3fc6: INFO]         "MinLevel": "Information",[07/13/2021 21:24:14 > ef3fc6: INFO]         "Rules": []
[07/13/2021 21:24:14 > ef3fc6: INFO]       }
[07/13/2021 21:24:14 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:24:14 > ef3fc6: INFO]       FunctionResultAggregatorOptions
[07/13/2021 21:24:14 > ef3fc6: INFO]       {
[07/13/2021 21:24:14 > ef3fc6: INFO]         "BatchSize": 1000,[07/13/2021 21:24:14 > ef3fc6: INFO]         "FlushTimeout": "00:00:30",[07/13/2021 21:24:14 > ef3fc6: INFO]         "IsEnabled": true
[07/13/2021 21:24:14 > ef3fc6: INFO]       }
[07/13/2021 21:24:14 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:24:14 > ef3fc6: INFO]       QueuesOptions
[07/13/2021 21:24:15 > ef3fc6: INFO]       {
[07/13/2021 21:24:15 > ef3fc6: INFO]         "BatchSize": 16,[07/13/2021 21:24:15 > ef3fc6: INFO]         "NewBatchThreshold": 16,[07/13/2021 21:24:15 > ef3fc6: INFO]         "MaxPollingInterval": "00:01:00",[07/13/2021 21:24:15 > ef3fc6: INFO]         "MaxDequeueCount": 5,[07/13/2021 21:24:15 > ef3fc6: INFO]         "VisibilityTimeout": "00:00:00",[07/13/2021 21:24:15 > ef3fc6: INFO]         "MessageEncoding": "Base64"
[07/13/2021 21:24:15 > ef3fc6: INFO]       }
[07/13/2021 21:24:15 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:24:15 > ef3fc6: INFO]       SingletonOptions
[07/13/2021 21:24:15 > ef3fc6: INFO]       {
[07/13/2021 21:24:15 > ef3fc6: INFO]         "LockPeriod": "00:00:15",[07/13/2021 21:24:15 > ef3fc6: INFO]         "ListenerLockPeriod": "00:01:00",[07/13/2021 21:24:15 > ef3fc6: INFO]         "LockAcquisitionTimeout": "10675199.02:48:05.4775807",[07/13/2021 21:24:15 > ef3fc6: INFO]         "LockAcquisitionPollingInterval": "00:00:05",[07/13/2021 21:24:15 > ef3fc6: INFO]         "ListenerLockRecoveryPollingInterval": "00:01:00"
[07/13/2021 21:24:15 > ef3fc6: INFO]       }
[07/13/2021 21:24:15 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:24:15 > ef3fc6: INFO]       BlobsOptions
[07/13/2021 21:24:15 > ef3fc6: INFO]       {
[07/13/2021 21:24:15 > ef3fc6: INFO]         "MaxDegreeOfParallelism": 16
[07/13/2021 21:24:15 > ef3fc6: INFO]       }
[07/13/2021 21:24:15 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.JobHostService[0]
[07/13/2021 21:24:15 > ef3fc6: INFO]       Starting JobHost
[07/13/2021 21:24:15 > ef3fc6: INFO] info: Host.Startup[0]
[07/13/2021 21:24:15 > ef3fc6: INFO]       Found the following functions:
[07/13/2021 21:24:15 > ef3fc6: INFO]       Ross.WebJob.Tsl.Updater.Functions.CopyDates
[07/13/2021 21:24:15 > ef3fc6: INFO]       
[07/13/2021 21:24:15 > ef3fc6: INFO] info: Host.Startup[0]
[07/13/2021 21:24:15 > ef3fc6: INFO]       Job host started
[07/13/2021 21:24:15 > ef3fc6: INFO] Application started. Press Ctrl+C to shut down.
[07/13/2021 21:24:15 > ef3fc6: INFO] Hosting environment: Production
[07/13/2021 21:24:15 > ef3fc6: INFO] Content root path: D:\local\Temp\jobs\continuous\My.WebJob.Updater\s02hk5ti.50s\
[07/13/2021 21:34:22 > ef3fc6: SYS INFO] Detected WebJob file/s were updated,refreshing WebJob
[07/13/2021 21:34:22 > ef3fc6: SYS INFO] Status changed to Stopping
[07/13/2021 21:34:27 > ef3fc6: ERR ] Thread was being aborted.
[07/13/2021 21:34:27 > ef3fc6: SYS INFO] WebJob process was aborted
[07/13/2021 21:34:27 > ef3fc6: SYS INFO] Status changed to Stopped
[07/13/2021 21:34:28 > ef3fc6: SYS INFO] Status changed to Starting
[07/13/2021 21:34:28 > ef3fc6: SYS INFO] Run script 'run.cmd' with script host - 'WindowsScriptHost'
[07/13/2021 21:34:28 > ef3fc6: SYS INFO] Status changed to Running
[07/13/2021 21:34:28 > ef3fc6: INFO] 
[07/13/2021 21:34:28 > ef3fc6: INFO] D:\local\Temp\jobs\continuous\My.WebJob.Tsl.Updater\s02hk5ti.50s>dotnet Ross.WebJob.Tsl.Updater.dll  
[07/13/2021 21:34:29 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Extensions.Storage.Blobs.Config.BlobsExtensionConfigProvider[0]
[07/13/2021 21:34:29 > ef3fc6: INFO]       registered http endpoint = 
[07/13/2021 21:34:29 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:34:29 > ef3fc6: INFO]       LoggerFilterOptions
[07/13/2021 21:34:29 > ef3fc6: INFO]       {
[07/13/2021 21:34:29 > ef3fc6: INFO]         "MinLevel": "Information",[07/13/2021 21:34:29 > ef3fc6: INFO]         "Rules": []
[07/13/2021 21:34:29 > ef3fc6: INFO]       }
[07/13/2021 21:34:29 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:34:29 > ef3fc6: INFO]       FunctionResultAggregatorOptions
[07/13/2021 21:34:29 > ef3fc6: INFO]       {
[07/13/2021 21:34:29 > ef3fc6: INFO]         "BatchSize": 1000,[07/13/2021 21:34:29 > ef3fc6: INFO]         "FlushTimeout": "00:00:30",[07/13/2021 21:34:29 > ef3fc6: INFO]         "IsEnabled": true
[07/13/2021 21:34:29 > ef3fc6: INFO]       }
[07/13/2021 21:34:29 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:34:29 > ef3fc6: INFO]       QueuesOptions
[07/13/2021 21:34:29 > ef3fc6: INFO]       {
[07/13/2021 21:34:29 > ef3fc6: INFO]         "BatchSize": 16,[07/13/2021 21:34:29 > ef3fc6: INFO]         "NewBatchThreshold": 16,[07/13/2021 21:34:29 > ef3fc6: INFO]         "MaxPollingInterval": "00:01:00",[07/13/2021 21:34:29 > ef3fc6: INFO]         "MaxDequeueCount": 5,[07/13/2021 21:34:29 > ef3fc6: INFO]         "VisibilityTimeout": "00:00:00",[07/13/2021 21:34:29 > ef3fc6: INFO]         "MessageEncoding": "Base64"
[07/13/2021 21:34:29 > ef3fc6: INFO]       }
[07/13/2021 21:34:29 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:34:29 > ef3fc6: INFO]       SingletonOptions
[07/13/2021 21:34:29 > ef3fc6: INFO]       {
[07/13/2021 21:34:29 > ef3fc6: INFO]         "LockPeriod": "00:00:15",[07/13/2021 21:34:29 > ef3fc6: INFO]         "ListenerLockPeriod": "00:01:00",[07/13/2021 21:34:29 > ef3fc6: INFO]         "LockAcquisitionTimeout": "10675199.02:48:05.4775807",[07/13/2021 21:34:29 > ef3fc6: INFO]         "LockAcquisitionPollingInterval": "00:00:05",[07/13/2021 21:34:29 > ef3fc6: INFO]         "ListenerLockRecoveryPollingInterval": "00:01:00"
[07/13/2021 21:34:29 > ef3fc6: INFO]       }
[07/13/2021 21:34:29 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
[07/13/2021 21:34:29 > ef3fc6: INFO]       BlobsOptions
[07/13/2021 21:34:29 > ef3fc6: INFO]       {
[07/13/2021 21:34:29 > ef3fc6: INFO]         "MaxDegreeOfParallelism": 16
[07/13/2021 21:34:29 > ef3fc6: INFO]       }
[07/13/2021 21:34:29 > ef3fc6: INFO] info: Microsoft.Azure.WebJobs.Hosting.JobHostService[0]
[07/13/2021 21:34:29 > ef3fc6: INFO]       Starting JobHost
[07/13/2021 21:34:29 > ef3fc6: INFO] info: Host.Startup[0]
[07/13/2021 21:34:29 > ef3fc6: INFO]       Found the following functions:
[07/13/2021 21:34:29 > ef3fc6: INFO]       My.WebJob.Updater.Functions.CopyDates
[07/13/2021 21:34:29 > ef3fc6: INFO]       
[07/13/2021 21:34:30 > ef3fc6: INFO] info: Host.Startup[0]
[07/13/2021 21:34:30 > ef3fc6: INFO]       Job host started
[07/13/2021 21:34:30 > ef3fc6: INFO] Application started. Press Ctrl+C to shut down.
[07/13/2021 21:34:30 > ef3fc6: INFO] Hosting environment: Production
[07/13/2021 21:34:30 > ef3fc6: INFO] Content root path: D:\local\Temp\jobs\continuous\My.WebJob.Updater\s02hk5ti.50s\
[07/13/2021 22:34:28 > ef3fc6: SYS INFO] WebJob is still running
[07/14/2021 10:34:28 > ef3fc6: SYS INFO] WebJob is still running

更新 3:终于有了一些东西 - 不支持 TLS 版本

当我选择查看流日志时,VS 中输出窗格中的片段

Application:Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
Application:   at System.Net.HttpWebRequest.GetResponse()
Application:   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd,IRetryPolicy policy,OperationContext operationContext)
Application:   --- End of inner exception stack trace ---
Application:   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd,OperationContext operationContext)
Application:   at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.DownloadBlockList(BlockListingFilter blockListingFilter,AccessCondition accessCondition,BlobRequestOptions options,OperationContext operationContext)
Application:   at Microsoft.WindowsAzure.WebSites.Diagnostics.AzureBlobTraceListener.AppendStreamToBlob(Stream stream)
Application:   at Microsoft.WindowsAzure.WebSites.Diagnostics.AzureBlobTraceListener.ConsumeBuffer()
Application:Request Information
Application:RequestID:6bbeae68-701e-009c-0ad8-787fc9000000
Application:RequestDate:Wed,14 Jul 2021 17:46:01 GMT
Application:StatusMessage:The TLS version of the connection is not permitted on this storage account.
Application:ErrorCode:TlsVersionNotPermitted

解决方法

我可能遇到了很多问题,但是,我终于克服了。 以下是我为解决困境所做的事情:

1.) 我需要将 AzureWebJobsStorage 键值放入我的 Application Settings(也许以前的 webjobs 迭代会从 Connection Strings 获取它 - 但不再)

2.) 我使用了一些 5.0.0 beta NuGet 包,这些包可能在 #1 上失败了,所以当我把它们滚回来的时候。 (特别是 Microsoft.Azure.WebJobs.Extensions.Storage - 我回滚到 3.x.x - 那时我开始收到日志中显示的 TLS 版本错误。4.0.4 稳定且快乐)

这是我目前的 NuGet 状态:
enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...