在 Prism.Unity 中注册一个依赖于另一个服务的服务实例

我有一个身份验证服务,我在我的 Unity Container 中注册,如下所示:

Container.RegisterInstance<IAuthenticationService>(new AuthenticationService());

我的问题是我AuthentificationService依赖于INavigationService,它的构造函数将它作为参数:

public AuthenticationService(INavigationService navigationService)

据我了解,Unity Container 负责注入INavigationService实现。但在这种情况下,我提供了AuthenticationService实例,那么我如何告诉 UnityINavigationService在我的AuthentificationService implementation.

豪金格

首先,您使用像 unity 这样的 DI Container 来摆脱new. 因此,如果您被迫这样做,注册实例是最后的手段。

通常,您要注册types,告诉 unity要创建什么,将如何创建给容器。

你应该写

Container.RegisterType<IAuthenticationService, AuthenticationService>( new ContainerControlledLifetimeManager() );

在创建AuthenticationService实例时,unity 将尝试解析所有构造函数参数,并将 传递INavigationService给您的AuthenticationService

internal class AuthenticationService
{
    public AuthenticationService( INavigationService navigationService )
    {
        // here you've got the Navigation Service...
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Unity中使用Prism在同一实例中注册服务?

Prism5 + Unity,从另一个模块为PopupWindowAction设置视图

DryIoC - 在 Unity Prism 中注册等效项

使用Unity在Prism for Xamarin Forms中注册实例以进行导航

在 Prism.Unity.Forms 中注销 View 和 ViewModel

如何测试依赖于另一个服务(又依赖于Http服务)的服务/组件?

如何使用Unity和Prism通过约定注册类型?

Unity + EntityFramework + PRISM-“依赖项解析失败”

使用依赖于另一个服务中的服务的管道

将Prism.Unity.UnityBootstrapper与Unity.IUnityContainer一起使用

无法构建 Unity 容器 - Prism

单元测试Angular服务,该服务依赖于另一个使用InjectionToken的服务

如何等待一个GameObject实例完成自己的启动,从另一个GameObject实例的Unity启动?

如何告诉另一个脚本在 Unity 中实例化一个特殊的预制件?

在项目“应用”中,已解析的Google Play服务库依赖项依赖于另一个版本

错误:在项目“应用”中,已解决的Google Play服务库依赖项依赖于另一个

当 Prism 中的另一个属性发生变化时更新一个属性

升级到Prism和Xamarin表单后的Unity类型注册问题

WPF Prism 6.2 (Unity) - 如何在启动模块加载时注册事件

WPF Prism.Unity-如何使用命名依赖注入构造ViewModel

如何/在哪里使用PRISM和Unity实例化MVVM中的模型对象

Prism with Xamarin.Forms - 如何自动装配另一个 ContentView 中的 ContentView

Dock 上的 .desktop 文件在 unity/android studio 的另一个实例中打开

Unity 通过碰撞改变另一个类

将自定义 IFileProvider 添加到依赖于另一个 DI 服务的 RazorViewEngineOptions

redux-saga-1异步的服务器端渲染依赖于另一个

如果 Kubernetes 端点依赖于另一个服务端点,如何创建它

WPF与PRISM 4.0和Unity的工作示例

将服务实例注入另一个服务