Autofac SingleInstance无法正常工作 [英] Autofac SingleInstance not working

查看:141
本文介绍了Autofac SingleInstance无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使一个Singleton实例与Autofac一起使用.我有点用Winforms做准mvvm类型的事情,只是一个实验,所以不要挂断电话.但是我正在尝试让我的模型成为单个实例,并在命令中带有引用(ICommand并不是WPF的变体):

I am trying to get a Singleton instance working with Autofac. I'm kind of doing a quasi-mvvm type thing with Winforms, just an experiment so don't get to hung up on that. But I am trying you have my model be a single instance with a reference in a command (ICommand here is not the WPF variety):

我对容器进行了以下设置:

I have the following setup of a container:

var cb = new ContainerBuilder();
cb.RegisterType<CalculateCommissionCommand>().As<ICommand<TradeEntry>>().SingleInstance();
cb.RegisterType<CalculationsModel>().As<ICalculationsModel>().SingleInstance();
cb.Register(c => new CalculationsView() { Model = c.Resolve<ICalculationsModel>() }).SingleInstance();
cb.Build();

现在,命令将ICalculationsModel用作构造函数参数.但是,当我在要传递给Command的模型中设置一个值时,该值不会出现在已经使用CalculationsView设置的模型中.尽管调用了"singleInstance"方法,但似乎正在向命令和视图传递不同的CalculationsModel实例.我想念什么吗?为什么会这样?

Now the Command takes an ICalculationsModel as a constructor parameter. However, when I set a value in the Model being passed to the Command, that value does not appear within the model that has already been set withing the CalculationsView. It seems that the command and the view are being passed different instances of the CalculationsModel despite the "singleInstance" method being called. Am I missing something? Why is this happening?

推荐答案

从代码中尚不清楚如何存储/使用容器.您可能已经创建了多个容器.

It's not clear from your code how you are storing/using the container. It is likely you have created multiple containers.

这篇关于Autofac SingleInstance无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