我有这个设置:
public static void Initialize(ISessionFactory factory)
{
var container = new Container();
InitializeContainer(container, factory);
container.RegisterMvcControllers(
Assembly.GetExecutingAssembly());
container.RegisterMvcAttributeFilterProvider();
container.Verify();
DependencyResolver.SetResolver(
new SimpleInjectorDependencyResolver(container));
}
private static void InitializeContainer(
Container container, ISessionFactory factory)
{
container.RegisterPerWebRequest<ISession>(
() => factory.OpenSession(), true);
}
Initialize 方法在以下位置调用Application_Start
:
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
SimpleInjectorInitializer.Initialize(
new NHibernateHelper(
Assembly.GetCallingAssembly(),
this.Server.MapPath("/"))
.SessionFactory);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
但是当我尝试调用控制器操作时,我得到一个ArgumentException
:
类型“PositionReportApi.Controllers.PositionsController”没有默认构造函数
堆栈跟踪:
在 System.Linq.Expressions.Expression.New(Type type) 在 System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor控制器描述符,类型控制器类型)
我无法注册ISession
.
如何注册由工厂创建的 ISession?