I’m trying to set together a system that uses Hangfire with Unity DI framework.
I get this error when the job is running:
Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = “lms.services.LMSEnvironmentServices”, name = “(none)”.
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, lms.database.UnitOfWork.IUnitOfWork, is an interface and cannot be constructed. Are you missing a type mapping?
I’ll try to eplain the setup. I’m a novice in DI, basically I’m trying to follow best-practice implementation even if I don’t understand all of it.:
Database (this uses _unitOfWork)
Services (this has _unitOfWork in constructor)
Resolver (has componenetloader that finds all dependencies)
MVC application (this knows about the services but no _unitOfWork)
I’m trying to call a simple service as a backgroundjob.
I also want to update a local table to keep a per-user list of background jobs
var hjobid= BackgroundJob.Enqueue<LMSEnvironmentServices>(c => c.slowLog((int)loggedInUser.UserID));
var userJob = new JobEntity
{
UserId = loggedInUser.UserID,
HangfireJobId = System.Convert.ToInt32(hjobid),
};
_jobServices.CreateJob(userJob);
So I’m using LMSEnvironmentServices and jobServices. First is doing the work, second is keeping track of the work.
I have installed Hangfire.Unity
My startup.cs looks like this:
GlobalConfiguration.Configuration.UseStorage(
new MySqlStorage(ConfigurationManager.ConnectionStrings["Hangfire"].ConnectionString)
);
var hangfireContainer = new UnityContainer();
GlobalConfiguration.Configuration.UseActivator(new UnityJobActivator(hangfireContainer));
There is a UnityWebApiActivator that initiates my three projects
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion
public static void RegisterTypes(IUnityContainer container)
{
ComponentLoader.LoadContainer(container, ".\\bin", "LMSAPI.dll");
ComponentLoader.LoadContainer(container, ".\\bin", "lms.services.dll");
ComponentLoader.LoadContainer(container, ".\\bin", "lms.database.dll");
}
}
My question is:
How can I get rid of the error regarding _unitOfWork while still keeping the design that backgroundjob’s should now know about the databaselayer?
It seems that the background jobs have a completely different setup-system compared to the actual runtime, is this correct?
Does anybody have a complete sample project for reference as to what a complete DI setup should look like?