【发布时间】:2011-07-18 22:22:39
【问题描述】:
我在尝试解析存储库时不断收到错误消息。
在类型上找到带有“公共绑定标志”的构造函数都不能使用可用的服务和参数调用: 无法解析构造函数“Void .ctor(Repository.Data.INHibernateSession)”的参数“Repository.Data.INHibernateSession nHibernateSession”。
全球.asax
builder.RegisterAssemblyTypes(assembly).Where(t => typeof(IDependency).IsAssignableFrom(t)).
AsImplementedInterfaces().InstancePerLifetimeScope();
builder.RegisterAssemblyTypes(assembly).Where(t => typeof(ISingletonDependency).IsAssignableFrom(t)).
AsImplementedInterfaces().SingleInstance();
builder.RegisterAssemblyTypes(assembly).Where(t => typeof(ITransientDependency).IsAssignableFrom(t)).
AsImplementedInterfaces().InstancePerDependency();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency();
然后我有我的单身工厂
public interface INhibernateFactory : ISingletonDependency
{
ISessionFactory SessionFactory { get; }
}
然后我的生命周期实例
public interface INHibernateSession : IDependency
{
ISession Session { get; }
}
public class NHibernateSession : IDependency
{
private ISession _session;
private readonly ISessionFactory _sessionFactory;
public NHibernateSession(ISessionFactory sessionFactory)
{
_sessionFactory = sessionFactory;
}
然后在我的通用存储库中
public class Repository<T> : IRepository<T> where T : class
{
private readonly INHibernateSession _nHibernateSession;
public Repository(INHibernateSession nHibernateSession)
{
_nHibernateSession = nHibernateSession;
}
public ISession Session
{
get { return _nHibernateSession.Session; }
}
似乎我所做的只是创建一个单例,将其注入会话,然后注入存储库。 (我所有其他依赖项都可以正常工作)
如果有人能指出我正确的方向,为什么这不能解决,我很困惑?
【问题讨论】:
-
ISessionFactory在 autofac 中注册了吗? -
我尝试添加
builder.RegisterType(typeof(ISessionFactory)).InstancePerLifetimeScope();,但仍然出现同样的错误 -
但是你需要注册实现
ISessionFactory的具体类 -
我将其更改为
builder.RegisterInstance(new NhibernateFactory()).As(typeof(INhibernateFactory)).SingleInstance();,但我仍然可以使用可用的服务和参数调用类型“Repository.Data.NHibernateSession”上的“公共绑定标志”的构造函数之一:不能解析构造函数“Void .ctor(NHibernate.ISessionFactory)”的参数“NHibernate.ISessionFactory sessionFactory”。
标签: nhibernate asp.net-mvc-3 dependency-injection autofac