A feature with a background-running thread in Orchard

Topics: Core, Customizing Orchard, Writing modules
Dec 8, 2012 at 7:51 PM

So what I'm trying to do is an Orchard Feature that if enabled, runs a separate thread (service) that queriesIRepository<> 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

Coordinator
Dec 8, 2012 at 11:06 PM

This was cross-posted from SO. Please don't do that. I answered there: http://stackoverflow.com/questions/13778217/a-feature-with-a-background-running-thread-in-orchard/13782615#13782615