C# 属性注入不工作,请帮助。团结一致
我花了几个小时试图解决这个问题,但没有成功。我使用unity将我的_translation属性注入到我的SSPresourceManagerRapper类中,该类返回null,而不是translationService对象 当应用程序开始注册所有依赖项时,将调用注册依赖项UIbootstrapper类。并在创建控制器对象时解析C# 属性注入不工作,请帮助。团结一致,c#,asp.net-mvc,unity-container,C#,Asp.net Mvc,Unity Container,我花了几个小时试图解决这个问题,但没有成功。我使用unity将我的_translation属性注入到我的SSPresourceManagerRapper类中,该类返回null,而不是translationService对象 当应用程序开始注册所有依赖项时,将调用注册依赖项UIbootstrapper类。并在创建控制器对象时解析 public class UiBootstrapper { public static IUnityContainer RegisterUiComponents
public class UiBootstrapper
{
public static IUnityContainer RegisterUiComponents(IUnityContainer container)
{
container.RegisterType<SSPResourceManagerWrapper>();
container.RegisterType<SSPResourceManager>();
container.RegisterType<ITranslationService, TranslationService>();
return container;
}
使用SSPResourceManager的位置
public abstract class AddressVM
{
[Required(ErrorMessageResourceType = typeof(SSPResourceManager), ErrorMessageResourceName
= "SSP_Validation_Required")]
[StringLength(50)]
public string Name { get; set; }
}
您正在创建自己的SSPresourceManagerRapper实例,而不是使用Unity容器解析它:
private static ITranslationService _translationService =
new SSPResourceManagerWrapper()._translation;
容器从来没有机会提供依赖项。尝试使用解析:
或者更好:
private static ITranslationService _translationService =
container.Resolve<ITranslationService>();
一直都是Trinity,救命啊!没有帮助,Unity B-我无权访问SSPresourceManager类中的容器对象。容器对象在global.asax中使用。集中解决。我试着用constructor注入来代替。正在将ITranslationService传递给SSPResourceManager,。我将更新帖子问题是的,这是一个困难的地方。您正在尝试从容器初始化全局变量,为此,您需要对容器进行全局变量访问。IoC容器在解析非全局依赖关系图时工作得最好。如果不想公开容器本身,则可以尝试使用:EnterpriseLibraryContainer.Current.GetInstance
private static ITranslationService _translationService =
container.Resolve<SSPResourceManagerWrapper>()._translation;
private static ITranslationService _translationService =
container.Resolve<ITranslationService>();