【问题标题】:System.NullReferenceException at Startup.ConfigureServices(IServiceCollection services) in windows serverWindows 服务器中 Startup.ConfigureServices(IServiceCollection 服务)的 System.NullReferenceException
【发布时间】:2020-10-26 09:32:13
【问题描述】:

我已经构建了一个 Asp.Net core 3.1 web api。在本地,我的编码工作正常。但是在服务器上发布它时,我遇到了错误

System.NullReferenceException:对象引用未设置为对象的实例。 在 D:\E-Commerce\Application\WEBAPI\WEBAPI\Startup.cs:line 43 中的 Nypoo.WEBAPI.Startup.ConfigureServices(IServiceCollection services) 在 System.RuntimeMethodHandle.InvokeMethod(对象目标,对象 [] 参数,签名 sig,布尔构造函数,布尔 wrapExceptions) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object[] 参数,CultureInfo 文化) 在 Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(对象实例,IServiceCollection 服务) 在 Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.c__DisplayClass9_0.g__Startup|0(IServiceCollection serviceCollection) 在 Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(对象实例,IServiceCollection 服务) 在 Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.c__DisplayClass8_0.b__0(IServiceCollection 服务) 在 Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(类型 startupType、HostBuilderContext 上下文、IServiceCollection 服务) --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.c__DisplayClass13_0.b__2(IApplicationBuilder 应用程序) 在 Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.c__DisplayClass0_0.g__MiddlewareFilterBuilder|0(IApplicationBuilder 构建器) 在 Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter.c__DisplayClass4_0.b__0(IApplicationBuilder 应用程序) 在 Microsoft.AspNetCore.HostFilteringStartupFilter.c__DisplayClass0_0.b__0(IApplicationBuilder 应用程序)

我也附上了我的 StartUp.cs 文件代码

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers(o => o.InputFormatters.Insert(0, new RawRequestBodyFormatter()));

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddTransient<IHelperSettings, HelperSettings>();
            services.AddScoped<IHelperService, HelperService>();
            services.AddScoped<IAuthenticationService, AuthenticationService>();

            services.Configure<TokenSettings>(Configuration.GetSection("tokenSetting"));
            var token = Configuration.GetSection("tokenSetting").Get<TokenSettings>();
            var secret = Encoding.ASCII.GetBytes(token.Secret);
}

在错误中,它显示了我用粗体突出显示的本地文件夹路径,错误出现在我通过从 appsettings.json 文件中读取令牌设置来配置令牌设置的那一行。我尝试为生产和开发使用不同的 appsettings 文件,但似乎没有任何效果。有人可以帮我弄这个吗。以下是我的 appsettings.Production.json 文件


{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "tokenSetting": {
    "secret": "my secret key",
    "issuer": "issuer",
    "audience": "audience",
    "accessExpiration": 30,
    "refreshExpiration": 60
  },
  "ConnectionStrings": {
    "NypooConnectionString": "Data Source=*******;Initial Catalog=***;Persist Security Info=True;User ID=***;Password=***;MultipleActiveResultSets=True"
  }
}


【问题讨论】:

    标签: asp.net-core asp.net-core-webapi


    【解决方案1】:

    确保使用适当的appsetttings.json 进行配置。

    如果您使用开发环境,appsettings.Development.json 文件会覆盖appsettings.json 中的数据。

    如果您使用生产环境,appsettings.Production.json 文件会覆盖appsettings.json 中的数据。

    当我们发布我们的应用默认情况是生产,所以确保tokenSetting部分在appsettings.Production.jsonappsettings.json文件中。

    【讨论】:

    • 我已将我的 appsettings 文件附加到问题中。它有令牌设置部分。我不明白为什么错误中的路径指向我的本地文件夹
    • 我确实有那个文件。
    • 确保文件存在于已发布的应用程序中
    • 它与 appsettings.json 和 appsettings.Development.json 一起存在于已发布的应用程序中
    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多