简体   繁体   English

由于 serviceProvider.GetService,无法添加 singleton 服务<irabbitmqconsumer> () 返回 null</irabbitmqconsumer>

[英]Unable to add a singleton service due to serviceProvider.GetService<IRabbitMQConsumer>() returning null

I am trying to add a singleton service of the type mentioned in IRabbitMQConsumer with an implementation type in RabbitMQConsumer to the Iservicecollection.我正在尝试将 IRabbitMQConsumer 中提到的类型的 singleton 服务与 RabbitMQConsumer 中的实现类型添加到 Iservicecollection。 But when I tried using below approach, I am receiving Null Reference exception as the GetService<ILoggerFactory> is returning null value.但是当我尝试使用以下方法时,我收到 Null 引用异常,因为 GetService<ILoggerFactory> 正在返回 null 值。

This code was working when I was using.Net core 2.2 and appears as a problem when I have migrated to.Net core 3.1.12.此代码在我使用 .Net core 2.2 时有效,并且在我迁移到 .Net core 3.1.12 时出现问题。 I have tried many stack-overflow threads as below, where it seems to be correct but not sure why my code is throwing an exception.我已经尝试了许多堆栈溢出线程,如下所示,它似乎是正确的,但不确定我的代码为什么会抛出异常。 Resolving instances with ASP.NET Core DI from within ConfigureServices , How to Resolve Instance Inside ConfigureServices in ASP.NET Core Please find my code below and kindly help me to fix this problem as I am very beginner in these middle-wares. 从 ConfigureServices 中使用 ASP.NET 核心 DI 解析实例,如何在 ASP.NET 核心中解析 ConfigureServices 中的实例请在下面找到我的代码并帮助我解决这个问题,因为我是这些中间件的初学者。

#Program.cs #程序.cs

internal class Program
{
    private static void Main(string[] args)
    {
        IServiceCollection services = new ServiceCollection();
        IServiceProvider serviceProvider = services.BuildServiceProvider();

        Startup startup = new Startup();
        startup.ConfigureServices(services, logger);

        //configure console logging
        serviceProvider.GetService<ILoggerFactory>();
        var logger = serviceProvider.GetService<ILoggerFactory>(); // logger returns null

        //do the actual work here
        var client = serviceProvider.GetService<IRabbitMQConsumer>(); // client returns null
        client.CreateConnection();
        client.ProcessMessages();

    }
}

#startup.cs #startup.cs

 public class Startup
{
    private static readonly Logger _log = LogManager.GetCurrentClassLogger();
    IConfigurationRoot Configuration { get; }
    public Startup()
    {
        LogManager.LoadConfiguration(string.Concat(Directory.GetCurrentDirectory(), "/NLog.config"));
       
    }

    public void ConfigureServices(IServiceCollection services, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddNLog(); // System.NullReferenceException
        services.AddSingleton<IRabbitMQConsumer, RabbitMQConsumer>();// System.NullReferenceException
    }
}

You need to configure the ServiceCollection before you call BuildServiceProvider .您需要在调用BuildServiceProvider之前配置 ServiceCollection。

In your case, you should move the IServiceProvider serviceProvider = services.BuildServiceProvider();在您的情况下,您应该移动IServiceProvider serviceProvider = services.BuildServiceProvider(); line down below startup.ConfigureServices(services, logger);startup.ConfigureServices(services, logger);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ServiceProvider.GetService() 显示错误 - ServiceProvider.GetService() showing error ServiceProvider.GetService 为瞬态实例工厂中的作用域服务返回相同的实例 - ServiceProvider.GetService returns the same instance for scoped service in transient instance factory .Net Core 2.0控制台程序在运行`serviceProvider.GetService&#39;时具有空值 <Application> ();`? - .Net core 2.0 console program got null value when running `serviceProvider.GetService<Application>();`? 无论如何在.net核心中使用带有反射的ServiceProvider.GetService()? - is there anyway to use ServiceProvider.GetService() with reflection in .net core? 服务提供者.GetService<type> 每次使用都抛出异常</type> - ServiceProvider.GetService<Type> throw exception for every usage WCF服务未返回虚拟属性ServiceProvider - WCF Service not returning virtual property ServiceProvider HttpContext.RequestServices.GetService&lt;&gt;() 返回的证书验证服务为 null - Certificate Validation Service returned by HttpContext.RequestServices.GetService<>() is null ActionFilterAttribute 中的 GetService 返回 null - GetService in ActionFilterAttribute returns null DependencyResolver.Current.GetService 总是返回 null - DependencyResolver.Current.GetService always returns null TfsConfigurationServer.GetService <VersionControlServer> ()始终返回null - TfsConfigurationServer.GetService<VersionControlServer>() always returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM