I am trying to create a generic controller that receives a job name string and a .json and parses it into the correct “job” type given the job name.
Here’s what the controller looks like:
public void GenericController([FromBody] object input) { var obj = JsonConvert.DeserializeObject(input.ToString(), Type.GetType("Job1")); BackgroundJob.Enqueue(() => obj.GetType().GetMethod("Execute").Invoke(obj, null)); }
Note that “Job1” is a placeholder that will later be replaced with a string sent with/in “input”. Also, the “Execute” method is just Console.WriteLine(“hi”).
Here’s the error message:
System.MissingMethodException
No parameterless constructor defined for this object.
System.MissingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at Hangfire.JobActivator.ActivateJob(Type jobType) at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type) at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context) at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_0.<PerformJobWithFilters>b__0() at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func
1 continuation)
at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_1.b__2()
at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable1 filters) at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context) at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)
Any ideas why this happens?
Also, is there a simpler/less verbose way to do what I’m trying to do?