ASP.NET MVC : 모든 요청에 대해 컨트롤러가 생성됩니까?
아주 간단한 질문 : ASP.NET의 컨트롤러는 모든 HTTP 요청에 대해 생성됩니까, 아니면 응용 프로그램 시작시 생성되어 요청 전체에서 재사용됩니까?
컨트롤러는 특정 HTTP 요청에 대해서만 생성됩니까?
이전 가정이 맞다면 믿을 수 있습니까? 하나의 요청에만 적용되는 데이터베이스 컨텍스트 (Entity Framework)를 만들고 싶습니다. 컨트롤러의 생성자에서 초기화 된 속성으로 생성하면 모든 요청에 대해 새로운 컨텍스트 인스턴스가 생성 될 수 있습니까?
ControllerFactory (기본적으로 DefaultControllerFactory)에 의해 모든 요청에 대해 컨트롤러가 생성됩니다.
http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultcontrollerfactory.aspx
점을 유의 Html.Action
HTML을 도우미가 다른 컨트롤러를 생성합니다.
짧은 버전은 ControllerActivator.Create
(모든 요청에 대해) 컨트롤러를 생성하기 위해 호출됩니다 (Resolver가 설정되지 않은 경우 DependencyResolver 또는 Activator를 통해 새 컨트롤러를 초기화 함).
public IController Create(RequestContext requestContext, Type controllerType) {
try {
return (IController)(_resolverThunk().GetService(controllerType) ?? Activator.CreateInstance(controllerType));
}
더 긴 버전은 이것입니다 (여기 MvcHandler의 소스 코드입니다)
protected internal virtual void ProcessRequest(HttpContextBase httpContext)
{
SecurityUtil.ProcessInApplicationTrust(() =>
{
IController controller;
IControllerFactory factory;
ProcessRequestInit(httpContext, out controller, out factory);
try
{
controller.Execute(RequestContext);
}
finally
{
factory.ReleaseController(controller);
}
});
}
private void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)
{
//non relevant code
// Instantiate the controller and call Execute
factory = ControllerBuilder.GetControllerFactory();
controller = factory.CreateController(RequestContext, controllerName);
if ( controller == null )
{
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
MvcResources.ControllerBuilder_FactoryReturnedNull,
factory.GetType(),
controllerName));
}
}
다음은 컨트롤러 공장 코드입니다.
public virtual IController CreateController(RequestContext requestContext, string controllerName) {
Type controllerType = GetControllerType(requestContext, controllerName);
IController controller = GetControllerInstance(requestContext, controllerType);
return controller;
}
기본적으로 이것을 호출합니다.
protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType) {
return ControllerActivator.Create(requestContext, controllerType);
}
ControllerActivator에서이 메서드를 호출합니다 (이 코드는 DependencyResolver에 인스턴스를 요청하거나 Activator 클래스를 사용합니다).
public IController Create(RequestContext requestContext, Type controllerType) {
try {
return (IController)(_resolverThunk().GetService(controllerType) ?? Activator.CreateInstance(controllerType));
}
이것은 너무 많은 정보에 속할 수 있습니다 ... 그러나 저는 당신이 정말로 모든 요청에 대해 새로운 컨트롤러를 얻는다는 것을 보여주고 싶었습니다.
컨트롤러에 대한 빈 생성자를 만들고 생성자에 중단 점을 넣었습니다. 새로운 요청이있을 때마다 맞았습니다. 그래서 모든 요청에 대해 만들어 졌다고 생각합니다.
The controller will be created when any Action in a specific Controller is performed.
I have a project where all of my Controllers inherit from an ApplicationController
and every time that an action is performed, the breakpoint is hit inside of the ApplicationController
- regardless of its "current" Controller.
I initialize my agent (which works as my context) whenever my controller is created like such:
public IWidgetAgent widgetAgent { get; set; }
public WidgetController()
{
if (widgetAgent == null)
{
widgetAgent = new WidgetAgent();
}
}
This is obviously not what you need - as you mentioned that you only wanted a single instance each time it was called. But it is a good place to check what is going on each time and to ensure that another instance of your context does not currently exist.
Hope this helps.
Controllers are created for every request. The magic happens in the routing in the gobal.aspx. The mapping paths direct MVC to which controller to create and action on the controller to call, and parameters to pass to them.
http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-vb
참고URL : https://stackoverflow.com/questions/5425920/asp-net-mvc-is-controller-created-for-every-request
'code' 카테고리의 다른 글
Java에서 instanceof 피하기 (0) | 2020.08.27 |
---|---|
socket.io 및 세션? (0) | 2020.08.27 |
문맥 자유 문법이란 무엇입니까? (0) | 2020.08.27 |
Python 3을 실행하기 위해 IPython 노트북을 얻는 방법? (0) | 2020.08.27 |
SQL Server의 이상한 작동 문제 (-100 / -100 * 10 = 0) (0) | 2020.08.27 |