c# – 在Unity中的类型之间共享终身经理?

前端之家收集整理的这篇文章主要介绍了c# – 在Unity中的类型之间共享终身经理?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Unity文档中看到的示例让您通过内联新的LifetimeManager()来指定生命周期管理器.所以我有这个代码
container.RegisterType<ApplicationDbContext>(new PerRequestLifetimeManager());

container.RegisterType<IUserStore<ApplicationUser>,UserStore<ApplicationUser>>(new PerRequestLifetimeManager(),new InjectionConstructor(typeof (ApplicationDbContext)));

container.RegisterType<UserManager<ApplicationUser>>(new PerRequestLifetimeManager());

很好,但我想知道为什么我要创建这么多实例.我有什么理由不这样写吗?

var lifetimeManager = new PerRequestLifetimeManager();

container.RegisterType<ApplicationDbContext>(lifetimeManager);

container.RegisterType<IUserStore<ApplicationUser>,UserStore<ApplicationUser>>(lifetimeManager,new InjectionConstructor(typeof (ApplicationDbContext)));

container.RegisterType<UserManager<ApplicationUser>>(lifetimeManager);

这看起来很明显但是通过PDF阅读所有的例子都是前一种风格而没有评论,所以我想知道我是否不理解它是如何工作的.

解决方法

不,你不能这样做.如果您尝试以下操作,您会发现您的应用程序会抛出异常:

The lifetime manager is already registered. Lifetime managers cannot be reused,please create a new one.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The lifetime manager is already registered. Lifetime managers cannot be reused,please create a new one.

原文链接:https://www.f2er.com/csharp/244707.html

猜你在找的C#相关文章