我正在尝试在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>();
}
我以自包含模式构建了项目,并将其复制到树莓派中。它在/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内容...
我这样解决,我在Program.cs的类上创建了一个静态道具并且名称是“ 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 :)");
}
这里是魔术般的触摸
在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(); });
希望对您有帮助:)