0

I am using Autofac and mediatr. The flow is a message coming from EventHub and I process it and send it to respective command handlers. I can inject few things from the api projects perspective, using the builder.

builder.Register(ctx => {
   var userInfo = ctx.Resolve<UserContextProvider>().GetUserInformation();
   var connectionString = ResolveConnectionString(userInfo);
   return new Repository(connectionString);
}).As<IRepository>();

As the messages are comming from event hub, I need to resolve those parameters dynamically. I have injected the IComponentContext into my message processor class and am trying to resolve the parameters.

_componentContext.Resolve<ActivityCommandHandler>(new NamedParameter("conStr", ConnectionString));

When I put breakpoint on the handler constructor I can see the conStr when the above line is executed.

Is this the correct way of doing it ?

When I send the command using _mediatr.send(mycommand) I am getting the below exception.

None of the constructors found with Autofac.Core.Activators.Reflection.DefaultConstructorFinder on type CommandHandlers.ActivityCommandHandler can be invoked with the available services and parameters: Cannot resolve parameter System.String conStr of constructor Void .ctor(AutoMapper.IMapper, MediatR.IMediator, System.String)

HariHaran
  • 3,642
  • 2
  • 16
  • 33
  • It's quite unclear to me. How do you register `ActivityCommandHandler` ? how the `conStr`. Do you want to propagate the `conStr` parameter to the whole dependency graph ? – Cyril Durand Jun 06 '19 at 11:36
  • Hi @CyrilDurand thanks for the reply. I have already registered all the `IrequestHander<,>` types – HariHaran Jun 06 '19 at 11:52
  • OK but how did you register them ? How *Autofac* will know how to provide the `conStr` parameter ? – Cyril Durand Jun 06 '19 at 12:09
  • @CyrilDurand i did register them based on wiki from https://github.com/jbogard/MediatR/wiki – HariHaran Jun 06 '19 at 12:17
  • How Autofac will know how to provide the `conStr` parameter when building the `ActivityCommandHandler` ? – Cyril Durand Jun 06 '19 at 12:25
  • @CyrilDurand sorry thats what im resolving using the `_componentContext` i mistyped in the question before – HariHaran Jun 06 '19 at 12:42

1 Answers1

0

My suggestion is not to use named-parameters.

Wrap information in an object and register the object itself.

Possible object structure:

public class ConnectionConfiguration 
{
   public string ConnectionString { get; }

   public ConnectionConfiguration(string connectionString) => ConnectionString = connectionString;
}
builder.Register(_ => new ConnectionConfiguration(ConnectionString));

Now inject that in your handler. No extra registration required which also doesn't work because the handlers are already registered to be resolved as open-generics as can be seen here. The ServiceFactory registration takes care of creating those handlers.

FYI: I created a nuget-package for registering MediatR with Autofac that even can be used together with default-service-provider.

alsami
  • 8,996
  • 3
  • 25
  • 36