BadImageFormatException for UnitTest with C#

110 views Asked by At

I'm working with this code on C# and at the beginning I couldn't run it because it threw this exception: 'BadImageFormatException ERROR (HRESULT 0x8007000B)'. I resolved it by simply changing: Tools -> Options -> Projects and Solutions -> Web Projects -> Uncheck the 'Use the 64 bit version of IIS Express for web sites and projects'

And after this it works.

But if I try to test my Unit Tests they fail all and retrieve the same exception: 'Error: System.InvalidOperationException: The configuration is invalid. Creating the instance for type IAuthenticator failed. The registered delegate for type IAuthenticator threw an exception. Error (HRESULT 0x8007000B)'

How can I resolve it also for the Unit Test part?

(EDIT) This is the change that I made but which doesn't resolve the issue

Target Platform

(EDIT1) This is the class ApplicationBootstrapper which registers some Interfaces (I delete some registrations which are useless for this question):

public static class ApplicationBootstrapper
    {
        public static void Bootstrap(Container container)
        {

            container.Register<IConnectionStringProvider, ConnectionStringProvider>(Lifestyle.Singleton);
            container.Register<IGeneratoreCodiciNumerici, GeneratoreCodiciNumerici>(Lifestyle.Singleton);
            container.Register<ITimeRounder, DefaultTimeRounder>(Lifestyle.Singleton);

            container.Register<IConfigurazioneImpiantoProvider, ConfigurazioneImpiantoProvider>(Lifestyle.Singleton);

            container.Register<IAuthenticator>(() => AuthenticatorFactory.CreateNew("VIS"), Lifestyle.Singleton);
            container.Register<IUserAuthenticator, UserAuthenticator>(Lifestyle.Singleton);

            container.Register<IUserPreferencesQuery>(() => UserPreferencesQueryFactory.CreateNew("VIS"), Lifestyle.Singleton);
            container.Register<IUserPreferencesCommand>(() => UserPreferencesCommandFactory.CreateNew("VIS"), Lifestyle.Singleton);

            container.Register<IRequestAuthorizationService, RequestAuthorizationService>(Lifestyle.Singleton);

            container.Register<IReceptionProvider, ReceptionProvider>(Lifestyle.Singleton);

            container.Register<IProfiliProvider, ProfiliProvider>(Lifestyle.Singleton);

            container.Register<IAppLogProvider, AppLogProvider>(Lifestyle.Singleton);

            container.Register<IHtmlProvider, HtmlProvider>(Lifestyle.Transient);
            container.Register<IPdfProvider, PdfProvider>(Lifestyle.Transient);
            container.Register<IBlackListProvider, BlackListProvider>();
            container.Register<INotificatore, Notificatore>();
            container.Register<INotificheDal, NotificheDalBulk>();

            container.Collection.Register(typeof(IValidator<>), assemblies);

            container.RegisterSingleton<IMediator, Mediator>();

            container.Register(typeof(IRequestHandler<,>), assemblies);
            container.Register(typeof(IRequestHandler<>), assemblies);
            container.Register(typeof(RequestHandler<,>), assemblies);
            container.Register(typeof(RequestHandler<>), assemblies);

            var notificationHandlerTypes = container.GetTypesToRegister(typeof(INotificationHandler<>), assemblies, new TypesToRegisterOptions
            {
                IncludeGenericTypeDefinitions = true,
                IncludeComposites = false,
            });
            container.Collection.Register(typeof(INotificationHandler<>), notificationHandlerTypes);

            container.Collection.Register(typeof(IPipelineBehavior<,>), new[]
            {
                typeof(RequestPreProcessorBehavior<,>),
                typeof(RequestPostProcessorBehavior<,>),

                typeof(AuthorizationPipelineBehaviour<,>),
                typeof(ErrorLogPipelineBehaviour<,>),
                typeof(ValidationPipelineBehaviour<,>),
                typeof(ShouldRetryTransactionPipelineBehaviour<,>),
                typeof(TransactionPipelineBehaviour<,>),
            });

container.Collection.Register(typeof(IRequestPreProcessor<>), assemblies);

container.Collection.Register(typeof(IRequestPostProcessor<,>), assemblies);

            container.Register(() => new ServiceFactory(container.GetInstance), Lifestyle.Singleton);
        }
    }
1

There are 1 answers

4
durgaprasad On

This may occur if the code being tested is compiled for the x86 platform, but the unit test project is compiled for the x64 platform or vise versa.

Make sure that the target platform of the unit test project is the same as the target platform of the code being tested. You can change the platform of project by right click on project in solution explorer and select Properties. Then, go to the Build tab and select the Platform Target drop-down list.

refer this image

Updated: If it is singleton,only one instance of the service being created for entire lifetime of the application.If the IAuthenticator service is registered with the Transient lifetime scope, then a new instance of the service will be created for each request.The scope in this case is the unit test. When a unit test is run, a new scope is created. This means that a new instance of the IAuthenticator service will be created for each unit test but if you make as singleton scope it will throw error.