Skip to content

Commit

Permalink
Throw NotSupported when job contains delegate or expression
Browse files Browse the repository at this point in the history
  • Loading branch information
odinserj committed Dec 2, 2016
1 parent cc6b49b commit 14c6b79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Hangfire.Core/Common/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,14 @@ private static void Validate(
throw new NotSupportedException(
"Parameters, passed by reference, are not supported: there is no guarantee that specified method will be invoked inside the same process.");
}

var parameterTypeInfo = parameter.ParameterType.GetTypeInfo();

if (parameterTypeInfo.IsSubclassOf(typeof(Delegate)) || parameterTypeInfo.IsSubclassOf(typeof(Expression)))
{
throw new NotSupportedException(
"Anonymous functions, delegates and lambda expressions aren't supported in job method parameters: it's very hard to serialize them and all their scope in general.");
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Hangfire.Core.Tests/Common/JobFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Newtonsoft.Json;
using Xunit;

// ReSharper disable LocalizableElement
// ReSharper disable AssignNullToNotNullAttribute

#pragma warning disable 618
Expand Down Expand Up @@ -308,6 +309,20 @@ public void Ctor_ThrowsAnException_WhenMethodIsNotPublic()
() => Job.FromExpression(() => PrivateMethod()));
}

[Fact]
public void Ctor_ThrowsAnException_WhenMethodParametersContainADelegate()
{
Assert.Throws<NotSupportedException>(
() => Job.FromExpression(() => DelegateMethod(() => Console.WriteLine("Hey delegate!"))));
}

[Fact]
public void Ctor_ThrowsAnException_WhenMethodParametersContainAnExpression()
{
Assert.Throws<NotSupportedException>(
() => Job.FromExpression(() => ExpressionMethod(() => Console.WriteLine("Hey expression!"))));
}

[Fact]
public void Perform_ThrowsAnException_WhenActivatorIsNull()
{
Expand Down Expand Up @@ -689,6 +704,14 @@ public async void AsyncVoidMethod()
await Task.Yield();
}

public void DelegateMethod(Action action)
{
}

public void ExpressionMethod(Expression<Action> expression)
{
}

public interface ICommandDispatcher
{
void DispatchTyped<TCommand>(TCommand command);
Expand Down

0 comments on commit 14c6b79

Please sign in to comment.