Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Container InjectionConstructor - Cannot convert lambda expression to type 'object[]' because it is not a delegate type

Does anyone knows how to workaround the fact Unity Container InjectionConstructor does not have any overload for Func<string>?

this.unityContainer
  .RegisterType<IService1Client, Service1Client>()
  .Configure<InjectedMembers>()
  .ConfigureInjectionFor<Service1Client>(
    new InjectionConstructor(() => 
      this.unityContainer.Resolve<User>()
        .SelectedDepartment
        .ApplicationServerUrl
        .ToString()));

Cheers,

like image 944
guercheLE Avatar asked Feb 27 '23 19:02

guercheLE


2 Answers

You could use the InjectionFactory.

this.unityContainer.RegisterType<IService1Client>(
  new InjectionFactory((ctr, type, name) =>
  {
    User user = this.unityContainer.Resolve<User>();
    string url = user.SelectedDepartment.ApplicationServerUrl.ToString();
    return new Service1Client(url);
  }));
like image 110
joelnet Avatar answered Mar 04 '23 01:03

joelnet


Think I found out the answer myself:

Func<string> GetApplicationServerUrl = () => {
  return this.unityContainer.Resolve<User>()
    .SelectedDepartment
    .ApplicationServerUrl
    .ToString(); 
};

this.unityContainer.RegisterType<IService1Client, Service1Client>()
  .Configure<InjectedMembers>()
  .ConfigureInjectionFor<Service1Client>(
    new InjectionConstructor(GetApplicationServerUrl()));
like image 26
guercheLE Avatar answered Mar 04 '23 01:03

guercheLE