These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: using NHibernate in Windows Service
PostPosted: Thu Oct 09, 2008 9:57 am 
Newbie

Joined: Thu Oct 09, 2008 9:36 am
Posts: 5
Hi.
I have a problem using NHibernate in Windows Service.

I have created two classes:
1. NHLogEntry
2. LogWriter

NHLogEntry is a persistent class and therefore has its own mapping info. The NHLogEntry.hbm.xml is located in the same assembly as the NHLogEntry belongs to.

LogWriter stores instances of NHLogEntry into a database via NHibernate. LogWriter lives in its own assembly apart from the NHLogEntry.

I have also created a unit test for the NUnit which tests whether the work gets done correctly.

As long as I use LogWriter and NHLogEntry in a unit test or in any other windows application everything goes OK.

But when I try to use these two in a Windows Service the NHibernate fails to Configure() by not locating info about the NHLogEntry.

Information regarding the exception I get is below.
Hibernate version: 2.0.1.4000

Mapping documents: App.Config, NHLogEntry.hbm.xml

Code between sessionFactory.openSession() and session.close():
void ILogWriter.write ( LogEntry entry ) {
this._trans = this._session.BeginTransaction();
NHLogEntry e = new NHLogEntry();
e.Entry = entry;
this._session.Save ( e );
this._trans.Commit();
}

