AutoMapper / AutoMapper.Extensions.Microsoft.DependencyInjection

MIT License
258 stars 79 forks source link

Dependency Injection fails with Net Core 3.0 HostBuilder #119

Closed KseinyaR-STCU closed 4 years ago

KseinyaR-STCU commented 4 years ago

NetCore is moving from the old WebHostBuilder to a new HostBuilder. In the Program.cs that now looks like this:

      public static IHostBuilder CreateHostBuilder(string[] args) =>
              Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
                  { webBuilder.UseStartup<Startup>(); });

This breaks all the services.AddAutoMapper usages we have.

Not sure if this is an issue within AutoMapper itself or the NetCore HostBuilder, but considering they are deprecating their old WebHostBuilder (which works fine), this will need to be fixed on some side.

The error thrown is:


System.AggregateException
  HResult=0x80131500
  Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: WebApplication1.BooleanValueResolver Lifetime: Transient ImplementationType: WebApplication1.BooleanValueResolver': Unable to resolve service for type 'System.String' while attempting to activate 'WebApplication1.BooleanValueResolver'.)
  Source=Microsoft.Extensions.DependencyInjection
  StackTrace:
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
   at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter1.CreateServiceProvider(Object containerBuilder)
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at WebApplication1.Program.Main(String[] args) in C:\Users\me\source\repos\WebApplication1\WebApplication1\Program.cs:line 16

Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: WebApplication1.BooleanValueResolver Lifetime: Transient ImplementationType: WebApplication1.BooleanValueResolver': Unable to resolve service for type 'System.String' while attempting to activate 'WebApplication1.BooleanValueResolver'.

Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate 'WebApplication1.BooleanValueResolver'.
KseinyaR-STCU commented 4 years ago

You can repro this with a new empty Net Core 3.0 Web Application. Adding a reference to AutoMapper.Extensions.Microsoft.DependencyInjection, any resolver (I have a basic boolean one), and a services.AddAutoMapper in the Startup.cs recreates the error.

lbargaoanu commented 4 years ago

I think this is better suited for StackOverflow.

KseinyaR-STCU commented 4 years ago

@lbargaoanu How is this a StackOverflow question? I'm not asking how to do something.

Automapper works great in Net Core 2. Updating to Net Core 3, without any other changes at all, breaks Automapper. Unless you only support Net Core 2, this seems like an issue.

I narrowed the error down on resolvers that use DataRow.

    public class BooleanValueFromDataRowResolver : IValueResolver<DataRow, object, bool>
    {
        private readonly string initValue;

        public BooleanValueFromDataRowResolver(string initValue) => this.initValue = initValue;

        public bool Resolve(DataRow source, object destination, bool destMember, ResolutionContext context) =>
            ((string)source[initValue])?.Equals("Y") ?? false;
    }
jbogard commented 4 years ago

Why is your resolver accepting a string as its constructor? That means it needs a string dependency.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.