So what I’m trying to do is an Orchard Feature that if enabled, runs a separate thread (service) that queries IRepository<> of some PartRecord.
About Starting the service:
I tried starting the service on IFeatureEventHandler.Enabled(), but this gets executed only on enabling the feature, not when Orchard is started.
So i looked in the Orchard framework for anything that i can use and i found IOrchardShellEvents.Activated().
So I basicly did this:
public class MyService : IOrchardShellEvents {
...More stuff...
public void Activated() {
running = true;
//Run DoWork() in separate thread
}
public void Terminating() {
running = false;
}
private void DoWork(){
//do service work while running = true
}
}
This happened to work, but I’m not sure if this is the common practice for starting a custom defined thread when Orchard starts. So please correct me if it’s not done like this..
About Repository querying problem:
The repository gets injected and at first it queries the table just fine. After a while tho, it throws an exception saying that: "Multiple simultaneous connections or connections with different connection strings inside the same transaction are not currently supported.".
It seems extremely bizzare, that a query that get executed a couple of times crashes after a while;
Here’s the code for the shows how i use the repository:
public MyService(ServiceManager manager, IRepository<SomePartRecord> repo) {
this.manager = manager;
//The manager of the service uses the repository to get a single column(ExpectaId, not a PK) out of each row
manager.LoadIds = () =>
repo.Table.ToList().Select(record => record.ExpectaId);
}
Note: The Func<> manager.LoadIds is called once per 10 seconds
Note: I’m using MySql Server 5.5
OK, so the answer to any question beginning with “how do I spin a separate thread in order to…” is “don’t”. Seriously. See for example http://ayende.com/blog/158945/thou-shall-not-do-threading-unless-you-know-what-you-are-doing
Fortunately, Orchard provides a way to run tasks in the background without having to spin your own threads: How to run scheduled tasks in Orchard?