我正在运行一个。Net Core 2.1.2 WebJob在localhost与这些设置:
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
这是Program. cs:
public static class Program
{
public static IConfigurationRoot Configuration { get; set; }
private static ConnectionStringsSettings _connectionStringsSettings;
private static ConnectionStringsSettings ConnectionStringsSettings
{
get
{
return _connectionStringsSettings ?? (_connectionStringsSettings = Configuration
.GetSection("ConnectionStrings")
.Get<ConnectionStringsSettings>()
);
}
}
public static async Task Main(string[] args)
{
var host = new HostBuilder()
.ConfigureAppConfiguration((_, configuration) =>
{
configuration.AddJsonFile("appsettings.json", optional: true);
configuration.AddJsonFile("appsettings.Development.json", optional: true);
configuration.AddEnvironmentVariables();
if (args != null)
{
configuration.AddCommandLine(args);
}
Configuration = configuration.Build();
})
.ConfigureServices((hostContext, services) =>
{
services.AddOptions();
services.Configure<ConnectionStringsSettings>(hostContext.Configuration.GetSection("ConnectionStrings"));
services.Configure<AzureStorageSettings>(hostContext.Configuration.GetSection("AzureStorage"));
services.AddApplicationInsightsTelemetry(hostContext.Configuration);
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
})
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterType<QueueFunctions>().InstancePerDependency();
builder.RegisterType<VideosCompressorQueueService>().As<IVideosQueueService>();
})
.Build()
.EnsureQueueExists();
var jobHostConfiguration = new JobHostConfiguration
{
JobActivator = new JobActivator(host),
DashboardConnectionString = ConnectionStringsSettings.AzureWebJobsDashboard,
StorageConnectionString = ConnectionStringsSettings.AzureWebJobsStorage
};
var webjob = new JobHost(jobHostConfiguration);
webjob.RunAndBlock();
}
public static IHost EnsureQueueExists(this IHost host)
{
var videosQueueService = host.Services.GetRequiredService<IVideosQueueService>();
videosQueueService.EnsureQueueExistsAsync().Wait();
return host;
}
}
JobActivator. cs:公共类JobActivator:IJobActivator{私有只读IHost_host;
public JobActivator(IHost host)
{
_host = host;
}
public T CreateInstance<T>()
{
return _host.Services.GetService<T>();
}
}
和QueueFunctions. cs:公共类QueueFunctions{私有只读IOptions_azureStorageSettings;私有只读ILogger_logger;
public QueueFunctions(
IOptions<AzureStorageSettings> azureStorageSettings,
ILogger<QueueFunctions> logger
)
{
_azureStorageSettings = azureStorageSettings;
_logger = logger;
}
public async Task ProcessAsync([QueueTrigger(Constants.Queues.Videos)] string text)
{
Console.WriteLine(text);
}
}
添加的新主机生成器是否有问题。Net Core 2?很可能我在代码中遗漏了什么?
对于那些有相同问题的人,问题来自Microsoft Azure WebJob nuget包的版本。我需要更新到:
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta5" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0-beta5" />
现在所有的事情都正常工作。