Skip to content

Commit

Permalink
Remove use of PushStreamContent
Browse files Browse the repository at this point in the history
Fixes hashicorp#41

There appears to have been some users that don't have the
`PushStreamContent` class so the serialization has been changed to
pre-serialize everything and then just push the data as a byte array to
the Consul agent.
  • Loading branch information
highlyunavailable committed Feb 19, 2016
1 parent ccebafb commit c5e5bb3
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions Consul/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public class WriteResult<T> : WriteResult
/// <summary>
/// Represents a persistant connection to a Consul agent. Instances of this class should be created rarely and reused often.
/// </summary>
public partial class ConsulClient :IDisposable
public partial class ConsulClient : IDisposable
{
private object _lock = new object();
internal HttpClient HttpClient { get; set; }
Expand Down Expand Up @@ -565,16 +565,9 @@ protected TOut Deserialize<TOut>(Stream stream)
}
}

protected void Serialize(object value, Stream stream)
protected byte[] Serialize(object value)
{
using (var writer = new StreamWriter(stream))
{
using (var jsonWriter = new JsonTextWriter(writer))
{
Client.serializer.Serialize(jsonWriter, value);
jsonWriter.Flush();
}
}
return System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value));
}
}
public class GetRequest<T> : ConsulRequest
Expand Down Expand Up @@ -945,7 +938,7 @@ public async Task<WriteResult> Execute(CancellationToken ct)
}
else
{
content = new PushStreamContent((stream, httpContent, transportContext) => { Serialize(_body, stream); });
content = new ByteArrayContent(Serialize(_body));
}

var response = await Client.HttpClient.PutAsync(BuildConsulUri(Endpoint, Params), content, ct).ConfigureAwait(false);
Expand Down Expand Up @@ -1028,7 +1021,7 @@ public async Task<WriteResult<TOut>> Execute(CancellationToken ct)
}
else
{
content = new PushStreamContent((stream, httpContent, transportContext) => { Serialize(_body, stream); });
content = new ByteArrayContent(Serialize(_body));
}

var response = await Client.HttpClient.PutAsync(BuildConsulUri(Endpoint, Params), content, ct).ConfigureAwait(false);
Expand Down Expand Up @@ -1117,7 +1110,7 @@ public async Task<WriteResult<TOut>> Execute(CancellationToken ct)
}
else
{
content = new PushStreamContent((stream, httpContent, transportContext) => { Serialize(_body, stream); });
content = new ByteArrayContent(Serialize(_body));
}

var response = await Client.HttpClient.PostAsync(BuildConsulUri(Endpoint, Params), content, ct).ConfigureAwait(false);
Expand Down

0 comments on commit c5e5bb3

Please sign in to comment.