我正在创建一个由Azure Q存储上的Q消息触发的.net核心Azure Web作业。
触发网络作业时,出现以下错误。
System.InvalidOperationException:“尝试激活'xxx.Functions'时,无法解析类型为'xxx.SERVICE.Cloud.ICloudStorageService'的服务。”
有什么建议吗?
在program.cs / main中,我连接了DI:
IServiceCollection services = new ServiceCollection();
//...
services.AddTransient<ICloudStorageService, AzureBlobStorageService>();
还有网络工作的“麻烦”
public class Functions
{
private readonly ICloudStorageService _storageService;
public Functions(ICloudStorageService cloudStorageService)
{
_storageService = cloudStorageService;
}
public async Task ProcessQueueMessage(
[QueueTrigger(AzureQueues.NoShowUploads)] BatchJob job
, ILogger logger
)
{
//...
}
}
AzureBlobStorageService
类实现了ICloudStorageService
接口
public class AzureBlobStorageService : ICloudStorageService
{
//...
}
更新:
class Program
{
static void Main(string[] args)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
IServiceCollection services = new ServiceCollection();
services.Configure<SendGridClientOptions>(options =>
{
services.Configure<AzureQueueOptions>(options =>
{
configuration.GetSection("AzureQueueOptions").Bind(options);
options.StorageConnectionString = configuration.GetConnectionString("Storage");
});
services.Configure<AzureBlobStorageOptions>(options =>
{
configuration.GetSection("AzureBlobStorageOptions").Bind(options);
options.StorageConnectionString = configuration.GetConnectionString("Storage");
});
services.AddTransient<Functions, Functions>();
services.AddTransient<ICloudStorageService, AzureBlobStorageService>();
services.AddTransient<WebJobEntryPoint>();
services.BuildServiceProvider().GetService<WebJobEntryPoint>().Run();
}
}
webentrypoint类
public class WebJobEntryPoint
{
private readonly MailOptions _mailOptions;
private readonly SendGridClientOptions _sendGridClientOptions;
private readonly AzureBlobStorageOptions _azureBlobStorageOptions;
private readonly AzureQueueOptions _azureQueueOptions;
public WebJobEntryPoint(
IOptionsMonitor<SendGridClientOptions> sendGridClientOptionsMonitor,
IOptionsMonitor<MailOptions> mailOptionsMonitor,
IOptionsMonitor<AzureBlobStorageOptions> azureBlobStorageOptionsMonitor,
IOptionsMonitor<AzureQueueOptions> azureQueueOptionsMonitor)
{
_sendGridClientOptions = sendGridClientOptionsMonitor.CurrentValue;
_mailOptions = mailOptionsMonitor.CurrentValue;
_azureBlobStorageOptions = azureBlobStorageOptionsMonitor.CurrentValue;
_azureQueueOptions = azureQueueOptionsMonitor.CurrentValue;
}
public void Run()
{
var builder = new HostBuilder()
.ConfigureWebJobs(webJobConfiguration =>
{
webJobConfiguration.AddAzureStorageCoreServices();
webJobConfiguration.AddAzureStorage(c => c.BatchSize = _azureQueueOptions.BatchSize);
})
.ConfigureLogging((context, b) =>
{
b.AddConsole();
});
var host = builder.Build();
using (host)
{
host.Run();
}
}
}
答案 0 :(得分:3)
对于您当前的设计,主机不知道主要进行的服务注册。
这是基于原始示例中提供的内容的当前设置的简化:
class Program {
static void Main(string[] args) {
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var azureQueueOptions = configuration.GetSection("AzureQueueOptions").Get<AzureQueueOptions>();
azureQueueOptions.StorageConnectionString = configuration.GetConnectionString("Storage");
var builder = new HostBuilder()
.ConfigureWebJobs(webJobConfiguration => {
webJobConfiguration.AddAzureStorageCoreServices();
webJobConfiguration.AddAzureStorage(c => c.BatchSize = azureQueueOptions.BatchSize);
})
.ConfigureServices(services => {
services.AddTransient<Functions>();
services.AddTransient<ICloudStorageService, AzureBlobStorageService>();
})
.ConfigureLogging((context, config) => {
config.AddConsole();
});
var host = builder.Build();
host.Run();
}
}
创建一个实际的Startup
并让构建者使用它:
var builder = new HostBuilder()
.UseStartup<Startup>()
//...omitted for brevity