JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
572 stars 119 forks source link

Lamar compilation failure with AspNetCore Healthchecks UI #135

Closed sstrink closed 5 years ago

sstrink commented 5 years ago

I have an existing aspnetcore solution, 2.2.105 sdk, using Lamar and I recently added the new healthcheck api to my solution. I am using the 2.1.0 version of Lamar with the latest version of the healthchecks api (2.2.x) and healthchecks ui (2.2.19) projects.

In my ConfigureContainer method I am registering healthchecks for redis, sql server, and publishing to application insights.

       services.AddHealthChecks()
            .AddApplicationInsightsPublisher()
            .AddSqlServer(...)
            .AddRedis(...)

Then adding

        services.AddHealthChecksUI();

In my Configure method I register the following hooks:

        app.UseHealthChecks("/hc",
            new HealthCheckOptions
            {
                Predicate = hc => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });

        app.UseHealthChecks("/hc-cache",
            new HealthCheckOptions
            {
                Predicate = hc => hc.Tags.Contains("cache"),
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });

        app.UseHealthChecks("/hc-db",
            new HealthCheckOptions
            {
                Predicate = hc => hc.Tags.Contains("database"),
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });

        app.UseHealthChecks("/hc-domain",
            new HealthCheckOptions
            {
                Predicate = hc => hc.Tags.Contains("domainservice"),
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });

        app.UseHealthChecksUI(opts => opts.UIPath = "/hc-ui");

The healthcheck UI calls a service with the route /healthchecks-api that returns all of the above healthcheck urls I register. When Lamar is used I receive the following error in my output from the healthchecks-api call. If Lamar is removed from the solution, the healthchecks api and UI work fine.

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:17811/healthchecks-api
Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware:Trace: All hosts are allowed. Microsoft.AspNetCore.Routing.Matching.DfaMatcher:Debug: No candidates found for the request path '/healthchecks-api' Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware:Debug: Request did not match any endpoints Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware:Debug: The request path does not match the path filter Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware:Error: An unhandled exception has occurred while executing the request.

System.InvalidOperationException: Compilation failures!

CS0122: 'HealthChecksDb' is inaccessible due to its protection level

Code:

using System; using System.Threading.Tasks;

namespace Jasper.Generated { // START: HealthChecks_UI_Core_Data_HealthChecksDb_healthChecksDb public class HealthChecks_UI_Core_Data_HealthChecksDb_healthChecksDb : Lamar.IoC.Resolvers.ScopedResolver { private readonly System.Func<object, object> _func_of_object_object;

    public HealthChecks_UI_Core_Data_HealthChecksDb_healthChecksDb(System.Func<object, object> func_of_object_object)
    {
        _func_of_object_object = func_of_object_object;
    }

    public override object Build(Lamar.IoC.Scope scope)
    {
        var options = scope.GetInstance<Microsoft.EntityFrameworkCore.DbContextOptions<HealthChecks.UI.Core.Data.HealthChecksDb>>("options");
        return _func_of_object_object(options);
    }

}

// END: HealthChecks_UI_Core_Data_HealthChecksDb_healthChecksDb

}

