In my account controller i am trying to get an object of nhibernate ISession but on CreateContoller method of my controller factory class StructureMapControllerFactory it throws "Make sure that the controller has a parameterless public constructor.". Although with any other controller it works fine.
Below is my StructureMapControllerFactory class:
public class StructureMapControllerFactory : DefaultControllerFactory
{
public override IController CreateController(RequestContext requestContext, string controllerName)
{
try
{
var controllerType = base.GetControllerType(requestContext, controllerName);
return ObjectFactory.GetInstance(controllerType) as IController;
}
catch (Exception)
{
//Use the default logic
return base.CreateController(requestContext, controllerName);
}
}
}
and code s开发者_Go百科nippet from Global.asax file:
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(new SmartHRMS.Utility.StructureMapControllerFactory());
StructureMap.ObjectFactory.Initialize(x =>
{
x.For<NHibernate.ISessionFactory>()
.Singleton()
.Use(SmartHRMS.Utility.SessionHelper.GetSessionFactory());
x.For<NHibernate.ISession>()
.HttpContextScoped()
.Use(context => context.GetInstance<NHibernate.ISessionFactory>().OpenSession());
});
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
protected void Application_EndRequest()
{
StructureMap.ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}
Exception:
{"An error occurred when trying to create a controller of type 'HCM.Controllers.AccountController'. Make sure that the controller has a parameterless public constructor."}
Thanks to mookid8000 who gave me the hint, after finding out the actual error i changed my StructureMapControllerFactory and Global.asax.cs like this:
public class StructureMapControllerFactory : DefaultControllerFactory
{
readonly Container container;
public StructureMapControllerFactory(Container container)
{
this.container = container;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return base.GetControllerInstance(requestContext, controllerType);
try
{
return container.GetInstance(controllerType) as Controller;
}
catch (StructureMapException)
{
throw;
}
}
}
and
protected void Application_Start()
{
var container = new StructureMap.Container(config =>
{
config.For<System.Web.Security.MembershipProvider>()
.Use(System.Web.Security.Membership.Provider);
config.For<SmartHRMSTest.Controllers.IFormsAuthentication>()
.Use<SmartHRMSTest.Controllers.FormsAuthenticationService>();
config.For<SmartHRMSTest.Controllers.IMembershipService>()
.Use<SmartHRMSTest.Controllers.AccountMembershipService>();
config.For<NHibernate.ISessionFactory>()
.Singleton()
.Use(SmartHRMS.Utility.SessionHelper.GetSessionFactory());
config.For<NHibernate.ISession>()
.HttpContextScoped()
.Use(context => context.GetInstance<NHibernate.ISessionFactory>().OpenSession());
});
ControllerBuilder.Current.SetControllerFactory(new SmartHRMS.Utility.StructureMapControllerFactory(container));
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
精彩评论