편집에 대해 정의 된 매개 변수가없는 생성자 : 이것은 고정 -ASP.NET MVC2 오류 :이 개체
솔루션 아래 해결 방법을 참조하십시오 : 우선 잘못 내 노드는 /shared/web.config에 정의했다 WebUI 프로젝트의 루트에있는 web.config 대신. 또한 web.config 내에서 연결 문자열을 올바르게 정의하지 않았습니다.
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return null;
else
return (IController)container.Resolve(controllerType);
}
:
<configuration>
<configSections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
<!--more sectiongroup and sections redacted for brevity -->
</configSections>
<castle>
<components>
<component id="ProdsRepository" service="DomainModel.Abstract.IProductsRepository, DomainModel" type="DomainModel.Concrete.SqlProductsRepository, DomainModel">
<parameters>
<connectionString>Data Source=.\SQLExpress;Initial Catalog=SportsStore; Integrated Security=SSPI</connectionString>
</parameters>
</component>
</components>
</castle>
나는 또한과 같이 유효하지 않은 요청에 대해 null을 반환하는 WindsorControllerFactory.cs (IOC의 컨테이너)의 메소드 본문을 조정했다 : 나는 아래의 적절한 Web.config의 섹션을 붙여 넣은 솔루션의 끝
저는 Sanderson에 의해 Pro ASP.NET MVC2라는 책을 읽었습니다. IoC 컨테이너를 구현하고 web.config를 곧바로 만들었습니다. 내 응용 프로그램을 실행하려고하면 "이 객체에 대해 정의 된 매개 변수가없는 생성자가 없습니다."라는 오류 메시지가 나타납니다.
일부 검색 후 SO here에서 정확한 문제를 발견했습니다. 해결 방법은 매개 변수없이 생성자를 만드는 것이지만이 작업을 수행하는 데 문제가 있습니다.
public ProductsRepository() : this(new productsRepository())
{
}
을 나는 "새로운 이후에 갈 필요가 정확히 무엇에 대한 불분명 해요 : 내가 일을 시도 매개 변수가 공공 ProductsController가 위
namespace WebUI.Controllers
{
public class ProductsController : Controller
{
private IProductsRepository productsRepository;
public ProductsController(IProductsRepository productsRepository)
{
this.productsRepository = productsRepository;
}
public ViewResult List()
{
return View(productsRepository.Products.ToList());
}
}
}
아래 ProductsController.cs의 코드를 붙여 넣은 ". IProducts 리 보셋은 작동하지 않는 것 같고 내가 작성한 것도하지 않습니다. 아래 스택 추적을 붙여 넣었습니다.
Stack Trace:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +86
System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +230
System.Activator.CreateInstance(Type type, Boolean nonPublic) +67
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
[InvalidOperationException: An error occurred when trying to create a controller of type 'WebUI.Controllers.ProductsController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +190
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +68
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +118
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +46
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +63
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +13
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8682818
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
어떤 도움을 주시면 감사하겠습니다.
편집 : 게시 WindsorControllerFactory.cs 코드 :
namespace WebUI
{
public class WindsorControllerFactory : DefaultControllerFactory
{
WindsorContainer container;
// The contructor:
// 1. Sets up a new IoC container
// 2. Registers all components specified in web.config
// 3. Registers all controller types as components
public WindsorControllerFactory()
{
// Instantiate a container, taking config from web.config
container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
// Also register all the controller types as transient
var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;
foreach (Type t in controllerTypes)
container.AddComponentLifeStyle(t.FullName, t, Castle.Core.LifestyleType.Transient);
}
// Constructs the controller instance needed to service each request
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return (IController)container.Resolve(controllerType);
}
}
}
Edit2가 : 타당한의 Web.config 노드 :
<configSections>
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
Castle.Windsor" />
</configSections>
<castle>
<components>
<component id="ProdsRepository"
service="DomainModel.Abstract.IproductsRepository, DomainModel"
type="DomainModel.Concrete.SqlProductsRepository, DomainModel"></component>
<parameters>
</parameters>
</components>
</castle>
가능한 중복하여 작업을 수행 할 수 있도록
하지만 최선은 될 것이다.NET MVC :이 개체에 대해 정의 된 매개 변수가없는 생성자 없음] (http://stackoverflow.com/questions/1355464/asp-net-mvc-no-parameterless-constructor-defined-for-this-object) –