gpt4 book ai didi

c# - 当从不同的目录运行时,记录器在 asp.net 应用程序中为 null

转载 作者:太空宇宙 更新时间:2023-11-04 09:58:05 25 4
gpt4 key购买 nike

我正在尝试在 Linux 上运行一个 asp.net 核心 Web 应用程序,如果我从它的目录运行它,它会工作,但如果我尝试从不同的目录运行它,我会得到一个 NullReferenceException 来自 Ilogger

我有这样定义的服务

    public class GPIOOutput : Output
{
private ILogger<GPIOOutput> logger;
private List<IGpioPin> pins;

public GPIOOutput(IOptions<GPIOOptions> options, ILogger<GPIOOutput> logger)
:base(options.Value.Pins.Count)
{
this.logger = logger;
pins = options.Value.Pins.Select(p => Pi.Gpio[p]).ToList();
pins.ForEach(p => p.PinMode = GpioPinDriveMode.Output);
UpdateOutput();
}

protected override void UpdateOutput()
{
for (int i = 0; i < pins.Count; i++)
{
pins[i].Write(!channelData[i]);
logger.LogInformation(channelData[i].ToString());
}
}
}

我是这样添加的:

public static void AddGPIO(this IServiceCollection services)
{
var config = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
services.AddOptions<GPIOOptions>().Bind(config.GetSection("Hardware:GPIO"));
services.AddSingleton<IOutput, GPIOOutput>();
}

我在自包含模式下构建项目并将 if 复制到我的树莓派。它位于 /home/pi/test 中。当我这样运行它时,它起作用了:

pi@raspberrypi:~ $ cd test
pi@raspberrypi:~/test $ sudo ./SmartHouse --urls "http://0.0.0.0:80/"
Hosting environment: Production
Content root path: /home/pi/test
Now listening on: http://0.0.0.0:80
Application started. Press Ctrl+C to shut down.

但是如果我像这样从 /home/pi 尝试,我会收到以下错误:

pi@raspberrypi:~ $ sudo ./test/SmartHouse --urls "http://0.0.0.0:80/"

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an
at SmartHouse.RPI.GPIO.GPIOOutput..ctor(IOptions`1 options, ILogger`1 logger) in E:\Efraim's souse\SmartHouse\RPI\GPIO\GPIOOutput.cs:line 22
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstruuctorCallSite constructorCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ISere callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ite scopedCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingletnCallSite singletonCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ISere callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstruuctorCallSite constructorCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ISere callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ite scopedCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingletnCallSite singletonCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ISere callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstruuctorCallSite constructorCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ISere callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransietCallSite transientCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ISere callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerrableCallSite enumerableCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ISere callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstruuctorCallSite constructorCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ISere callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ite scopedCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingletnCallSite singletonCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ISere callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__Di_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Typpe, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Typpe)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredServicrovider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredServicceProvider provider)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.StartAsync(CancellationToken cancellationToke
at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken to shutdownMessage)
at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken to
at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
at SmartHouse.Program.Main(String[] args) in E:\Efraim's stuff\SmartHouse\SmartHouse\Program.c
Aborted

最佳答案

我在我的项目中遇到了类似的问题。 如果我的项目在不在项目文件夹(或发布文件夹)中时运行命令行,Kestrel 没有从项目或 appsettings.json 文件中找到任何文件...并且 appsettings.json 值变为空并且我的应用程序出现故障这也没有在我的项目中找到任何 html 内容......

I solved like this, I create a static prop on class in my Program.cs and name is "PathDomain"

之后,我使用此命令获取应用程序路径域

PathDomain = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;

        public static string PathDomain { get; set; }
public static void Main(string[] args)
{

PathDomain = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
CreateWebHostBuilder(args).Build().Run();
Console.WriteLine("Api started :)");

}

Here is come magic touch

IWebHostBuilder CreateWebHostBuilder 方法中,我使用 UseContentRoot() 方法并将我的 PathDomain 变量传递给方法

我的方法看起来像这样

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<ProjenInlineApiStartup>()
.UseKestrel()
.UseContentRoot(PathDomain)//<------THIS IS THE LINE
.ConfigureAppConfiguration((hostingContext, config) =>
{

string CONFIG_SERVER_FILE = Environment.GetEnvironmentVariable("CONFIG_SERVER_FILE");
string RUN_PLATFORM = Environment.GetEnvironmentVariable("RUN_PLATFORM");
string ASPNETCORE_ENVIRONMENT = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Console.WriteLine("ASPNETCORE_ENVIRONMENT: " + ASPNETCORE_ENVIRONMENT);
Console.WriteLine("CONFIG_SERVER_FILE: "+ CONFIG_SERVER_FILE);
Console.WriteLine("RUN_PLATFORM: " + RUN_PLATFORM);

config.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{RUN_PLATFORM}.json", true, true);

config.AddEnvironmentVariables();

if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureLogging((hostingContext, config) => { config.ClearProviders(); });

希望对你有帮助:)

关于c# - 当从不同的目录运行时,记录器在 asp.net 应用程序中为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58524538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com