internal static IPayload Generate(string subject, IPayload body, string replyTo = null) { var bodySizeString = body.Size.ToString(); var preBodyLen = 3 + 1 + subject.Length + (replyTo?.Length + 1 ?? 0) + 1 + bodySizeString.Length; var preBody = new byte[preBodyLen]; FillPreBody(preBody, subject, bodySizeString, replyTo); var pubCmd = new PayloadBuilder(); pubCmd.Append(preBody); pubCmd.Append(NatsEncoder.CrlfBytes); pubCmd.Append(body); pubCmd.Append(NatsEncoder.CrlfBytes); return(pubCmd.ToPayload()); }
public async Task Given_responder_exists_When_requesting_using_payload_It_should_get_response() { var value = Guid.NewGuid().ToString("N"); var payloadBuilder = new PayloadBuilder(); payloadBuilder.Append(Encoding.UTF8.GetBytes(value)); _responder.Sub("getValue", stream => stream.Subscribe(msg => _responder.Pub(msg.ReplyTo, msg.GetPayloadAsString()))); var response = await _requester.RequestAsync("getValue", payloadBuilder.ToPayload()); response.GetPayloadAsString().Should().Be(value); }
public IPayload Encode <TItem>(TItem item) where TItem : class { EnsureArg.IsNotNull(item, nameof(item)); var builder = new PayloadBuilder(); using (var stream = new MemoryStream()) { Serializer.Serialize(stream, item); builder.Append(stream.ToArray()); } return(builder.ToPayload()); }
public IPayload Encode <TItem>(TItem item) where TItem : class { EnsureArg.IsNotNull(item, nameof(item)); var builder = new PayloadBuilder(); using (var stream = new MemoryStream()) { using (var sw = new StreamWriter(stream, Encoding.UTF8)) { var jw = new JsonTextWriter(sw); _serializer.Serialize(jw, item, typeof(TItem)); } builder.Append(stream.ToArray()); } return(builder.ToPayload()); }