标签: unity-container

如何在动作过滤器上使用Unity和asp.net mvc执行属性注入?

我试图通过属性注入使依赖注入工作在我的动作过滤器上.我无法弄清楚如何自动设置过滤器的依赖关系.这是我到目前为止的代码.

public class UnityActionInvoker : ControllerActionInvoker
{
    IUnityContainer container;

    public UnityActionInvoker(IUnityContainer container) {
        this.container = container;
    }

    protected override ActionExecutedContext InvokeActionMethodWithFilters(ControllerContext controllerContext, IList<IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters) {
        foreach (var filter in filters) {
            // HELP: dependency injection on all marked filter properties
        }

        return base.InvokeActionMethodWithFilters(controllerContext, filters, actionDescriptor, parameters);
    }
}

public class UnityControllerFactory : DefaultControllerFactory
{
    IUnityContainer container;

    public UnityControllerFactory(IUnityContainer container) {
        this.container = container;
    }

    protected override IController GetControllerInstance(Type controllerType) {
        Controller controller = null;

        if …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc inversion-of-control unity-container

4
推荐指数
1
解决办法
2245
查看次数

WPF + MvvM + Prism

我是Wpf和Mvvm世界的新手,但我发现了几个例子,并且发现有一些不同的方式来实例化模型.我想知道最好/最正确的方法.两种方式都使用Unity

我发了什么:

var navigatorView = new MainView();
navigatorView.DataContext = m_Container.Resolve<INavigatorViewModel>();
m_RegionManager.Regions["NavigatorRegion"].Add(navigatorView);
Run Code Online (Sandbox Code Playgroud)

我做了什么:

var navigatorView = m_Container.Resolve<MainView>;
m_RegionManager.Regions["NavigatorRegion"].Add(navigatorView);
Run Code Online (Sandbox Code Playgroud)

并且我更改了构造函数以接收viewmodel,因此我可以将datacontext指向它:

public MainView(NavigatorViewModel navigatorViewModel)
{
 this.DataContext = navigatorViewModel;
}  
Run Code Online (Sandbox Code Playgroud)

其他例子我发现了另一种方式:

...vm = new viewmodel 
...m = new model
v.model = vm;
Run Code Online (Sandbox Code Playgroud)

获取/设置DataContext

干杯

wpf prism unity-container mvvm

4
推荐指数
1
解决办法
2610
查看次数

我应该将Unity容器传递给我的依赖项吗?

所以我有:

应用A:需要B类(不同的组装)

B级:需要C级(同样,不同的装配)

C类:使用容器来解析各种对象,但容器的生命周期(以及它解析的对象)应该由组合根控制.

我想我理解这在大多数情况下是如何工作的,但是在C类中,我需要根据传入的对象的属性来解决.

我认为我要问的是,容器是否成为依赖关系,因此,如何最好地将它放在需要的地方(不确定我是否真的希望通过一堆构造函数传递它) - 属性注入是要走的路?)

我相信这个来源尽可能干净简洁:

namespace InjectionTest
{
    using System;
    using Microsoft.Practices.Unity;

