public async Task <TResponse> GetResponseAsync() { var httpRequest = new HttpRequestMessage(HttpMethod.Post, method.FullName); httpRequest.Headers.TryAddWithoutValidation("Accept", "application/grpc-web"); var serializer = method.RequestMarshaller.ContextualSerializer; var serializationContext = new DefaultSerializationContext(); serializer(request, serializationContext); var payloadWithHeader = new byte[serializationContext.Payload.Length + 5]; serializationContext.Payload.CopyTo(payloadWithHeader, 5); // Bytes 1-4 (zero-indexed) should be the payload length WriteInt32BigEndian(serializationContext.Payload.Length, payloadWithHeader, 1); //httpRequest.Content = new StringContent(Convert.ToBase64String(payloadWithHeader)); httpRequest.Content = new ByteArrayContent(payloadWithHeader); httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web"); responseTask = httpClient.SendAsync(httpRequest); var response = await responseTask; var responseBody = await response.Content.ReadAsByteArrayAsync(); if (responseBody == null || responseBody.Length == 0) { return(default);
private static byte[] SerializeInternal <T>(IMarshallerFactory factory, object value) { using (var context = new DefaultSerializationContext()) { factory.CreateMarshaller <T>().ContextualSerializer((T)value, context); return(context.GetContent()); } }
public static byte[] SerializeValue <T>(Marshaller <T> marshaller, T value) { byte[] payload; using (var serializationContext = new DefaultSerializationContext()) { marshaller.ContextualSerializer(value, serializationContext); payload = serializationContext.GetContent(); } return(payload); }
private static T ContextualClone <T>(T value, Marshaller <T> marshaller) { byte[] content; using (var serializationContext = new DefaultSerializationContext()) { marshaller.ContextualSerializer(value, serializationContext); content = serializationContext.GetContent(); } Console.WriteLine("Size: {0}", content.Length); return(marshaller.ContextualDeserializer(new DefaultDeserializationContext(content))); }
private static Metadata SerializeHeader <T>(Marshaller <T> marshaller, string headerName, T headerValue) { byte[] payload; using (var serializationContext = new DefaultSerializationContext()) { marshaller.ContextualSerializer(headerValue, serializationContext); payload = serializationContext.GetContent(); } return(new Metadata { { headerName, payload } }); }
private void MarshallTest <T>(T value) { byte[] content; using (var serializationContext = new DefaultSerializationContext(10 * 1024)) { DataContractMarshaller <T> .Default.ContextualSerializer(value, serializationContext); content = serializationContext.GetContent(); } var actual = DataContractMarshaller <T> .Default.ContextualDeserializer(new DefaultDeserializationContext(content)); actual.ShouldBe(value); }
public void MarshallNull() { byte[] content; using (var serializationContext = new DefaultSerializationContext()) { DataContractMarshaller <string> .Default.ContextualSerializer(null !, serializationContext); content = serializationContext.GetContent(); } var actual = DataContractMarshaller <string> .Default.ContextualDeserializer(new DefaultDeserializationContext(content)); actual.ShouldBeNull(); }
public static object CreateInstance(Dictionary <string, object> values, Type instanceType) { if (instanceType == null) { throw new ArgumentNullException("instanceType"); } if (values == null) { throw new ArgumentNullException("values"); } var context = new DefaultSerializationContext(); var serializer = new ObjectSerializer(context, instanceType); return(serializer.PopulateInstance(values, null)); }