CadyIO / hangfire-ravendb

RavenDB job storage for Hangfire
https://www.nuget.org/packages/Hangfire.Raven/
18 stars 35 forks source link

Not working in ASP.Net applications #12

Closed mjohnson0580 closed 7 years ago

mjohnson0580 commented 7 years ago

The latest version doesn't appear to be working in ASP.Net applications.

When making a call to BackgroundJob.Enqueue I am getting the following exception:

System.NullReferenceException
                      Message=Object reference not set to an instance of an object.
                      Source=Hangfire.Raven
                      StackTrace:
                           at Hangfire.Raven.RavenJsonMethodConverter.Read(Type type, Object value, JsonSerializer serializer)
                           at Hangfire.Raven.RavenJsonConverterBase`1.ReadJson(JsonReader reader, Type type, Object existingValue, JsonSerializer serializer)
                           at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue) in C:\Builds\RavenDB-Stable-3.5\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 1771
                           at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) in C:\Builds\RavenDB-Stable-3.5\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 900
                           at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) in C:\Builds\RavenDB-Stable-3.5\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 2005

I believe this is due to Assembly.GetEntryAssembly() returning null on the following line: https://github.com/cady-io/hangfire-ravendb/blob/c65b8b13143302424763c2d5d76eeed68a2e225c/src/Hangfire.Raven/RavenJsonConverters.cs#L90

The same error would also occur here: https://github.com/cady-io/hangfire-ravendb/blob/c65b8b13143302424763c2d5d76eeed68a2e225c/src/Hangfire.Raven/RavenJsonConverters.cs#L107

AustinWinstanley commented 7 years ago

This was a tmp replacement for a problem in AspNetCore that has been fixed now in 2.1.

Although there is a workaround needed for actually passing the function of another assembly. A local function needs to be passed.

So instead of

BackgroundJob.Enqueue(() => Console.WriteLine("Background Job: Hello, world!"));

use

BackgroundJob.Enqueue(() => Test());

[AutomaticRetry(Attempts = 2, LogEvents = true, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public static void Test()
{
    Console.WriteLine("Background Job: Hello, world!"));
}

I have added this as another issue #14