Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate _logger field in generated handler code #226

Closed
agross opened this issue Mar 7, 2023 · 3 comments
Closed

Duplicate _logger field in generated handler code #226

agross opened this issue Mar 7, 2023 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@agross
Copy link

agross commented Mar 7, 2023

I have a case where generated code tries to compile a class with 2 fields named _logger. It's related to using a custom policy where Infrastructure.Authorization.AuthorizationPolicy.EnforcePolicy has an ILogger parameter and the upgrade of Wolverine:

-    WolverineFx (0.9.12)
+    WolverineFx (0.9.13)
       FastExpressionCompiler (>= 3.3.4)
-      JasperFx.CodeGeneration.Commands (>= 1.1)
-      JasperFx.Core (>= 1.1)
-      JasperFx.RuntimeCompiler (>= 1.1)
-      Lamar.Microsoft.DependencyInjection (>= 10.0.2)
+      JasperFx.CodeGeneration.Commands (>= 2.0)
+      JasperFx.Core (>= 1.2)
+      JasperFx.RuntimeCompiler (>= 2.0)
+      Lamar.Microsoft.DependencyInjection (>= 11.0)

This is the generated code:

Failed to create a message handler for Application.UseCases.Reservierung.Reservieren.ArbeitsplatzReservieren
System.InvalidOperationException: Compilation failures!

CS0100: The parameter name 'logger' is a duplicate
CS0102: The type 'ArbeitsplatzReservierenHandler1260337258' already contains a definition for '_logger'
CS0229: Ambiguity between 'ArbeitsplatzReservierenHandler1260337258._logger' and 'ArbeitsplatzReservierenHandler1260337258._logger'
CS0229: Ambiguity between 'ILogger<ArbeitsplatzReservieren> logger' and 'ILogger<DefaultAuthorizationService> logger'
CS0229: Ambiguity between 'ArbeitsplatzReservierenHandler1260337258._logger' and 'ArbeitsplatzReservierenHandler1260337258._logger'
CS0229: Ambiguity between 'ILogger<ArbeitsplatzReservieren> logger' and 'ILogger<DefaultAuthorizationService> logger'
CS0229: Ambiguity between 'ArbeitsplatzReservierenHandler1260337258._logger' and 'ArbeitsplatzReservierenHandler1260337258._logger'
CS0229: Ambiguity between 'ArbeitsplatzReservierenHandler1260337258._logger' and 'ArbeitsplatzReservierenHandler1260337258._logger'

Code:

// <auto-generated/>
#pragma warning disable
using Application.UseCases.Reservierung.Reservieren.Regeln;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Wolverine.Marten.Publishing;

namespace Internal.Generated.WolverineHandlers
{
    // START: ArbeitsplatzReservierenHandler1260337258
    public class ArbeitsplatzReservierenHandler1260337258 : Wolverine.Runtime.Handlers.MessageHandler
    {
        private readonly Wolverine.Marten.Publishing.OutboxedSessionFactory _outboxedSessionFactory;
        private readonly Application.UseCases.Reservierung.Reservieren.Regeln.IPolicyRule _policyRule1845047320;
        private readonly Microsoft.AspNetCore.Authorization.IAuthorizationHandler _authorizationHandler53398325;
        private readonly Microsoft.AspNetCore.Http.IHttpContextAccessor _httpContextAccessor;
        private readonly Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Authorization.AuthorizationOptions> _options2;
        private readonly Microsoft.AspNetCore.Authorization.IAuthorizationPolicyProvider _authorizationPolicyProvider1934092963;
        private readonly Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Authorization.AuthorizationOptions> _options1;
        private readonly Microsoft.AspNetCore.Authorization.IAuthorizationHandler _authorizationHandler_53449486;
        private readonly Microsoft.Extensions.Logging.ILogger<Application.UseCases.Reservierung.Reservieren.ArbeitsplatzReservieren> _logger;
        private readonly Microsoft.AspNetCore.Authorization.IAuthorizationHandler _authorizationHandler_1541787964;
        private readonly Application.UseCases.Reservierung.Reservieren.Regeln.IPolicyRule _policyRule2086865308;
        private readonly Microsoft.AspNetCore.Authorization.IAuthorizationHandler _authorizationHandler184019271;
        private readonly Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Authorization.DefaultAuthorizationService> _logger;

