Nhibernate and Windows Service

525 views
Skip to first unread message

Sacha

unread,
Oct 14, 2010, 8:07:35 PM10/14/10
to Fluent NHibernate
I have a Windows service which has a timer and in the timer_Elapsed
event handler a method from another component will be called which is
supposed to look into db and read or update some records.
The timer Elapsed event is getting fired every 2 minutes.I used
nhibernate for data access and all the nhibernate and db related
configuration code is done in the component and windows service has a
reference to the component.When I run the method from unit test it is
working fine which means the configurations and mappings are correct,
but from windows service I get this exception:


FluentNHibernate.Cfg.FluentConfigurationException was caught
Message="An invalid or incomplete configuration was used while
creating a SessionFactory. Check PotentialReasons collection, and
InnerException for more detail.\r\n\r\n"
Source="FluentNHibernate"
StackTrace:
at
FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:
\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line
121
InnerException: NHibernate.MappingException
Message="Could not configure datastore from input stream
(XmlDocument)"
Source="NHibernate"
StackTrace:
at NHibernate.Cfg.Configuration.LogAndThrow(Exception
exception)
at NHibernate.Cfg.Configuration.AddInputStream(Stream
xmlInputStream, String name)
at
NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc, String name)
at
NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc)
at
FluentNHibernate.PersistenceModel.Configure(Configuration cfg) in d:
\Builds\FluentNH\src\FluentNHibernate\PersistenceModel.cs:line 265
at
FluentNHibernate.Cfg.FluentMappingsContainer.Apply(Configuration cfg)
in d:\Builds\FluentNH\src\FluentNHibernate\Cfg
\FluentMappingsContainer.cs:line 141
at
FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg) in
d:\Builds\FluentNH\src\FluentNHibernate\Cfg
\MappingConfiguration.cs:line 64
at
FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:
\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line
112
InnerException: System.TypeInitializationException
Message="The type initializer for
'NHibernate.Util.TypeNameParser' threw an exception."
Source="NHibernate"
TypeName="NHibernate.Util.TypeNameParser"
StackTrace:
at NHibernate.Util.TypeNameParser.Parse(String
type, String defaultNamespace, String defaultAssembly)
at
NHibernate.Cfg.ClassExtractor.ClassEntry..ctor(String extends, String
className, String entityName, String assembly, String namespace)
at
NHibernate.Cfg.ClassExtractor.GetClassEntries(XmlDocument document)
at
NHibernate.Cfg.MappingsQueue.AddDocument(NamedXmlDocument document)
at
NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument
document)
at
NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String
name)
at
NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream,
String name)
InnerException: System.Threading.ThreadAbortException
Message="Exception of type
'System.Threading.ThreadAbortException' was thrown."
InnerException:

Here is the code I have in the service :

public InitiatorService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
timer1.Enabled = true;
}

protected override void OnStop()
{
}
private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
lock (processLock)
{
DoWorkComponent component= new DoWorkComponent ();
component.DoWorkMethod();
}
}

In the DoWorkComponent :

public void DoWorkMethod()
{
try
{
ISession session =GetSession();
ITransaction transaction = session.BeginTransaction();

//code to do actions

session.Flush();
transaction.Commit();
session.Close(); }
catch (Exception ex)
{
throw new InvalidOperationException(ex.Message);
}
}

Please let me know if this is a correct practice and how I can fix
this problem.

The nhibernate version I am using is 2.1.2.4000 and I set CallContext
to thread_static.

Thanks in Advance,

Sacha

Rasmoo

unread,
Oct 15, 2010, 4:46:35 AM10/15/10
to Fluent NHibernate
Maybe this isn't related to Fluent NHibernate. Are you sure that the
service and the component of yours is set up properly? Which user does
the service run as? Will it be able to access relevant assemblies and
configuration files?

If you don't need the additional complexity of services, then try a
scheduled task.

Sacha

unread,
Oct 15, 2010, 7:00:05 AM10/15/10
to Fluent NHibernate
Thanks a lot for your reply.

Originaly the service run under LocalSystem account.I changed it to
run as administrator and the error disappeared! So your concern is
very valid.
I am getting another error though which is related to loading
OracleClient on 64 bit machine.I'll work on that one and see how it
goes.
By the way, considering the short interval for this kind of processes
do you think the windows task scheduler is reliable enough to be used
here?What this process does is checking the db and retrieving some
records, then creating messages and submitting them to msmq. It also
updates some other records in db.
I have 2 other processes which needed to be run every night, I plan
to use task scheduler for those.They actually will need to run
different methods from the same component.
> > Sacha- Hide quoted text -
>
> - Show quoted text -

Graham Hay

unread,
Oct 15, 2010, 4:57:56 AM10/15/10
to fluent-n...@googlegroups.com
Is your SessionFactory set up right? Try using it without the threading.

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.


Sacha

unread,
Oct 17, 2010, 8:13:21 PM10/17/10
to Fluent NHibernate

The SessionFactory is setup corrctly and everything is working fine
now.
The problem as was mentioned in previous replies was with the user
account.

Thanks,
Sacha
> > fluent-nhibern...@googlegroups.com<fluent-nhibernate%2Bunsubscr­i...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/fluent-nhibernate?hl=en.- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages