Could not cast or convert from System.String to System.Type

I am getting “System.ArgumentException: Could not cast or convert from System.String to System.Type.”. I have copied the error below.
I get it while I am enqueuing a job
BackgroundJob.Enqueue(() => _emailSender.SendEmail(“xxxxx”, 25, “xxxx”, “xxxx”, “xxxx”))

Any help on this is appreciated. Thank you very much!

{“FailedAt”:“2018-01-10T19:52:24.0855131Z”,“ExceptionType”:“Newtonsoft.Json.JsonSerializationException”,“ExceptionMessage”:“Error converting value “System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e” to type ‘System.Type’. Path ‘[0]’, line 1, position 107.”,“ExceptionDetails”:“Newtonsoft.Json.JsonSerializationException: Error converting value “System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e” to type ‘System.Type’. Path ‘[0]’, line 1, position 107. —> System.ArgumentException: Could not cast or convert from System.String to System.Type.\r\n at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)\r\n at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)\r\n — End of inner exception stack trace —\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)\r\n at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)\r\n at Hangfire.Common.JobHelper.FromJson[T](String value)\r\n at Hangfire.Storage.InvocationData.Deserialize()”}

I am getting error on System defined types. When I pass custom defined type, it works:

_backgroundJobClient.Enqueue(() => _emailSender.SendEmail(emailParams)); <- This works.
_backgroundJobClient.Enqueue(() => _emailSender.SendEmail(“string”)); <- This doesn’t work.

Any insights from expert on this is highly appreciated. Thank you!

Having same issue.
With hangfire latest version

We are also facing issues with this. Has anyone in this thread made any progress or reached any conclusions. We are using .net core 2.

This appears to happen in a non deterministic fashion. Some jobs fail many times with this error then pass.

One common theme is that all jobs initially fail with a transient error when making an http call. The subsequent retries that DO fail, fail with this error.

Error converting value "MarketingPreferences.Host.Events.Vortex.PrivacyServiceGetConsent, MarketingPreferences.Host, Version=1.8.3.0, Culture=neutral, PublicKeyToken=null" to type 'System.Type'. Path '[0]', line 1, position 149.

Newtonsoft.Json.JsonSerializationException: Error converting value "MarketingPreferences.Host.Events.Vortex.PrivacyServiceGetConsent, MarketingPreferences.Host, Version=1.8.3.0, Culture=neutral, PublicKeyToken=null" to type 'System.Type'. Path '[0]', line 1, position 149. ---> System.ArgumentException: Could not cast or convert from System.String to System.Type.
   at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
   at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
   --- End of inner exception stack trace ---
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Hangfire.Common.JobHelper.FromJson[T](String value)
   at Hangfire.Storage.InvocationData.Deserialize()

I am also experiencing this issue. I have both client and server in the same solution. I have Cleaned the Solution and Rebuilt the solution, and still it intermittently fails as above: trying to convert the argument for the method being executed to System.Type. No solution at this stage.

1 Like

I had this issue to. I’m glad I found this post, this was driving me nuts. It appears if I use simple type (string) arguments to the delegate of the BackgoundJobClient.Enqueue() method, I get the “cannot convert string to type” error. If change my arguments to using a custom complex type, it works.

I haven’t had time to test this issue but has anyone tried an explicit cast:

_backgroundJobClient.Enqueue(() => _emailSender.SendEmail((string)“string”));

This might be a bug but I’d be interested to see if there is an easy workaround to it by playing with the casting.