• 【日常排雷】 .Net core 生产环境appsetting读取失败


    关键词System.ArgumentNullException: String reference not set to an instance of a String. (Parameter 's')

    1.问题出现

    某年某月某日,把webapi开发完了,也通过了swagger进行了单元测试。

    dotnet build
    dotnet publish -o publish
    dotnet .publishxx.Webapi.dll
    

    然后String reference not set to an instance of a String. (Parameter 's')

    fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
          An unhandled exception has occurred while executing the request.
    System.ArgumentNullException: String reference not set to an instance of a String. (Parameter 's')
       at System.Text.Encoding.GetBytes(String s)
       at AliMobilePush.Webapi.Startup.<ConfigureServices>b__5_2(JwtBearerOptions option) in E:工作事项codedev-pushAlipushAliMobilePush.WebapiStartup.cs:line 75
       at Microsoft.Extensions.Options.ConfigureNamedOptions`1.Configure(String name, TOptions options)
       at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
       at Microsoft.Extensions.Options.OptionsMonitor`1.<>c__DisplayClass11_0.<Get>b__0()
       at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
       at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.get_Value()
       at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
       at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name)
       at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
       at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
       at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
       at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
       at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
       at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
       at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
    

    配置文件读出来为null,为什么?

    2.难道是非管理员,权限不够?

    切换至管理员,运行上述命令,还是报错。

    3.无意间解决

    cd publish
    dotnet xx.Webapi.dll
    

    就能读到了,这是为什么,根据上述命令的差异,大概能猜测到应该是路径不同,导致读取appsetting.json失败。

    4.原来是这样

    dotnet run,应该会根据env.ContentRootPath(env 的类型是IHostingEnvironment )来读取配置文件 appsettings.Production.jsonappsettings.json文件,ContentRootPath 属性得到的值为当前启动命令的目录,而不是dll所在的目录,所以应在发布项目dll所在的目录执行 dotnet xx.dll,否则会导致配置文件里面的参数读取不到。请看源码和命令对比:

    // Host.CreateDefaultBuilder(args)
    
    //源码Host.cs           
    builder.ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;
    
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
    
                    if (env.IsDevelopment() && !string.IsNullOrEmpty(env.ApplicationName))
                    {
                        var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                        if (appAssembly != null)
                        {
                            config.AddUserSecrets(appAssembly, optional: true);
                        }
                    }
    
                    config.AddEnvironmentVariables();
    
                    if (args != null)
                    {
                        config.AddCommandLine(args);
                    }
                })
    

    5.参考链接

    https://www.cnblogs.com/DHclly/p/9606866.html

    https://www.netnr.com/home/list/115

    https://github.com/dotnet/extensions.git

    https://my.oschina.net/u/4364008/blog/3205437

  • 相关阅读:
    matlab-图像处理-数字图像去噪
    matlab-图像处理-边缘检测算法五种
    深度学习-梯度下降原理-学习率的作用-反向传播-神经网络基础构架-正则化与激活函数
    深度学习-softmax分类器
    Jvm调优理论篇
    美团分布式定时调度框架XXL-Job基本使用
    Mongodb的基本使用及对接多数据源
    登录注册框手机号和验证码校验--前端部分
    登录接口jmeter提取token,用于后面模块验证
    jmeter之报文造数据,生成随机数字,,,生成前面固定格式在随机数字
  • 原文地址:https://www.cnblogs.com/RandyField/p/13028490.html
Copyright © 2020-2023  润新知