at LamarCompiler.AssemblyGenerator.Generate(String code) at LamarCompiler.GeneratedAssembly.CompileAll(IServiceVariableSource services) at Lamar.IoC.Instances.GeneratedInstance.buildResolver(Scope scope) at Lamar.IoC.Instances.GeneratedInstance.ToResolver(Scope topScope) at Lamar.ServiceGraph.FindResolver(Type serviceType) at Lamar.IoC.Scope.TryGetInstance(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at HealthChecks.UI.Middleware.UIApiEndpointMiddleware.InvokeAsync(HttpContext context) at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) Application Insights Telemetry: {"name":"Microsoft.ApplicationInsights.Dev.c63d34963c49429ba2056dad00e4ac45.Exception","time":"2019-03-14T15:39:18.9669103Z","iKey":"c63d3496-3c49-429b-a205-6dad00e4ac45","tags":{"ai.application.ver":"1.0.0.0","ai.cloud.roleInstance":"ALSSTRINK-01","ai.operation.id":"7c3d427096acca469ddc44ea7d3bdf13","ai.operation.parentId":"|7c3d427096acca469ddc44ea7d3bdf13.68b8c6a2_","ai.operation.name":"GET /healthchecks-api","ai.location.ip":"::1","ai.internal.sdkVersion":"aspnet5c:2.6.1","ai.internal.nodeName":"ALSSTRINK-01"},"data":{"baseType":"ExceptionData","baseData":{"ver":2,"exceptions":[{"id":7657598,"outerId":0,"typeName":"System.InvalidOperationException","message":"Compilation failures!\n\nCS0122: 'HealthChecksDb' is inaccessible due to its protection level\n\nCode:\n\nusing System;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace Jasper.Generated\r\n{\r\n // START: HealthChecks_UI_Core_Data_HealthChecksDb_healthChecksDb\r\n public class HealthChecks_UI_Core_Data_HealthChecksDb_healthChecksDb : Lamar.IoC.Resolvers.ScopedResolver\r\n {\r\n private readonly System.Func<object, object> _func_of_object_object;\r\n\r\n public HealthChecks_UI_Core_Data_HealthChecksDb_healthChecksDb(System.Func<object, object> func_of_object_object)\r\n {\r\n _func_of_object_object = func_of_object_object;\r\n }\r\n\r\n\r\n\r\n public override object Build(Lamar.IoC.Scope scope)\r\n {\r\n var options = scope.GetInstance<Microsoft.EntityFrameworkCore.DbContextOptions>(\"options\");\r\n return _func_of_object_object(options);\r\n }\r\n\r\n }\r\n\r\n // END: HealthChecks_UI_Core_Data_HealthChecksDb_healthChecksDb\r\n \r\n \r\n}\r\n\r\n","hasFullStack":true,"parsedStack":[{"level":0,"method":"LamarCompiler.AssemblyGenerator.Generate","assembly":"LamarCompiler, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","line":0},{"level":1,"method":"LamarCompiler.GeneratedAssembly.CompileAll","assembly":"LamarCompiler, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","line":0},{"level":2,"method":"Lamar.IoC.Instances.GeneratedInstance.buildResolver","assembly":"Lamar, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","line":0},{"level":3,"method":"Lamar.IoC.Instances.GeneratedInstance.ToResolver","assembly":"Lamar, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","line":0},{"level":4,"method":"Lamar.ServiceGraph.FindResolver","assembly":"Lamar, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","line":0},{"level":5,"method":"Lamar.IoC.Scope.TryGetInstance","assembly":"Lamar, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null","line":0},{"level":6,"method":"Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService","assembly":"Microsoft.Extensions.DependencyInjection.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":7,"method":"HealthChecks.UI.Middleware.UIApiEndpointMiddleware+d4.MoveNext","assembly":"HealthChecks.UI, Version=2.2.19.0, Culture=neutral, PublicKeyToken=null","line":0},{"level":8,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":9,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":10,"method":"Microsoft.AspNetCore.Builder.Extensions.MapMiddleware+d3.MoveNext","assembly":"Microsoft.AspNetCore.Http.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":11,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":12,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":13,"method":"Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware+d3.MoveNext","assembly":"Microsoft.AspNetCore.Http.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":14,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":15,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":16,"method":"Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware+d3.MoveNext","assembly":"Microsoft.AspNetCore.Http.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":17,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":18,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":19,"method":"Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware+d3.MoveNext","assembly":"Microsoft.AspNetCore.Http.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":20,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":21,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":22,"method":"Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware+d3.MoveNext","assembly":"Microsoft.AspNetCore.Http.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":23,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":24,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":25,"method":"Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware+d7.MoveNext","assembly":"Microsoft.AspNetCore.StaticFiles, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":26,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":27,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":28,"method":"Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware+d5.MoveNext","assembly":"Swashbuckle.AspNetCore.SwaggerUI, Version=4.0.1.0, Culture=neutral, PublicKeyToken=4232c99127b3c254","line":0},{"level":29,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":30,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":31,"method":"Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware+d6.MoveNext","assembly":"Swashbuckle.AspNetCore.Swagger, Version=4.0.1.0, Culture=neutral, PublicKeyToken=62657d7474907593","line":0},{"level":32,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":33,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":34,"method":"Microsoft.AspNetCore.Routing.EndpointMiddleware+d3.MoveNext","assembly":"Microsoft.AspNetCore.Routing, Version=2.2.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":35,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":36,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":37,"method":"Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware+d6.MoveNext","assembly":"Microsoft.AspNetCore.Routing, Version=2.2.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":38,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":39,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":40,"method":"Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware+d5.MoveNext","assembly":"Microsoft.AspNetCore.Localization, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0},{"level":41,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":42,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","line":0},{"level":43,"method":"Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+d_7.MoveNext","assembly":"Microsoft.AspNetCore.Diagnostics, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60","line":0}]}],"properties":{"DeveloperMode":"true","handledAt":"Platform","AspNetCoreEnvironment":"Development"}}}} Microsoft.AspNetCore.Server.Kestrel:Debug: Connection id "0HLL8ICFOLSN6" completed keep alive response. Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 424.1822ms 500 text/html; charset=utf-8 Application Insights Telemetry: {"name":"Microsoft.ApplicationInsights.Dev.c63d34963c49429ba2056dad00e4ac45.Request","time":"2019-03-14T15:39:18.6107582Z","iKey":"c63d3496-3c49-429b-a205-6dad00e4ac45","tags":{"ai.application.ver":"1.0.0.0","ai.cloud.roleInstance":"ALSSTRINK-01","ai.operation.id":"7c3d427096acca469ddc44ea7d3bdf13","ai.operation.name":"GET /healthchecks-api","ai.location.ip":"::1","ai.internal.sdkVersion":"aspnet5c:2.6.1","ai.internal.nodeName":"ALSSTRINK-01"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"|7c3d427096acca469ddc44ea7d3bdf13.68b8c6a2","name":"GET /healthchecks-api","duration":"00:00:00.4296757","success":false,"responseCode":"500","url":"http://localhost:17811/healthchecks-api","properties":{"_MS.ProcessedByMetricExtractors":"(Name:'Requests', Ver:'1.1')","DeveloperMode":"true","AspNetCoreEnvironment":"Development"}}}} The thread 0x5eac has exited with code 0 (0x0).

jeremydmiller commented 5 years ago

I hate the aspnet core team is my first thought here. This isn't going to get fixed quickly if you need to switch IoC tools. Lamar's basic model doesn't deal well with internal types and I honestly didn't realize how prevalent the usage of internal types in IoC containers was before I built Lamar.