        public ArbeitsplatzReservierenHandler1260337258(Wolverine.Marten.Publishing.OutboxedSessionFactory outboxedSessionFactory, [Lamar.Named("ohneBestehendeReservierung")] Application.UseCases.Reservierung.Reservieren.Regeln.IPolicyRule policyRule1845047320, [Lamar.Named("resourceAccessRequirementHandler")] Microsoft.AspNetCore.Authorization.IAuthorizationHandler authorizationHandler53398325, Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Authorization.AuthorizationOptions> __options2, [Lamar.Named("protectedResourcePolicyProvider")] Microsoft.AspNetCore.Authorization.IAuthorizationPolicyProvider authorizationPolicyProvider1934092963, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Authorization.AuthorizationOptions> __options1, [Lamar.Named("decisionRequirementHandler")] Microsoft.AspNetCore.Authorization.IAuthorizationHandler authorizationHandler_53449486, Microsoft.Extensions.Logging.ILogger<Application.UseCases.Reservierung.Reservieren.ArbeitsplatzReservieren> logger, [Lamar.Named("realmAccessRequirementHandler")] Microsoft.AspNetCore.Authorization.IAuthorizationHandler authorizationHandler_1541787964, [Lamar.Named("imZeitraumReservierbar")] Application.UseCases.Reservierung.Reservieren.Regeln.IPolicyRule policyRule2086865308, [Lamar.Named("rptRequirementHandler")] Microsoft.AspNetCore.Authorization.IAuthorizationHandler authorizationHandler184019271, Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Authorization.DefaultAuthorizationService> logger)
        {
            _outboxedSessionFactory = outboxedSessionFactory;
            _policyRule1845047320 = policyRule1845047320;
            _authorizationHandler53398325 = authorizationHandler53398325;
            _httpContextAccessor = httpContextAccessor;
            _options2 = __options2;
            _authorizationPolicyProvider1934092963 = authorizationPolicyProvider1934092963;
            _options1 = __options1;
            _authorizationHandler_53449486 = authorizationHandler_53449486;
            _logger = logger;
            _authorizationHandler_1541787964 = authorizationHandler_1541787964;
            _policyRule2086865308 = policyRule2086865308;
            _authorizationHandler184019271 = authorizationHandler184019271;
            _logger = logger;
        }



        public override async System.Threading.Tasks.Task HandleAsync(Wolverine.Runtime.MessageContext context, System.Threading.CancellationToken cancellation)
        {
            var passThroughAuthorizationHandler = new Microsoft.AspNetCore.Authorization.Infrastructure.PassThroughAuthorizationHandler(_options1);
            var authorizationHandlerList = new System.Collections.Generic.List<Microsoft.AspNetCore.Authorization.IAuthorizationHandler>{passThroughAuthorizationHandler, _authorizationHandler_53449486, _authorizationHandler184019271, _authorizationHandler_1541787964, _authorizationHandler53398325};
            var defaultAuthorizationHandlerProvider = new Microsoft.AspNetCore.Authorization.DefaultAuthorizationHandlerProvider(authorizationHandlerList);
            var defaultAuthorizationHandlerContextFactory = new Microsoft.AspNetCore.Authorization.DefaultAuthorizationHandlerContextFactory();
            var defaultAuthorizationEvaluator = new Microsoft.AspNetCore.Authorization.DefaultAuthorizationEvaluator();
            var defaultAuthorizationService = new Microsoft.AspNetCore.Authorization.DefaultAuthorizationService(_authorizationPolicyProvider1934092963, defaultAuthorizationHandlerProvider, _logger, defaultAuthorizationHandlerContextFactory, defaultAuthorizationEvaluator, _options2);
            var currentUserService = new Infrastructure.Authorization.CurrentUserService(_httpContextAccessor);
            var identityService = new Infrastructure.Authorization.IdentityService(defaultAuthorizationService, currentUserService);
            var policyRuleArray = new Application.UseCases.Reservierung.Reservieren.Regeln.IPolicyRule[]{_policyRule2086865308, _policyRule1845047320};
            var policyEvaluator = new Application.UseCases.Reservierung.Reservieren.PolicyEvaluator(policyRuleArray);
            var arbeitsplatzReservieren = (Application.UseCases.Reservierung.Reservieren.ArbeitsplatzReservieren)context.Envelope.Message;
            await using var documentSession = _outboxedSessionFactory.OpenSession(context);
            await Infrastructure.Authorization.AuthorizationPolicy.EnforcePolicy<Application.UseCases.Reservierung.Reservieren.ArbeitsplatzReservieren>(identityService, ((Microsoft.Extensions.Logging.ILogger)_logger), arbeitsplatzReservieren).ConfigureAwait(false);
            (var arbeitsplatz, var eventStream) = await Application.UseCases.Reservierung.Reservieren.ReservierenHandler.LoadAsync(arbeitsplatzReservieren, documentSession, cancellation).ConfigureAwait(false);
            var outgoing1 = Application.UseCases.Reservierung.Reservieren.ReservierenHandler.Handle(arbeitsplatzReservieren, arbeitsplatz, eventStream, currentUserService, policyEvaluator, documentSession);
            // Outgoing, cascaded message
            await context.EnqueueCascadingAsync(outgoing1).ConfigureAwait(false);


            // Commit the unit of work
            await documentSession.SaveChangesAsync(cancellation).ConfigureAwait(false);
        }

    }

