0
votes

I've build a Asp.Net core 3.1 web api. In local my coding is working fine. But while publishing it in the server, I was getting error as

System.NullReferenceException: Object reference not set to an instance of an object. at Nypoo.WEBAPI.Startup.ConfigureServices(IServiceCollection services) in D:\E-Commerce\Application\WEBAPI\WEBAPI\Startup.cs:line 43 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.g__Startup|0(IServiceCollection serviceCollection) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.b__0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.b__2(IApplicationBuilder app) at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.g__MiddlewareFilterBuilder|0(IApplicationBuilder builder) at Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter.<>c__DisplayClass4_0.b__0(IApplicationBuilder app) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder app)

I'm attaching my StartUp.cs file code as well

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);
}

In the error it shows my local folder path which I've highlighted in bold and the error is at the line where I was configuring the Token settings by reading them from appsettings.json file. I've tried by having different appsettings files for Production and development but nothing seems to be working. Can someone help me with this. Following is my appsettings.Production.json file


{
  "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"
  }
}


1

1 Answers

1
votes

Make sure you use appropriate appsetttings.json for configuration.

If you use Development environment, appsettings.Development.json file overrides the data in appsettings.json.

If you use Production environment, appsettings.Production.json file overrides the data in appsettings.json.

When we publish our app default case is Production, So make sure tokenSetting section is in appsettings.Production.json or appsettings.json files.