开发者

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>开发者_Go百科;()
  .Configure<InjectedMembers>()
  .ConfigureInjectionFor<Service1Client>(
    new InjectionConstructor(() => 
      this.unityContainer.Resolve<User>()
        .SelectedDepartment
        .ApplicationServerUrl
        .ToString()));

Cheers,


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);
  }));


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()));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