简体   繁体   English

ConfigureServices() 中可能出现 null 引用返回警告

[英]Possible null reference return warning in ConfigureServices()

Here is my code -这是我的代码 -

IHost host = Host.CreateDefaultBuilder(args)
            .ConfigureServices((context, services) =>
            {
                var cns = context.Configuration.GetConnectionString("db");
                services.AddDbContext<DatabaseContext>(options => options.UseMySql(cns, ServerVersion.AutoDetect(cns)));

                //Possible null reference return warning here-
                services.AddScoped<IDatabaseContext>(provider => provider.GetService<DatabaseContext>());

                services.AddScoped<CensusService>();
            })
            .Build();

To remove that warning, what should be the right way to write the line?要删除该警告,该行的正确写法应该是什么?

ServiceProviderServiceExtensions.GetService<T> returns T? ServiceProviderServiceExtensions.GetService<T>返回T? which will become nullable reference type if T is a reference type, while the registration requires a non-nullable one.如果T是引用类型,它将成为可为空的引用类型,而注册需要一个不可为空的类型。

Use ServiceProviderServiceExtensions.GetRequiredService<T> which returns an instance or throws InvalidOperationException if there is no service of type T :使用ServiceProviderServiceExtensions.GetRequiredService<T>返回一个实例,如果没有T类型的服务则抛出InvalidOperationException

services.AddScoped<IDatabaseContext>(provider =>
    provider.GetRequiredService<DatabaseContext>());

But in case of DbContext registration there are overloads of AddDbContext allowing to split the contract and implementation (as suggested by Julien in the comments), so if you need to resolve context only via the interface you can use one of those:但是在DbContext注册的情况下,有AddDbContext的重载允许拆分合同和实现(如 Julien 在评论中所建议的那样),因此如果您只需要通过接口解析上下文,您可以使用其中之一:

services.AddDbContext<IDatabaseContext, DatabaseContext>(options => ...));

Related:有关的:

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

相关问题 为什么此代码会给出“Possible null reference return”编译器警告? - Why does this code give a "Possible null reference return" compiler warning? 将 JToken 显式转换为带有错误“可能 null 引用返回”警告的字符串 - Explicit conversion of JToken to string with false "possible null reference return" warning Resharper可能为空参考警告 - Resharper possible null reference warning 我收到“可能的 null 参考返回”警告,尽管结果可能被声明为 null - I get "Possible null reference return" warning, although result is declared maybe null 了解 可能 null 参考返回 - understanding Possible null reference return 可能的 null 参考返回 c# linq - possible null reference return c# linq 当空引用似乎不可能时,为什么我们会收到可能的取消引用空引用警告? - Why do we get possible dereference null reference warning, when null reference does not seem to be possible? 可能的空引用分配警告对我来说似乎是假的 - Possible null reference assignment warning seems bogus to me 如何解决此 SqlDataReader“Possible null reference assignment”警告? - How to resolve this SqlDataReader "Possible null reference assignment" warning? 关于XElement元素方法的Resharper可能为null的参考警告 - Resharper possible null reference warning on XElement Element method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM