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

Unhandled exception. System.TypeLoadException: Could not load type 'FluentMigrator.Runner.Constraints.MigrationConstraintAttribute' #1785

Open
pseudosma opened this issue Apr 27, 2024 · 0 comments

Comments

@pseudosma
Copy link

pseudosma commented Apr 27, 2024

The Bug

I introduced a MigrationConstraintAttribute to my code for a single migration, and everything worked fine locally until I pushed the code up to my remote repo and the CI pipeline kicked in to update a Docker DB for unit tests. My scripted migration failed and I saw System.TypeLoadException: Could not load type 'FluentMigrator.Runner.Constraints.MigrationConstraintAttribute' in the logs.

The migration was triggered via the FluentMigrator.DotNet.Cli in a script that prepares a DB for unit tests. My CI is running Ubuntu 22.04 where I am running Windows 11 locally, but the other key difference was that it downloading the latest version of FluentMigrator.DotNet.Cli (5.2.0) where I did the local development with version 3.3.2.

As it turns out, this problem seems to be related to the latest version of FluentMigrator.DotNet.Cli, and not the OS I'm using. If I force the CI to use version 3.3.2 like I started with, it no longer has the error. Furthermore, upgrading locally in Windows 11 to version 5.2.0 of the CLI tool reproduces the error on my maching.

I haven't found anything in the docs or changelog that indicates a breaking change occurred with MigrationConstraintAttributes.

To Reproduce

  • Have a migration that conditionally runs via a MigrationConstraintAttribute as part of a C# project
  • Run the migrations from FluentMigrator.DotNet.Cli (version 5.2.0) using a command like this:
    • dotnet fm migrate -p SqlServer2016 -c "Server=tcp:localhost,1455;Initial Catalog=myDatabase;Persist Security Info=False;User ID=sa;Password=******;MultipleActiveResultSets=False;Encrypt=False;TrustServerCertificate=False;Connection Timeout=30;" -a ./MyProject/bin/Debug/net8.0/MyProject.dll --allowDirtyAssemblies
    • Note that the allowDirtyAssemblies param was added while trying to debug the issue. It doesn't seem to make any difference; the only solution for me was downgrading.

Expected behavior

Expected version 5.2.0 of FluentMigrator.DotNet.Cli to behave like version 3.3.2 when running a migration with a MigrationConstraintAttribute.

Information

  • OS: This has occurred in Windows 11 and an Ubuntu 22.04 image in an Azure DevOps build agent
  • Platform: .Net 8.0
  • FluentMigrator version: 5.2.0
  • FluentMigrator runner: FluentMigrator.DotNet.Cli
  • Database Management System: MSSQLServer
  • Database Management System Version: MSSQLServer 2019, specifically the mcr.microsoft.com/mssql/server:2019-latest image running in Docker

Additional context

Here's a stack trace from the build agent:

2024-04-26T23:30:56.2741646Z Unhandled exception. System.TypeLoadException: Could not load type 'FluentMigrator.Runner.Constraints.MigrationConstraintAttribute' from assembly 'FluentMigrator.Runner, Version=5.2.0.0, Culture=neutral, PublicKeyToken=aacfc7de5acabf05'.
2024-04-26T23:30:56.2742433Z    at System.Reflection.RuntimeAssembly.GetExportedTypes()
2024-04-26T23:30:56.2743097Z    at FluentMigrator.Runner.Initialization.AssemblySourceMigrationRunnerConventionsAccessor.<>c.<.ctor>b__1_1(Assembly a) in D:\a\1\s\src\FluentMigrator.Runner.Core\Initialization\AssemblySourceMigrationRunnerConventionsAccessor.cs:line 50
2024-04-26T23:30:56.2743665Z    at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
2024-04-26T23:30:56.2744079Z    at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
2024-04-26T23:30:56.2744509Z    at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
2024-04-26T23:30:56.2745116Z    at FluentMigrator.Runner.Initialization.AssemblySourceMigrationRunnerConventionsAccessor.<>c__DisplayClass1_0.<.ctor>b__0() in D:\a\1\s\src\FluentMigrator.Runner.Core\Initialization\AssemblySourceMigrationRunnerConventionsAccessor.cs:line 50
2024-04-26T23:30:56.2745694Z    at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
2024-04-26T23:30:56.2746147Z    at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
2024-04-26T23:30:56.2746496Z    at System.Lazy`1.CreateValue()
2024-04-26T23:30:56.2746970Z    at FluentMigrator.Runner.Initialization.AssemblySourceMigrationRunnerConventionsAccessor.get_MigrationRunnerConventions() in D:\a\1\s\src\FluentMigrator.Runner.Core\Initialization\AssemblySourceMigrationRunnerConventionsAccessor.cs:line 65
2024-04-26T23:30:56.2747729Z    at Microsoft.Extensions.DependencyInjection.FluentMigratorServiceCollectionExtensions.<>c.<AddFluentMigratorCore>b__0_1(IServiceProvider sp) in D:\a\1\s\src\FluentMigrator.Runner.Core\FluentMigratorServiceCollectionExtensions.cs:line 83
2024-04-26T23:30:56.2748371Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2749032Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2749560Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2750062Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2750606Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2751579Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2752212Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
2024-04-26T23:30:56.2752865Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2753395Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2753917Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
2024-04-26T23:30:56.2754376Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
2024-04-26T23:30:56.2760722Z    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
2024-04-26T23:30:56.2761329Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
2024-04-26T23:30:56.2761816Z    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
2024-04-26T23:30:56.2762537Z    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
2024-04-26T23:30:56.2763223Z    at Microsoft.Extensions.DependencyInjection.FluentMigratorServiceCollectionExtensions.<>c.<AddFluentMigratorCore>b__0_2(IServiceProvider sp) in D:\a\1\s\src\FluentMigrator.Runner.Core\FluentMigratorServiceCollectionExtensions.cs:line 92
2024-04-26T23:30:56.2763911Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2765465Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2766986Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
2024-04-26T23:30:56.2767527Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2767938Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2768388Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2768845Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2769337Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
2024-04-26T23:30:56.2769885Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2770308Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2770739Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2771189Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2771693Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
2024-04-26T23:30:56.2772196Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2772622Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2773243Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2773763Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2774267Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
2024-04-26T23:30:56.2774789Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
2024-04-26T23:30:56.2775209Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
2024-04-26T23:30:56.2775627Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
2024-04-26T23:30:56.2776080Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
2024-04-26T23:30:56.2776540Z    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
2024-04-26T23:30:56.2776950Z    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
2024-04-26T23:30:56.2777325Z    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
2024-04-26T23:30:56.2777721Z    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
2024-04-26T23:30:56.2778171Z    at FluentMigrator.Runner.Initialization.TaskExecutor.RunnerScope..ctor(TaskExecutor executor) in D:\a\1\s\src\FluentMigrator.Runner.Core\Initialization\TaskExecutor.cs:line 257
2024-04-26T23:30:56.2778680Z    at FluentMigrator.Runner.Initialization.TaskExecutor.Execute() in D:\a\1\s\src\FluentMigrator.Runner.Core\Initialization\TaskExecutor.cs:line 161
2024-04-26T23:30:56.2779184Z    at FluentMigrator.DotNet.Cli.Commands.BaseCommand.ExecuteMigrations(MigratorOptions options, IConsole console) in D:\a\1\s\src\FluentMigrator.DotNet.Cli\Commands\BaseCommand.cs:line 30
2024-04-26T23:30:56.2779661Z    at FluentMigrator.DotNet.Cli.Commands.Migrate.OnExecute(IConsole console) in D:\a\1\s\src\FluentMigrator.DotNet.Cli\Commands\Migrate.cs:line 32
2024-04-26T23:30:56.2780064Z    at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
2024-04-26T23:30:56.2780431Z    at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
2024-04-26T23:30:56.2780916Z --- End of stack trace from previous location ---
2024-04-26T23:30:56.2781243Z    at McMaster.Extensions.CommandLineUtils.Conventions.ExecuteMethodConvention.Invoke(MethodInfo method, Object instance, Object[] arguments)
2024-04-26T23:30:56.2781648Z    at McMaster.Extensions.CommandLineUtils.Conventions.ExecuteMethodConvention.OnExecute(ConventionContext context, CancellationToken cancellationToken)
2024-04-26T23:30:56.2782052Z    at McMaster.Extensions.CommandLineUtils.Conventions.ExecuteMethodConvention.<>c__DisplayClass0_0.<<Apply>b__0>d.MoveNext()
2024-04-26T23:30:56.2782418Z --- End of stack trace from previous location ---
2024-04-26T23:30:56.2782713Z    at McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExecuteAsync(String[] args, CancellationToken cancellationToken)
2024-04-26T23:30:56.2783119Z    at McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExecuteAsync[TApp](CommandLineContext context, CancellationToken cancellationToken)
2024-04-26T23:30:56.2783586Z    at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute[TApp](CommandLineContext context)
2024-04-26T23:30:56.2784001Z    at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute[TApp](IConsole console, String[] args)
2024-04-26T23:30:56.2784332Z    at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute[TApp](String[] args)
2024-04-26T23:30:56.2784691Z    at FluentMigrator.DotNet.Cli.Program.Main(String[] args) in D:\a\1\s\src\FluentMigrator.DotNet.Cli\Program.cs:line 46

And a condensed version of my MigrationConstraintAttribute in C#

using System;
using FluentMigrator.Runner.Constraints;

[AttributeUsage(AttributeTargets.Class)]
public class EnvConstraintAttribute : MigrationConstraintAttribute
{
   public EnvConstraintAttribute (string requiredEnvironment)
        : base(context =>
        {
           string currentEnvironment = Environment.GetEnvironmentVariable("MY_ENVIRONMENT");
           return requiredEnvironment == currentEnvironment;
        })
   {}
}

and how it's used in a migration:

   [Migration(1234567890)]
   [EnvConstraint("prod")]
   public class MyMigration: Migration
   {
      public override void Up()
      .
      .
      .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant