-
Notifications
You must be signed in to change notification settings - Fork 9
/
CustomHeaders.cs
71 lines (62 loc) · 2.13 KB
/
CustomHeaders.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace ServiceFabric.Remoting.CustomHeaders
{
/// <inheritdoc />
/// <summary>
/// Custom headers passed on using remoting calls
/// </summary>
[Serializable]
public class CustomHeaders : Dictionary<string, object>
{
internal const string CustomHeader = "x-fabric-headers";
internal const string ReservedHeaderServiceUri = "x-fabric-service";
/// <summary>
/// Creates a new instance
/// </summary>
public CustomHeaders()
{
}
/// <summary>
/// Creates a new instance
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/></param>
/// <param name="context">The <see cref="StreamingContext"/></param>
protected CustomHeaders(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
/// <summary>
/// Create a new instance based on the current <see cref="RemotingContext"/>
/// </summary>
/// <returns>An instance of <see cref="CustomHeaders"/> with headers populated using the current <see cref="RemotingContext"/></returns>
public static CustomHeaders FromRemotingContext()
{
var customHeader = new CustomHeaders();
foreach (var key in RemotingContext.Keys)
{
customHeader.Add(key, RemotingContext.GetData(key));
}
return customHeader;
}
internal byte[] Serialize()
{
using (var stream = new MemoryStream())
{
var bf = new BinaryFormatter();
bf.Serialize(stream, this);
return stream.ToArray();
}
}
internal static CustomHeaders Deserialize(byte[] data)
{
using (var stream = new MemoryStream(data))
{
var bf = new BinaryFormatter();
return (CustomHeaders)bf.Deserialize(stream);
}
}
}
}