Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Consul.Test/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NuGet.Versioning;
using Xunit;

Expand Down Expand Up @@ -85,6 +87,14 @@ public class BaseFixture : IAsyncLifetime

static BaseFixture()
{
#if !NETFRAMEWORK // It will not compile for .NET Framework due to ambiguity of Newtonsoft.Json types
// We use invalid settings here to make sure that our library doesn't depend on default settings
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
};
#endif

// Some Consul object (e.g. semaphores) use multiple http connections,
// but on .NETFramework the default limit is sometimes very low (2) so we need to bump it to higher value.
// E.g. https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/System.Web/HttpRuntime.cs#L1200
Expand Down
11 changes: 9 additions & 2 deletions Consul/Client_Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public abstract class ConsulRequest
internal Stream ResponseStream { get; set; }
internal string Endpoint { get; set; }

internal readonly JsonSerializer _serializer = new JsonSerializer();
private readonly JsonSerializer _serializer = new JsonSerializer();

internal ConsulRequest(ConsulClient client, string url, HttpMethod method)
{
Expand Down Expand Up @@ -120,6 +120,13 @@ protected TOut Deserialize<TOut>(Stream stream)
}
}

protected static byte[] Serialize(object value) => Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value));
protected byte[] Serialize(object value)
{
using (var sw = new StringWriter())
{
_serializer.Serialize(sw, value);
return Encoding.UTF8.GetBytes(sw.ToString());
}
}
}
}
19 changes: 12 additions & 7 deletions Consul/Semaphore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -267,6 +268,7 @@ internal SemaphoreLock()
private Task _sessionRenewTask;
private Task _monitorTask;

private readonly JsonSerializer _serializer = new JsonSerializer();
internal SemaphoreOptions Opts { get; set; }

public bool IsHeld
Expand Down Expand Up @@ -721,14 +723,17 @@ private SemaphoreLock DecodeLock(KVPair pair)
/// <returns>A K/V pair with the lock data encoded in the Value field</returns>
private KVPair EncodeLock(SemaphoreLock l, ulong oldIndex)
{
var jsonValue = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(l));

return new KVPair(string.Join("/", Opts.Prefix, DefaultSemaphoreKey))
using (var sw = new StringWriter())
{
Value = jsonValue,
Flags = SemaphoreFlagValue,
ModifyIndex = oldIndex
};
_serializer.Serialize(sw, l);
var jsonValue = Encoding.UTF8.GetBytes(sw.ToString());
return new KVPair(string.Join("/", Opts.Prefix, DefaultSemaphoreKey))
{
Value = jsonValue,
Flags = SemaphoreFlagValue,
ModifyIndex = oldIndex
};
}
}

/// <summary>
Expand Down
Loading