简体   繁体   中英

Hangfire in separate Class Library .dll that is called from ASP.Net MVC5

I have a Class Library DLL called AppCore which stores all database related access and contains all application logic.

This Library is consumed by an ASP.Net (MVC5) application that uses the library for all DB access (through exposed methods) and application specific logic (the Entity Framework or DBContext is not exposed out of the DLL).

AppCore has a few methods which can take a few minutes to run. These methods need to be called from the ASP.Net application.

I think we all know that long running calls in ASP.Net are all: "No, don't do it, it will suck type of thing and you will curse for doing it later".

Therefore I am looking into Hangfire.io which is recommended by among others Scott Hanselman for long running ASP.Net calls.

Now my question here is that if anyone knows how to implement Hangfire in a Class library DLL that will, in turn, be consumed by an ASP.Net application. The AppCore.dll has the DbContext in it, and it has the long-running methods. But the calls will be coming form ASP.Net which has no direct knowledge of the database or the long running code logic.

Edit: The best way I can think of doing this is installing Hangfire in the ASP.Net application and set up a separate DBContext just for this there. I would, however, prefer to have it all in the AppCore DLL and not spread logic around.

my question here is that if anyone knows how to implement Hangfire in a Class library dll that will in turn be consumed by an ASP.Net application

You do not need to implement hangfire in the code which will eventually be executed by hangfire.

Hangfire will run in-process in ASP.net and will durably execute the specified code utilising late binding.

The code being executed has absolutely no idea who is executing it, nor does it care.

In your case, this means that where you would ordinarily call into your appcore.dll, you simply replace this with a call to hangfire to queue and then execute the task.

Let's say your appcore.dll exposes the following operation:

interface IDoSomethingBig
{
    void DoSomethingBig();
}

You can tell hangfire to execute this by:

BackgroundJob.Enqueue<IDoSomethingBig>(x => x.DoSomethingBig());  

Any dependencies which appcore.dll has which need to be satisfied will be resolved and queued alongside the task as execution context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM