2014-10-29 1 views
2

로컬에서 작동하는 .net 4.5.1을 사용하는 MVC 4 웹 응용 프로그램이 있는데 라이브 서버에 사이트를 배포하면 예외가 발생합니다 컨트롤러를 호출 할 때 예외가 발생합니다. 컨트롤러에 생성자를 넣을 때 예외가 throw됩니다. 컨트롤러에서 생성자를 제거하여 응용 프로그램을 테스트 했으므로 제대로 작동합니다. 왜 이런 일이 발생하는지 파악할 수 없습니다.asp.net mvc 응용 프로그램에서 응용 프로그램을 배포 할 때 컨트롤러 예외가 발생합니다.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

소스 오류 : 여기

namespace Test.Controllers 
{ 
    public class TestController : Controller 
    { 
    private readonly ICountryService _countryService; 

    public TestController() 
    { 
     _countryService=new CountryService(); 
    } 

    // GET: /Test/ 
    public ActionResult Index() 
    { 

      var items = _countryService.GetAllCountries(); 
     return View(items.ToList()); 
    } 
    } 
} 

예외입니다 :

여기 내 컨트롤러

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] Test.Controllers.TestController..ctor() +74

[TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232 System.Activator.CreateInstance(Type type, Boolean nonPublic) +83 System.Activator.CreateInstance(Type type) +6 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55

[InvalidOperationException: An error occurred when trying to create a controller of type 'Test.Controllers.TestController'. Make sure that the controller has a parameterless public constructor.] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +178 System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +77 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +66 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +191 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

나는 라이브 서버에 웹 사이트를 배포 할 때 이런 일이 왜

.

도움을 주시면 감사하겠습니다. 이 질문에 답변하기 위해 다른 정보가 필요하면 알려주십시오.

+0

가능한 중복 :

NinjectWebCommon.cs :

여기 Ninject에를 사용하여 의존성 주입을하는 방법 어떻게 해결할 수 있습니까?] (http://stackoverflow.com/questions/4660142/what-is-a-nul lreferenceexception-and-how-do-i-fix-it) –

+1

아마도'Response'는 null입니까? – Johan

답변

0

이것은 Response 개체가 null이기 때문입니다. 또한 브라우저에서 (예외가 발생한 경우) 스택 추적 바로 위에 있음을 알 수 있습니다.

편집 : 당신이 알 경우 &이 BTW, 당신의 HttpContext 객체가 대신 null

것 당신은 시도 할 수 있습니다 :

var res = System.Web.HttpContext.Current.Response; 
res.Write("hi"); 

을이 작동)

1

는 그것은 NullReferenceException에서의 귀하의 생성자. 즉, 아직 인스턴스화되지 않은 객체에 액세스하려는 것입니다. 액세스하는 유일한 객체는 Response 객체입니다. 따라서 논리적 결론은 컨트롤러가 인스턴스화 될 때 컨트롤러의 Response 멤버가 null이라는 것입니다. 이는 Initialize 메서드가 호출 될 때까지 컨텍스트 관련 데이터 (Controller.Request, Controller.Response 등)가 채워지지 않기 때문입니다.

제 질문은 ASP.NET MVC의 Response에 직접 쓰고 싶습니다. ASP.NET WebForms에서 가져온 나쁜 습관 인 것 같습니다. 무엇 NullReferenceException이있다

private static void RegisterServices(IKernel kernel) 
{ 
    kernel.Bind<ICountryService>().To<CountryService>(); 
    // other bindings.... 
    //... 
} 

TestController.cs

public class TestController : Controller 
{ 
    private readonly ICountryService _countryService; 

    public TestController(ICountryService countryService) 
    { 
     _countryService = countryService; 
    } 

    // GET: /Test/ 
    public ActionResult Index() 
    { 
     var items = _countryService.GetAllCountries(); 
     return View(items.ToList()); 
    } 

}의

+0

실제로 응답을 사용하고 싶지 않습니다. 그냥 생성자 내부에서 내 서비스를 인스턴스화하려고합니다. 주석 처리 된 코드에서이 예외가 주석 처리 될 때 볼 수 있습니다. – Mussammil

+1

@ Mussammil 왜 당신은 종속성을 사용하여 컨트롤러에 삽입합니까? 주입? –

+0

Porumbo, 나는 asp.net mvc에 익숙하지 않습니다. 의존성 주입을 많이 사용하는 경우, 의존성 주입을 사용하면 응용 프로그램 성능이 저하됩니까? – Mussammil

관련 문제