-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathCreatingContext.cs
83 lines (77 loc) · 3.33 KB
/
CreatingContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// This file is part of Hangfire.
// Copyright © 2013-2014 Sergey Odinokov.
//
// Hangfire is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3
// of the License, or any later version.
//
// Hangfire is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with Hangfire. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace Hangfire.Client
{
/// <summary>
/// Provides the context for the <see cref="IClientFilter.OnCreating"/>
/// method of the <see cref="IClientFilter"/> interface.
/// </summary>
public class CreatingContext : CreateContext
{
public CreatingContext(CreateContext context)
: base(context)
{
}
/// <summary>
/// Gets or sets a value that indicates that this <see cref="CreatingContext"/>
/// object was canceled.
/// </summary>
public bool Canceled { get; set; }
/// <summary>
/// Sets the job parameter of the specified <paramref name="name"/>
/// to the corresponding <paramref name="value"/>. The value of the
/// parameter is serialized to a JSON string.
/// </summary>
///
/// <param name="name">The name of the parameter.</param>
/// <param name="value">The value of the parameter.</param>
///
/// <exception cref="ArgumentNullException">The <paramref name="name"/> is null or empty.</exception>
public void SetJobParameter(string name, object value)
{
if (String.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(nameof(name));
Parameters[name] = value;
}
/// <summary>
/// Gets the job parameter of the specified <paramref name="name"/>
/// if it exists. The parameter is deserialized from a JSON
/// string value to the given type <typeparamref name="T"/>.
/// </summary>
///
/// <typeparam name="T">The type of the parameter.</typeparam>
/// <param name="name">The name of the parameter.</param>
/// <returns>The value of the given parameter if it exists or null otherwise.</returns>
///
/// <exception cref="ArgumentNullException">The <paramref name="name"/> is null or empty.</exception>
/// <exception cref="InvalidOperationException">Could not deserialize the parameter value to the type <typeparamref name="T"/>.</exception>
public T GetJobParameter<T>(string name)
{
if (String.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(nameof(name));
try
{
return Parameters.TryGetValue(name, out var parameter)
? (T)parameter
: default(T);
}
catch (Exception ex)
{
throw new InvalidOperationException(
$"Could not get a value of the job parameter `{name}`. See inner exception for details.", ex);
}
}
}
}