尝试使用Unity和Prism初始化模块时出现以下错误.该DLL被发现
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
Run Code Online (Sandbox Code Playgroud)
找到dll并找到名称
#region Constructors
public AdminModule(
IUnityContainer container,
IScreenFactoryRegistry screenFactoryRegistry,
IEventAggregator eventAggregator,
IBusyService busyService
)
: base(container, screenFactoryRegistry)
{
this.EventAggregator = eventAggregator;
this.BusyService = busyService;
}
#endregion
#region Properties
protected IEventAggregator EventAggregator { get; set; }
protected IBusyService BusyService { get; set; }
#endregion
public override void Initialize()
{
base.Initialize();
}
#region Register Screen Factories
protected override void RegisterScreenFactories()
{
this.ScreenFactoryRegistry.Register(ScreenKeyType.ApplicationAdmin, typeof(AdminScreenFactory));
}
#endregion
#region Register Views and Various Services
protected override …Run Code Online (Sandbox Code Playgroud) 我正在实施Ioc,我想确保一些正确的事情.
RegisterInstance,在解析它时将始终返回单例对象?但我想知道如何
1.每个解析创建一个单独的实例,PerResolve不能使用RegisterInstance,它只适用于RegisterType.
2.如果我将依赖对象作为静态属性,它将以相同的方式工作,如果我能够为每个解析创建单独的实例?
请帮忙?
public class ClientUser : UserServiceBase, IClientUser
{
private IDataServiceManager _dataServiceManager;
public ClientUser()
{
}
private IDataServiceManager DataServiceMgr
{
get
{
if (_dataServiceManager == null)
_dataServiceManager = ProjectContainer.Instance.Resolve<IDataServiceManager>();
return _dataServiceManager;
}
}
Run Code Online (Sandbox Code Playgroud) 我似乎在这里遇到了障碍,并希望得到任何能够在这一方面得到帮助的人.我不确定下面的错误消息是什么意思.我正在使用企业模式服务的缓存块,但我继续遇到下面的问题.我下载了最新版本,并尝试逐步解决这个问题,但我似乎无法解决确切的问题,我需要帮助.提前致谢
Test method WorldBank.Service.Business.UnitTest.TopicsManagerTest.Call_Children_out_of_schoolTest threw exception:
Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type ICacheManager, key "Cache Manager" ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager", name = "Cache Manager".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager,WuCache
---> System.InvalidOperationException: The current type, Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager, is …Run Code Online (Sandbox Code Playgroud) 我们有一个依赖于的类HttpContext.我们已经实现了这样:
public SiteVariation() : this(new HttpContextWrapper(HttpContext.Current))
{
}
public SiteVariation(HttpContextBase context)
{}
Run Code Online (Sandbox Code Playgroud)
现在我想要做的是实例化SiteVariation类Unity,所以我们可以创建一个构造函数.但我不知道如何HttpContextWrapper(HttpContext.Current))在配置方式中在Unity中配置这个新功能.
ps这是我们使用的配置方式
<type type="Web.SaveRequest.ISaveRequestHelper, Common" mapTo="Web.SaveRequest.SaveRequestHelper, Common" />
Run Code Online (Sandbox Code Playgroud) 目前,我有一个自定义ControllerFactory,我注入我的Unity容器:
在global.asax Application_Start()中:
var container = InitContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
var factory = new UnityControllerFactory(container);
ControllerBuilder.Current.SetControllerFactory(factory);
Run Code Online (Sandbox Code Playgroud)
在控制器工厂中,我将控制器设置为使用自定义ActionInvoker,如下所示:
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
var controller = base.GetControllerInstance(requestContext, controllerType) as Controller;
if (controller != null)
controller.ActionInvoker = new UnityActionInvoker(_container);
return controller;
}
Run Code Online (Sandbox Code Playgroud)
最后在我的自定义ActionInvoker中,我尝试使用ActionInvokers容器构建调用的操作:
protected override ActionExecutedContext InvokeActionMethodWithFilters(
ControllerContext controllerContext,
IList<IActionFilter> filters,
ActionDescriptor actionDescriptor,
IDictionary<string, object> parameters)
{
var builtUpFilters = new List<IActionFilter>();
foreach (IActionFilter actionFilter in filters)
{
builtUpFilters.Add(_container.BuildUp<IActionFilter>(actionFilter));
}
return base.InvokeActionMethodWithFilters(controllerContext, builtUpFilters, actionDescriptor, parameters);
}
Run Code Online (Sandbox Code Playgroud)
以下是正在构建的ActionFilters之一的示例:
public class PopulatRolesAttribute …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
_container = new UnityContainer();
_container.RegisterType<IDownloader, Downloader>();
_container.RegisterType<INewObject, NewObject>();
_container.RegisterType<SearchViewModel>();
Run Code Online (Sandbox Code Playgroud)
SearchViewModel 构造函数注入的类:
class SearchViewModel
{
private readonly Func<IDownloader> _downloaderFactory;
private readonly INewObject _newObject;
private IDownloader _downloader;
public SearchViewModel(Func<IDownloader> downloaderFactory, INewObject newObject)
{
_downloaderFactory = downloaderFactory;
_newObject = newObject;
}
}
Run Code Online (Sandbox Code Playgroud)
问题:如何注册SearchViewModel那个Fun<>参数?
_container.RegisterType<SearchViewModel>(new InjectionConstructor(DownloaderFactory()));
Run Code Online (Sandbox Code Playgroud)
上面的代码只能没有INewObject.
目标:自动解决工厂InjectionConstructor并INewObject, INewObject2, INewObject3自动解决(如无参数:) RegisterType<SearchViewModel>().
可能吗?也许是替补?
使用Unity Application块,当我们UnityContainer.Resolve<T>() 在WCF上下文中调用方法时,如何强制Unity配置创建对象的新实例?
我发现了一些与我发布的问题相似的问题,但我并没有从他们那里得到我真正需要的东西.我仍然在努力实现我的CustomMembershipProvider使用Microsoft Unity DI.
定制会员:
public class CustomMembershipProviderService : MembershipProvider
{
private readonly IUserService _userService;
public CustomMembershipProviderService(IUserService userService)
{
this._userService = userService;
}
public override string ApplicationName
{
...
Run Code Online (Sandbox Code Playgroud)
用户服务:
public class UserService : IUserService
{
private readonly IUserRepository _repository;
private readonly IUnitOfWork _unitOfWork;
public UserService(IUserRepository repository, IUnitOfWork unitOfWork)
{
this._repository = repository;
this._unitOfWork = unitOfWork;
}
...
Run Code Online (Sandbox Code Playgroud)
的AccountController:
public class AccountController : Controller
{
// next line is what I don't feel too sure about what …Run Code Online (Sandbox Code Playgroud) dependency-injection unity-container repository-pattern custom-membershipprovider asp.net-mvc-3
Unity如何获取接口的所有实例然后访问它们?
代码片段取自此处:Fail-Tracker
在StrcutureMap中,可以从程序集中注册所有类型的接口,然后访问它们,如下所示:
public class TaskRegistry : Registry
{
public TaskRegistry()
{
Scan(scan =>
{
scan.AssembliesFromApplicationBaseDirectory(
a => a.FullName.StartsWith("FailTracker"));
scan.AddAllTypesOf<IRunAtInit>();
scan.AddAllTypesOf<IRunAtStartup>();
scan.AddAllTypesOf<IRunOnEachRequest>();
scan.AddAllTypesOf<IRunOnError>();
scan.AddAllTypesOf<IRunAfterEachRequest>();
});
}
}
ObjectFactory.Configure(cfg =>
{
cfg.AddRegistry(new TaskRegistry());
});
Run Code Online (Sandbox Code Playgroud)
然后访问实现这些接口的所有类型,如:
using (var container = ObjectFactory.Container.GetNestedContainer())
{
foreach (var task in container.GetAllInstances<IRunAtInit>())
{
task.Execute();
}
foreach (var task in container.GetAllInstances<IRunAtStartup>())
{
task.Execute();
}
}
Run Code Online (Sandbox Code Playgroud)
这个代码在统一中的等价物是什么?
我如何在Application_BeginRequest中获得这些结构图
public void Application_BeginRequest()
{
Container = ObjectFactory.Container.GetNestedContainer();
foreach (var task in Container.GetAllInstances<IRunOnEachRequest>())
{
task.Execute();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个基类Class Base具有dependecy Dep和default和Injection Constructor-
Class Base : IBase
{
public IDep Dep { get; set; }
public Base()
{
Console.WriteLine("Default Constructor Base ");
}
[InjectionConstructor]
public Base(IDep dep)
{
Console.WriteLine("Injection Constructor Base ");
Dep = dep;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为在解析派生类时,应该自动注入Dependency dep(通过Constructor Injection).
但是,当我从它派生一个类并解析该类时,这似乎不起作用,而是调用Base的默认构造函数.
当我从Derived Class中显式调用构造函数时,我只能使它工作.
class Derived : Base
{
public Derived ()
{
Console.WriteLine("Default Constructor Derived ");
}
public Derived (IDep dep) : base(dep1)
{
Console.WriteLine("Injection Constructor Derived ");
}
}
Run Code Online (Sandbox Code Playgroud)
unity是否提供了任何直接的方法来隐式调用基类的注入构造函数(而不是通过显式的Construtor调用)?如果没有,是否有任何理由为什么统一容器本身不做?
c# dependency-injection unity-container constructor-injection
unity-container ×10
c# ×7
.net ×2
asp.net ×1
caching ×1
httpcontext ×1
modularity ×1
prism ×1
singleton ×1
structuremap ×1
wcf ×1
wpf ×1