Exception retrieving IFeatureService
Kronos11 opened this issue · 9 comments
Steps To Reproduce:
- Create ASP.NET Core App 3.1
- Install Esquio.HttpStore, Esquio.AspNetCore
- Register Esquio in ConfigureServices
services.AddEsquio(setup => setup.ConfigureDefaultDeploymentName(environment.EnvironmentName ?? "Development").ConfigureDefaultProductName(DefaultProductName).ConfigureOnErrorBehavior(OnErrorBehavior.Throw).ConfigureNotFoundBehavior(NotFoundBehavior.SetDisabled))
.AddAspNetCoreDefaultServices()
.AddHttpStore(options => options.UseBaseAddress(Url).UseApiKey(ApiKey));
- Attempt to retrieve IFeatureService via dependency injection
An exception of type 'System.NullReferenceException' occurred in Esquio.AspNetCore.dll but was not handled in user code: 'Object reference not set to an instance of an object.' at Esquio.AspNetCore.Providers.HttpContextScopedEvaluationHolder..ctor(IHttpContextAccessor httpContextAccessor) at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
Hopefully I'm missing something obvious but I couldn't find it based on looking at the demo app.
Other details .net sdk 3.1.6
Packages version? Demos is working well for you?
Packages 3.1.0
Demos is working fine
HttpContext from HttpContextAccessor is null for some reason.
Do you have addmvc or addcontrollerwithviews
tried both .AddMvc()
.AddControllersWithViews()
same issue
I condensed the entire thing into a unit test for easy access
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(c => c.ConfigureAppConfiguration(c => c.AddEnvironmentVariables()
.AddUserSecrets("83a46d2f-ad5e-4b7c-a91a-8ccc8d523a07")
.AddJsonFile("appsettings.json", true)
.AddJsonFile("esquio.json", true)
)
.ConfigureServices((c,s) => s.AddControllersWithViews().Services.AddEsquio().AddHttpStore(setup => setup.UseBaseAddress(c.Configuration["EsquioUrl"]))
.AddAspNetCoreDefaultServices()
.AddEndpointFallback((context) => {
context.Response.StatusCode = StatusCodes.Status404NotFound;
return Task.CompletedTask;
}))).Build();
var httpContextAccessor = host.Services.GetRequiredService<IHttpContextAccessor>();
var context = httpContextAccessor.HttpContext;
var fs = host.Services.GetRequiredService<IFeatureService>(); /// Throws exception here
Can you send me the repro?
See UnitTest above, also working on a repro test in Solution
Digging in a little more, it appears this may be more related to the fact that I'm using this outside of a scope of traditional asp.net core services. ie: I have a job that is running and kicking off, and when it does there is not HttpContext and thus you get this behavior. Ideally there should be a null reference check at the very least.
Thanks for contribute this!