Full stack trace of any exception that occurs:
NHibernate.Cfg.HibernateConfigException: An exception occurred during configuration of persistence layer. ---> System.IO.FileNotFoundException: Could not find file 'T:\Ice-3.3.0-VC90\bin\hibernate.cfg.xml'.
File name: 'T:\Ice-3.3.0-VC90\bin\hibernate.cfg.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlCharCheckingReader.Read()
at System.Xml.XsdValidatingReader.Read()
at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader)
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
--- End of inner exception stack trace ---
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
at NHibernate.Cfg.Configuration.Configure(XmlReader textReader)
at NHibernate.Cfg.Configuration.Configure(String fileName, Boolean ignoreSessionFactoryConfig)
at NHibernate.Cfg.Configuration.Configure()
at DIAmatix.Services.Log.DAL.LogWriterDAL._initNH() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriterDAL\LogWriterDAL.cs:line 39
NHibernate.Cfg.HibernateConfigException: An exception occurred during configuration of persistence layer. ---> System.IO.FileNotFoundException: Could not find file 'T:\Ice-3.3.0-VC90\bin\hibernate.cfg.xml'.
File name: 'T:\Ice-3.3.0-VC90\bin\hibernate.cfg.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlCharCheckingReader.Read()
at System.Xml.XsdValidatingReader.Read()
at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader)
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
--- End of inner exception stack trace ---
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
at NHibernate.Cfg.Configuration.Configure(XmlReader textReader)
at NHibernate.Cfg.Configuration.Configure(String fileName, Boolean ignoreSessionFactoryConfig)
at NHibernate.Cfg.Configuration.Configure()
at DIAmatix.Services.Log.DAL.LogWriterDAL._initNH() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriterDAL\LogWriterDAL.cs:line 42
at DIAmatix.Services.Log.DAL.LogWriterDAL..ctor() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriterDAL\LogWriterDAL.cs:line 63
NHibernate.Cfg.HibernateConfigException: An exception occurred during configuration of persistence layer. ---> System.IO.FileNotFoundException: Could not find file 'T:\Ice-3.3.0-VC90\bin\hibernate.cfg.xml'.
File name: 'T:\Ice-3.3.0-VC90\bin\hibernate.cfg.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlCharCheckingReader.Read()
at System.Xml.XsdValidatingReader.Read()
at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader)
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
--- End of inner exception stack trace ---
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
at NHibernate.Cfg.Configuration.Configure(XmlReader textReader)
at NHibernate.Cfg.Configuration.Configure(String fileName, Boolean ignoreSessionFactoryConfig)
at NHibernate.Cfg.Configuration.Configure()
at DIAmatix.Services.Log.DAL.LogWriterDAL._initNH() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriterDAL\LogWriterDAL.cs:line 42
at DIAmatix.Services.Log.DAL.LogWriterDAL..ctor() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriterDAL\LogWriterDAL.cs:line 66
at DIAmatix.Services.Log.LogWriterIce..ctor() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriter\LogWriter.cs:line 20
10/9/2008 17:21:27:218 DIAmatixBox: error: IceBox.FailureException
reason = "ServiceManager: exception while starting service Log: NHibernate.Cfg.HibernateConfigException: An exception occurred during configuration of persistence layer. ---> System.IO.FileNotFoundException: Could not find file 'T:\Ice-3.3.0-VC90\bin\hibernate.cfg.xml'.
File name: 'T:\Ice-3.3.0-VC90\bin\hibernate.cfg.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlCharCheckingReader.Read()
at System.Xml.XsdValidatingReader.Read()
at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader)
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
--- End of inner exception stack trace ---
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
at NHibernate.Cfg.Configuration.Configure(XmlReader textReader)
at NHibernate.Cfg.Configuration.Configure(String fileName, Boolean ignoreSessionFactoryConfig)
at NHibernate.Cfg.Configuration.Configure()
at DIAmatix.Services.Log.DAL.LogWriterDAL._initNH() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriterDAL\LogWriterDAL.cs:line 42
at DIAmatix.Services.Log.DAL.LogWriterDAL..ctor() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriterDAL\LogWriterDAL.cs:line 66
at DIAmatix.Services.Log.LogWriterIce..ctor() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriter\LogWriter.cs:line 23
at DIAmatix.Services.Log.LogWriterBox.IceBox.ServiceOperationsNC_.start(String name, Communicator communicator, String[] args) in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriter\LogWriter.cs:line 58
at IceBox.ServiceManagerI.startService(String service, String entryPoint, String[] args) in d:\builds\distbuilds\release\Ice-3.3.0\cs\src\IceBox\ServiceManagerI.cs:line 693"
at IceBox.ServiceManagerI.startService(String service, String entryPoint, String[] args) in d:\builds\distbuilds\release\Ice-3.3.0\cs\src\IceBox\ServiceManagerI.cs:line 706
at IceBox.ServiceManagerI.load(String name, String value) in d:\builds\distbuilds\release\Ice-3.3.0\cs\src\IceBox\ServiceManagerI.cs:line 489
at IceBox.ServiceManagerI.run() in d:\builds\distbuilds\release\Ice-3.3.0\cs\src\IceBox\ServiceManagerI.cs:line 338
Caused by: NHibernate.Cfg.HibernateConfigException: An exception occurred during configuration of persistence layer.
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
at NHibernate.Cfg.Configuration.Configure(XmlReader textReader)
at NHibernate.Cfg.Configuration.Configure(String fileName, Boolean ignoreSessionFactoryConfig)
at NHibernate.Cfg.Configuration.Configure()
at DIAmatix.Services.Log.DAL.LogWriterDAL._initNH() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriterDAL\LogWriterDAL.cs:line 42
at DIAmatix.Services.Log.DAL.LogWriterDAL..ctor() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriterDAL\LogWriterDAL.cs:line 66
at DIAmatix.Services.Log.LogWriterIce..ctor() in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriter\LogWriter.cs:line 23
at DIAmatix.Services.Log.LogWriterBox.IceBox.ServiceOperationsNC_.start(String name, Communicator communicator, String[] args) in T:\work\DIAmatixServices\trunk\src\dotnet\services\log\LogWriter\LogWriter.cs:line 58
at IceBox.ServiceManagerI.startService(String service, String entryPoint, String[] args) in d:\builds\distbuilds\release\Ice-3.3.0\cs\src\IceBox\ServiceManagerI.cs:line 693
Caused by: System.IO.FileNotFoundException: Could not find file 'T:\Ice-3.3.0-VC90\bin\hibernate.cfg.xml'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlCharCheckingReader.Read()
at System.Xml.XsdValidatingReader.Read()
at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader)
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)


Name and version of the database you are using: Microsoft SQL Server 2008


Top
 Profile  
 
 Post subject: Using NHibernate in Windows Service
PostPosted: Wed Oct 15, 2008 1:34 pm 
Newbie

Joined: Mon Jun 04, 2007 12:26 pm
Posts: 13
Location: Houston, Texas
By default window services set the default directory to the %WINDIR$\system32. I had the same problem finding the config file for log4net. You need to change your service's default directory to wherever your service is running. Here's a sample:

Code:
// Set current directory to assembly folder
// Need to to this so can find the configuration file for the logger
// default is %WINDIR%\system32 for window services
Environment.CurrentDirectory = System.IO.Path.GetDirectoryName
      (System.Reflection.Assembly.GetEntryAssembly().Location);



Hope that helps.
Diane


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.