ASP.NET Autofac Singleton运行两次构造函数注入 [英] ASP.NET Autofac Singleton running twice constructor injection
问题描述
我不确定我的期望是否错误.我正在使用ASP.NET MVC(不是核心)和Autofac.我的图片出现在网站的每个页面上,并且创建了一个服务来返回一个随机的图片名称,例如
I'm not sure if my expectations are wrong. I am using ASP.NET MVC (not core) and Autofac. I have an image that appears on every page of my website, and I have created a service to return a random image name like so
界面
public interface IBreadCrumbImage
{
string GetImage();
}
CLASS(为简洁起见)
CLASS (snipped for brevity)
public class BreadCrumbImage : IBreadCrumbImage
{
public string GetImage()
{
return ImageUrl;
}
}
GLOBAL.ASAX.CS(自动注册)
GLOBAL.ASAX.CS (Autofac registration)
protected void Application_Start()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<BreadCrumbImage>().As<IBreadCrumbImage>().SingleInstance();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
控制器
public class HomeController : Controller
{
private IBreadCrumbImage _breadCrumbImage;
public ActionResult Index(BreadCrumbImage breadCrumbImage)
{
_breadCrumbImage = breadCrumbImage;
var x = _breadCrumbImage.GetImage();
ViewBag.BreadCrumbImage = "/img/BreadCrumbs/" + x;
return View();
}
}
所有这些工作正常,并且我的IMAGE图像随机显示..但是,设置断点,此BreadCrumbImage服务每次被访问时都会实例化.我想实例化一次(图像的加载是在BreadCrumbImage的构造函数中完成的,它读取文件并将它们放入数组中-无需在应用程序启动时执行多次以上操作,而GetImages只是从数组抓取rnd).
All of this works, and I get my IMAGE showing up random.. however, setting breakpoints, this BreadCrumbImage service is being instantiated every time it is accessed. I'd like to have this instantiated once (the loading of the images is done in the constructor of BreadCrumbImage, it reads files and puts them into an array -- no need to do that more than once on app startup, and the GetImages just grabs rnd from array).
我是否在SingleInstance中注册了此错误,或者它不能按我期望的那样工作,我将不得不将一些基本解析器绑定到Global.asax.cs中创建的构建器?
Am I registering this wrong with the SingleInstance, or does this just not work as I'm expecting it to and I'm going to have to have some base resolver that's tied to the builder that's created in Global.asax.cs?
更新:回答,谢谢特拉维斯.需要将其放入autofac配置中,所有内容都像一个魅力一样.
UPDATE: Answer, thank you Travis. Needed to put this in to the autofac configuration, and everything worked like a charm.
builder.RegisterType<ExtensibleActionInvoker>()
.As<IActionInvoker>()
.WithParameter("injectActionMethodParameters", true);
推荐答案
看一下您提供的控制器,您没有使用构造函数注入. Index
方法是一个动作方法,不是一个动作方法.构造函数.此外,它要求的是BreadCrumbImage
而不是IBreadCrumbImage
.
Looking at the controller you provided, you're not using constructor injection. The Index
method is an action method, not a constructor. Further, it's asking for a BreadCrumbImage
not an IBreadCrumbImage
.
如果您从断点来看调用堆栈,我猜您将看到实例化该BreadCrumbImage
而不是Autofac的MVC模型绑定.我没有在您的Autofac设置中看到您正在使用
If you look at the call stack from your breakpoints, I'm guessing you're going to see MVC model binding going on instantiating that BreadCrumbImage
, not Autofac. I don't see anywhere in your Autofac setup that you're using the action parameter injection mechanism, and if you were you'd still see MVC model binding in the call stack because you didn't register BreadCrumbImage
as itself - you registered it as IBreadCrumbImage
so that's the only thing Autofac will resolve it as. You won't be able to resolve the concrete BreadCrumbImage
out of the container using the registrations you're showing.
这篇关于ASP.NET Autofac Singleton运行两次构造函数注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!