Xabaril / Esquio

Esquio is a Feature Toggle Library for .NET Developers.
Apache License 2.0
428 stars 49 forks source link

Exception retrieving IFeatureService #157

Closed Kronos11 closed 4 years ago

Kronos11 commented 4 years ago

Steps To Reproduce:

  1. Create ASP.NET Core App 3.1
  2. Install Esquio.HttpStore, Esquio.AspNetCore
  3. 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));
  1. Attempt to retrieve IFeatureService via dependency injection
  2. 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

unaizorrilla commented 4 years ago

Packages version? Demos is working well for you?

Kronos11 commented 4 years ago

Packages 3.1.0 Demos is working fine HttpContext from HttpContextAccessor is null for some reason.

unaizorrilla commented 4 years ago

Do you have addmvc or addcontrollerwithviews

Kronos11 commented 4 years ago

tried both .AddMvc() .AddControllersWithViews() same issue

Kronos11 commented 4 years ago

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
unaizorrilla commented 4 years ago

Can you send me the repro?

Kronos11 commented 4 years ago

See UnitTest above, also working on a repro test in Solution

Kronos11 commented 4 years ago

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.

unaizorrilla commented 4 years ago

Thanks for contribute this!