引发异常:Microsoft.Extensions.DependencyInjection中的“ System.InvalidOperationException”

时间:2019-05-09 10:12:39

标签: asp.net azure core azure-webjobs

我正在使用asp.net核心进行webjobs,我无法正确设置依赖项注入。

public static async Task Main(string[] args)
    {
            var host = BuildHost(args);

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


    public static IHost BuildHost(string[] args) =>
new HostBuilder()
    .ConfigureHostConfiguration(builder => 
    {
        builder
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();
    })
    .ConfigureWebJobs(b =>
    {
        b.AddAzureStorageCoreServices();
        b.AddAzureStorage();
        b.AddTimers();
    })
    .ConfigureServices((hostBuilderContext, services) =>
    {
        services.AddScoped<ISchedularService, SchedularService>();
        //services.AddSingleton<IHostedService, PrintTimeService>()
    }).Build();

并且在functions.cs文件中,我已经访问了构造函数中的调度程序服务。但是它没有触发构造函数,并引发了类似“ Microsoft.Extensions.DependencyInjection中抛出的异常:'System.InvalidOperationException'”

public class Functions
    {

    private readonly ISchedularService _schedularService;
    public Functions(SchedularService schedularService)
    {
        _schedularService = schedularService;
    }
}

2 个答案:

答案 0 :(得分:0)

请确保您使用的是最新软件包。我将您的代码与最新软件包一起使用,并且效果很好。

我没有使用异步方法。

       static void Main(string[] args)
        {

            var host = BuildHost(args);

            using (host)
            {
                 host.Run();
            }           
        }

        public static IHost BuildHost(string[] args) =>
            new HostBuilder()
            .ConfigureHostConfiguration(builder =>
            {
                builder.SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .AddEnvironmentVariables();
            })
            .ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                b.AddAzureStorage();
                b.AddTimers();
            })
            .ConfigureServices((hostBuilderContext, services) =>
            {
                services.AddScoped<IWebJobConfiguration,WebJobConfiguration>();
            })
            .Build();

在Functions.cs中:

        private readonly IWebJobConfiguration _webJobConfiguration;
        public Functions(IWebJobConfiguration webJobConfiguration)
        {
            Console.WriteLine("*** DI via constructor ***");
            _webJobConfiguration = webJobConfiguration;
        }

        public void ProcessQueueMessage([QueueTrigger("queue111")] string message, ILogger logger)
        {
            Console.WriteLine(_webJobConfiguration.Message);
            Console.WriteLine(message + ";consoleqqqq");
        }

结果:

enter image description here

答案 1 :(得分:0)

DependencyInjection问题通常在向服务添加错误的接口时发生。我遇到了类似的问题,我的代码仅适用于.Net Core 2.x,不适用于.Net Core3.x。 可能在3.x的接口中有更多控件。 这两个相同的注入在2.x中一起工作,但在3.x中不工作

        services.AddScoped<IHttpClientFactory, HttpClientFactory>();

        services.AddSingleton<IHttpClientFactory>(new HttpClientFactory(
            Configuration["PortalLogin:Adress"],
            LoginApiUserName,
            LoginApiPassword));