Autofac - 没有找到带有“公共绑定标志”的构造函数
尝试解析我的存储库时,我不断收到错误。
在类型上找到的带有“公共绑定标志”的构造函数都不能使用可用的服务和参数来调用: 无法解析构造函数“Void .ctor(Repository.Data.INHibernateSession)”的参数“Repository.Data.INHibernateSession nHibernateSession”。
Global.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; }
}
似乎我所做的就是创建一个单例,将其注入到会话中,然后注入到存储库中。 (我所有其他依赖项都工作正常)
如果有人能指出我正确的方向,为什么这无法解决,我很困惑?
I keep on getting an error when trying to resolve my repositories.
None of the constructors found with 'Public binding flags' on type can be invoked with the available services and parameters:
Cannot resolve parameter 'Repository.Data.INHibernateSession nHibernateSession' of constructor 'Void .ctor(Repository.Data.INHibernateSession)'.
Global.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();
then i have my singleton factory
public interface INhibernateFactory : ISingletonDependency
{
ISessionFactory SessionFactory { get; }
}
then my instance per lifetime
public interface INHibernateSession : IDependency
{
ISession Session { get; }
}
public class NHibernateSession : IDependency
{
private ISession _session;
private readonly ISessionFactory _sessionFactory;
public NHibernateSession(ISessionFactory sessionFactory)
{
_sessionFactory = sessionFactory;
}
then in my Generic repository
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; }
}
It seems all i'm doing is creating a singleton, inject that to the session, then inject into repository. (all my other dependencies work fine)
Appreciate if someone could point me in the right direction why this wont resolve, I'm quite stumped?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到的错误本身就说明了问题,它们指出了某种无法解决的类型。最新更改后,这是缺少的“ISessionFactory”接口。
进一步详细说明:当前的问题是,当要求 Autofac 构建
NHibernateSession
实例时,它不知道如何提供ISessionFactory
实例。要“学习”容器如何构建会话工厂,仅提供NHibernateFactory
实例是不够的。考虑这样的注册:最后一行注册了一个知道如何提供会话工厂实例的 lambda。
您需要为容器提供涉及的每种类型。使用“无构造函数...”错误来查看缺少哪些类型。
要进一步深入了解 Autofac 和 NHibernate,您必须在网络上搜索。例如,这个问题讨论 NHibernate 会话:
使用 Autofac 管理 NHibernate ISession
The errors you get speaks for them selves, they state some type that cannot be resolved. After your latest change, this is the the 'ISessionFactory' interface which is missing.
To further elaborate: the current problem is that Autofac, when asked to build
NHibernateSession
instances, doesn't know how to provide anISessionFactory
instance. To "learn" the container how to build session factories it is not enough to provide theNHibernateFactory
instance alone. Consider this registration:The last line there registers a lambda that knows how to provide the session factory instance.
You'll need to provide the container with every type involved. Use the "no constructors..." error to see which types are missing.
To dig further into Autofac and NHibernate you'll have to search around the net. E.g. this question discusses NHibernate sessions:
Managing NHibernate ISession with Autofac
我对 AutoFac 不熟悉;但是您的 NHibernateSession 类是否必须实现 INHibernateSession 接口才能正确解析?
马特
I am not familiar with AutoFac; but does your NHibernateSession class have to implement your INHibernateSession interface to resolve correctly?
Matt