    public class ApplicationA
    {
        static void Main(string[] args)
        {
            using (IUnityContainer container = new UnityContainer())
            {
                // Normally I'd use this, but for clarity in the example, I'm doing it in code.
                //container.LoadConfiguration(); 
                container.RegisterType<IClassB, ClassB>();
                container.RegisterType<IClassC, ClassC>();
                container.RegisterType<IFooBuilder, FrobBuilder>("frob");
                container.RegisterType<IFooBuilder, WidgetBuilder>("widget");
                IClassB machine = container.Resolve<IClassB>();
                InitialObject bar = new InitialObject() { Name = "widget" };
                machine.doSomethingWithBar(bar);
                bar = new InitialObject() { Name = "frob" …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection unity-container

4
推荐指数
1
解决办法
7024
查看次数

Unity - 如何将多个映射用于相同类型并注入对象

我正在使用Unity 2.0,在下面的代码中我试图在Worker对象中注入一个特定的工具.

我想使用以下代码.但是当然有一个错误"依赖的解决方案失败了".我相信我应该可以做这样的事情,但我很难搞清楚.

IUnityContainer container = new UnityContainer();
container.RegisterType<IWorker, Worker>("Worker")
    .RegisterType<ITool, ToolA>("ToolA")
    .RegisterType<ITool, ToolB>("ToolB")
    .RegisterType<ITool, ToolC>("ToolC");

IWorker worker = container.Resolve<Worker>("ToolA");
Run Code Online (Sandbox Code Playgroud)

我知道这不起作用,但我如何解决这个问题?

BarDev

c# dependency-injection ioc-container unity-container unity2.0

4
推荐指数
1
解决办法
5119
查看次数

查询Unity的所有接口实例

我需要在我的项目中找到所有实现名为IMyInterface的接口并在Unity容器中注册的类.

任何人都知道这样做的方法,没有统一创建在Unity注册的对象的实例?

.net c# asp.net inversion-of-control unity-container

4
推荐指数
1
解决办法
1851
查看次数

如何在 Linux 上更改 WinForms 应用程序的 WM_CLASS?

我有一个使用 WinForms 的跨平台 .NET 应用程序。

为了更好地与 Unity 兼容,我想设置WM_CLASSWinForms 窗口的属性。这可能吗?

.net x11 unity-container winforms

4
推荐指数
1
解决办法
944
查看次数

智能感知和自动完成统一

我正在使用IoC Container Unity,根据文档,将此xmlns属性添加到"unity"部分必须允许Visual Studio执行一些Intellisense操作:

<configuration>
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
    </configSections>
    [..]
    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        [..]
    </unity>
</configuration>
Run Code Online (Sandbox Code Playgroud)

实际上,它不起作用.似乎资源已被(重新)移动.你知道新的链接吗?

c# unity-container

4
推荐指数
1
解决办法
911
查看次数

依赖项未正确注入 mvc 5、owin、unity

我正在使用 ASP.NET MVC 5、EF6、ASP.NET Identity 和 Unity 作为 IoC,并且我想在构建网站时使用 Identity 框架提供的功能来操作用户和注册。

启动文件

public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }

    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(DbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);



        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Auth/Login"),
            Provider = new CookieAuthenticationProvider
             {
                 // Enables the application to validate the security stamp when the user logs in.
                 // …
Run Code Online (Sandbox Code Playgroud)

c# unity-container owin asp.net-mvc-5 asp.net-identity-2

4
推荐指数
1
解决办法
2529
查看次数

如何使用Unity注册AutoMapper配置文件

我有以下AutoMapper配置文件:

public class AutoMapperBootstrap : Profile
{
    protected override void Configure()
    {
        CreateMap<Data.EntityFramework.RssFeed, IRssFeed>().ForMember(x => x.NewsArticles, opt => opt.MapFrom(y => y.RssFeedContent));
        CreateMap<IRssFeedContent, Data.EntityFramework.RssFeedContent>().ForMember(x => x.Id, opt => opt.Ignore());
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在这样初始化它:

var config = new MapperConfiguration(cfg =>
{
       cfg.AddProfile(new AutoMapperBootstrap());
});

container.RegisterInstance<IMapper>("Mapper", config.CreateMapper());
Run Code Online (Sandbox Code Playgroud)

当我尝试在我的构造函数中注入它时:

private IMapper _mapper;
public RssLocalRepository(IMapper mapper)
{
    _mapper = mapper;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

当前类型AutoMapper.IMapper是一个接口,无法构造.你错过了类型映射吗?

如何使用Unity正确初始化AutoMapper配置文件,以便我可以通过DI在任何地方使用映射器?

c# unity-container automapper

4
推荐指数
1
解决办法
7520
查看次数

Prism 7 - 将 IContainer 对象注入视图模型

我最近有机会创建一个新的基于棱镜的应用程序。我用6.3版本已经有一段时间了,但是看到prism 7已经退出预发布,想试一试。我使用 Prism 模板包创建了一个新的棱镜应用程序,并且一切都按预期开箱即用。我更新了视图模型,就像在 6.3 中通常做的那样,传入容器,这样我就可以解析一些稍后会向视图提供信息的对象,在 6.3 中,我将执行以下操作:

public MainWindowViewModel(IRegionManager aRegionManager,
                           IUnityContainer aUnityContainer) : base()
Run Code Online (Sandbox Code Playgroud)

现在在 7.1.0.431 中,我尝试做同样的事情,但更新了接口以考虑新的 IOC 抽象。

public MainWindowViewModel(IRegionManager aRegionManager,
                           IContainerProvider aContainerProvider,
                           IContainerRegistry aContainerRegistry) : base()
Run Code Online (Sandbox Code Playgroud)

这会从 ViewModelLocator.AutoWireViewModel 中为 IContainerX 参数生成一个异常。

System.Exception {Unity.Exceptions.ResolutionFailedException}

{"Resolution of the dependency failed, type = 'Sample.ViewModels.MainWindowViewModel', name = '(none)'.\nException occurred while: while resolving.\nException is: InvalidOperationException - The current type, Prism.Ioc.IContainerProvider, is an interface and cannot be constructed. Are you missing a type 
Run Code Online (Sandbox Code Playgroud)

这就像我缺少一个引用,但我正在将该类型传递到应用程序的 RegisterTypes 调用中,因此应该找到所有引用。我对新的 7.X 版本做错了吗?

编辑:每@mnistic

这是模板包提供的 App.xaml.cs 中的代码,其中传入了 …

prism unity-container mvvm prism-7

4
推荐指数
1
解决办法
3343
查看次数