Error Injecting interfaces service in Custom Schedule Task

Yowaraj Chhetri asked on October 26, 2020 02:01

Hi there,

Injecting interfaces service to custom schedule task gives me error "Additional assemblies have to be registered before application pre-initialization.".

How do we inject interface and use in custom schedule task?

Recent Answers


Michal Samuhel answered on October 26, 2020 10:19

Hi, how does task look like? What API are you using, where and when is task running? Is it on separate thread? Error is pretty straightforward, but it seems that application is not fully initialized when task is being executed. Lower levels of API are quite layered and it seems that it is running either too early or without application context.

1 votesVote for this answer Mark as a Correct answer

Yowaraj Chhetri answered on October 26, 2020 11:33

Hi Michal,

This is the error I am getting.

Schedule-Task-Error

I have created Custom Schedule Task as a separate class library project. I already have another custom schedule task in that same project and it is running fine as there were no other service/helpers it was refering to.

With my current custom schedule task I have couple of service which I need to use in-order to achieve what I tend to do for this schedule task.

I tried to define those interface into custom schedule task using dependencyResolver as follows, but of no luck.

Schedule-Task

I am using K12 MVC and followed the documentation and already have a another custom schedule task running and working fine.

Thank you

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on October 26, 2020 11:48

Isn't it possible that the custom class library project is referencing some of the Kentico MVC NuGet packages? If yes, you need to remove these references from the class project which is linked in the Kentico admin app project.

0 votesVote for this answer Mark as a Correct answer

Yowaraj Chhetri answered on October 26, 2020 12:56 (last edited on October 26, 2020 13:03)

Hi Juraj,

I dont have any kentico MVC nuget installed in that class library, Following are the references in that class library project.

references

CMSApp project is referencing this class library project and this class library project is referencing another class library project where those services are implemented. CMSApp is not referencing class library project where serviecs are implemented.

The moment I comment out those interfaces references it works fine..

Thank you

0 votesVote for this answer Mark as a Correct answer

Michal Samuhel answered on October 27, 2020 09:19

Hi Yowar,

These do not seem to be our services, but I assume they do use our API, is that correct?

How are you initializing application in general? It seems that when native .NET is trying to resolve dependecies, our libraries are not registered within .NET container. Error is coming from global.asax so I would like to see how application is initialized.

I know for sure, that in version 13 in .Net Core we are registering our container into native .Net container, however for v12 I can not say for sure now how everything comes together.

We do describe how to register our services within other DI containers, namely Autofac, so you may end up with having to use that in order to register our container and be able to resolve our services correctly.

1 votesVote for this answer Mark as a Correct answer

Simon Crowell answered on September 20, 2021 05:49

For anyone else that comes across this.....

I was having the same/similar issue....

  • CMSApp references Class Library A
  • Class Library A references Class Library B
  • Class Library B references Nuget packages (Kentico.Xpereince.AspNet.Mvc5/Kentico.Xpereince.AspNet.Mvc5.Libraries/Kentico.Xpereince.Libraries)

I did not need the MVC5 nuget packages, just the Kentico.Xpereince.Libraries for access to the API. I assume there is some init code within those MVC5 libraries that was doing the same thing that the init within the global asax code of the CMS was doing and causing the problem.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.