0
我正在嘗試爲Nhibernate使用ServiceStack IUserAuthRepository實現。 我在我的服務IOC容器中註冊了NHibernateUserAuthRepository
,但我不知道如何告訴Nhibernate將AuthUser
和角色映射到數據庫表。使用Nhibernate的Servicestack UserAuth持久性(使用ServiceStack.Authentication.NHibernate)
我的映射發生在容器實例化時ISessionFactory
(使用FluentNhibernate)。
這裏的APPHOST代碼:
container.Register<ICacheClient>(new MemoryCacheClient());
container.Register<IDatabaseFactory>(c => new Oracle10DatabaseFactory(_DomainAssembly, _DomainAssemblyName,
c.Resolve<ConfigurationParameters>()));
// Register EventPublisher
container.RegisterAutoWiredAs<EventPublisher, IEventPublisher>().ReusedWithin(ReuseScope.Request);
container.RegisterAutoWiredAs<EventPublisherInterceptor, IInterceptor>().ReusedWithin(ReuseScope.Request);
// Register Session & UnitOfWork
container.Register<NHibernate.ISession>(item =>
container.Resolve<IDatabaseFactory>().SessionFactory.OpenSession(new EventPublisherInterceptor(container.Resolve<IEventPublisher>())))
.ReusedWithin(ReuseScope.Request);
container.Register<IUnitOfWork>(item => new UnitOfWork(container.Resolve<NHibernate.ISession>())).ReusedWithin(ReuseScope.Request);
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[] { new CredentialsAuthProvider() }));
這裏的Oracle10DatabaseFactory
構造:
public Oracle10DatabaseFactory(Assembly assembly, string namespace, ConfigurationParameters parameters)
{
var fileCache = new ConfigurationFileCache(assembly, parameters.PathToConfigurationFolder);
var config = fileCache.LoadConfigurationFromFile();
if (config == null)
{
var mapping = AutoMap.Assembly(assembly, new MappingConfiguration(new List<string>() { namespace }))
.Conventions.Add<ReferenceIndexConvention>()
.Conventions.Add<GuidIndexConvention>()
.UseOverridesFromAssemblyOf<MappingConfiguration>();
this.sessionFactory =
Fluently.Configure()
.Database(OracleClientConfiguration.Oracle10.ConnectionString(c => c.FromConnectionStringWithKey("ZetesMobility_DataAccess_Connectionstring_Oracle"))
.UseReflectionOptimizer())
.Mappings(m => m.AutoMappings.Add(mapping)) //.Add(baseMapping)
.CurrentSessionContext("web")
.ExposeConfiguration(c => c.BuildSchema(NHibernateExtensions.RecreateSchema()))
.ExposeConfiguration(x => x.SetProperty("hbm2ddl.keywords", "auto-quote"))
.ExposeConfiguration(fileCache.SaveConfigurationToFile)
.BuildSessionFactory();
config = fileCache.LoadConfigurationFromFile();
this.sessionFactory = config.BuildSessionFactory();
}
else
{
this.sessionFactory = config.BuildSessionFactory();
}
}
我如何驗證類添加到映射配置?
非常感謝大家。
現在我得到NHibernate的異常「無會話綁定到當前上下文」當我試圖創建默認用戶。userRepository.GetUserAuthByUserName(「管理員」)失敗。 –
如何/你在哪裏打開你的會話(ISessionFactory.OpenSession())? –
我正在試圖查看它,它很奇怪,因爲GitHub中的代碼與隨Nuget包一起提供的Api不同。公共NHibernateUserAuthRepository構造函數需要'ISessionFactory',但GitHub中的代碼是公共NHibernateUserAuthRepository(INHibernateSessionProvider sessionProvider)。然後我很難理解問題出在哪裏。 –