    // END: ArbeitsplatzReservierenHandler1260337258
    
    
}


    at JasperFx.RuntimeCompiler.AssemblyGenerator.Generate(String code)
    at JasperFx.RuntimeCompiler.AssemblyGenerator.Compile(GeneratedAssembly generatedAssembly, IServiceVariableSource services)
    at JasperFx.RuntimeCompiler.CodeFileExtensions.InitializeSynchronously(ICodeFile file, GenerationRules rules, ICodeFileCollection parent, IServiceProvider services)
    at Wolverine.Runtime.Handlers.HandlerGraph.HandlerFor(Type messageType)
    at Wolverine.Runtime.WolverineRuntime.Wolverine.Runtime.IExecutorFactory.BuildFor(Type messageType)
    at Wolverine.Runtime.WolverineRuntime.findInvoker(Type messageType)
@agross
Copy link
Author

agross commented Mar 8, 2023

using IntegrationTests;

using JasperFx.CodeGeneration;
using JasperFx.CodeGeneration.Frames;

using Lamar;

using Marten;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using Wolverine;
using Wolverine.Configuration;
using Wolverine.Marten;
using Wolverine.Runtime.Handlers;
using Wolverine.Tracking;

using Xunit;

namespace PersistenceTests.Marten.Bugs;

public class middleware_deps : PostgresqlContext
{
  [Fact]
  public async Task should_find_handler()
  {
    using var host = await Host.CreateDefaultBuilder()
                               .ConfigureServices(services =>
                               {
                                 services.AddLogging();
                                 services.AddScoped<IDependencyRequiringLogger, DependencyRequiringLogger>();

                                 services.AddMarten(connectionString: Servers.PostgresConnectionString)
                                         .IntegrateWithWolverine();
                               })
                               .UseWolverine(opts =>
                               {
                                 opts.Policies.Add<RequiringLoggerPolicy>();
                               })
                               .StartAsync();

    var id = Guid.NewGuid();

    await host.InvokeMessageAndWaitAsync(new StoreSomething(Id: id));
  }

  public interface IDependencyRequiringLogger
  {
  }

  public class DependencyRequiringLogger : IDependencyRequiringLogger
  {
    public DependencyRequiringLogger(ILogger<DependencyRequiringLogger> logger)
    {
    }
  }

  public class RequiringLoggerPolicy : IHandlerPolicy
  {
    public void Apply(IReadOnlyList<HandlerChain> chains, GenerationRules rules, IContainer container)
    {
      foreach (var chain in chains)
      {
        var method = GetType().GetMethod(nameof(SamplePolicy))!;

        var methodCall = new MethodCall(GetType(), method);

        chain.Middleware.Add(methodCall);
      }
    }

    public static Task SamplePolicy(ILogger logger, IDependencyRequiringLogger dep)
      => Task.CompletedTask;
  }
}

public record StoreSomething(Guid Id);

public class StoreSomethingHandler
{
  public static void Handle(StoreSomething command)
  {
  }
}

@jeremydmiller
Copy link
Member

That one's an easy fix (I think). I'll get that turned around soon. Thanks for all the feedback @agross !

@agross
Copy link
Author

agross commented Mar 13, 2023

The same happens if you return two instances of IEventStream<T> from LoadAsync (composite handler).

(var myDomainType, var eventStream, var eventStream) = await SomeHandler.LoadAsync(...)

Another thing you cannot return is a Context as that variable name is taken by Wolverine.Runtime.MessageContext.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants