Dependency injection issue with Azure Functions, .NET6, and MediatR 12
Asked Answered
E

2

5

Frustrating error I have never seen before and cannot shake it. Using simple Azure HTTP Function in .NET 6 and MediatR 12 and getting the following in Startup.cs for DI of MediatR:

System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'Boolean Microsoft.Extensions.DependencyInjection.ServiceDescriptor.get_IsKeyedService()'.

Source=MediatR

StackTrace:

at MediatR.Registration.ServiceRegistrar.<>c.b__12_0(ServiceDescriptor service) at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Linq.Enumerable.<OfTypeIterator>d__611.MoveNext() at System.Linq.Enumerable.SelectManySingleSelectorIterator2.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Linq.Enumerable.Any[TSource](IEnumerable1 source, Func2 predicate) at MediatR.Registration.ServiceRegistrar.RegisterBehaviorIfImplementationsExist(IServiceCollection services, Type behaviorType, Type subBehaviorType) at MediatR.Registration.ServiceRegistrar.AddRequiredServices(IServiceCollection services, MediatRServiceConfiguration serviceConfiguration) at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.AddMediatR(IServiceCollection services, MediatRServiceConfiguration configuration) at TheMove.Functions.Startup.Configure(IFunctionsHostBuilder builder) in C:\Users\shamu\source\repos\TheMove\TheMove.Functions\Startup.cs:line 25 at Microsoft.Azure.WebJobs.WebJobsBuilderExtensions.ConfigureAndLogUserConfiguredServices(IWebJobsStartup startup, WebJobsBuilderContext context, IWebJobsBuilder builder, ILoggerFactory loggerFactory) at Microsoft.Azure.WebJobs.WebJobsBuilderExtensions.UseWebJobsStartup(IWebJobsBuilder builder, Type startupType, WebJobsBuilderContext context, ILoggerFactory loggerFactory) at Microsoft.Azure.WebJobs.WebJobsBuilderExtensions.UseExternalStartup(IWebJobsBuilder builder, IWebJobsStartupTypeLocator startupTypeLocator, WebJobsBuilderContext context, ILoggerFactory loggerFactory) at Microsoft.Azure.WebJobs.Script.ScriptHostBuilderExtensions.<>c__DisplayClass7_0.b__1(HostBuilderContext context, IWebJobsBuilder webJobsBuilder)

Source Code:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;

[assembly: FunctionsStartup(typeof(TheMove.Functions.Startup))]
namespace TheMove.Functions
{
    public class Startup : FunctionsStartup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public Startup() { }

        public override void Configure(IFunctionsHostBuilder builder)
        {
            if(builder != null)
            {
                var assembly = AppDomain.CurrentDomain.Load("TheMove.Application");
                builder.Services.AddMediatR(cfg =>
                {
                    cfg.RegisterServicesFromAssembly(assembly);
                });
            }
        }
    }
}
Embus answered 17/1, 2024 at 21:2 Comment(0)
R
5

Had the same problem, but downgraded to version 12.1.1 and doing a clean and a rebuild fixed it for me.

Revivalist answered 6/2, 2024 at 9:29 Comment(1)
strange, but this worked for me.Women
S
2

I had to remove MediatR from other projects, after updating to 12.1.1 as A.Grefslie suggested.

Had a test project and another project, which implicitly was adding MediatR and referring to the project with ServiceCollection

Ran into the same issue again with version 12.2.0, downgraded to 12.1.1 and it worked

Sailesh answered 18/3, 2024 at 11:49 Comment(1)
strange, but this worked for me.Women

© 2022 - 2025 — McMap. All rights reserved.