public async Task <Message> InvokeAsync(Message msg) { try { var ti0 = DateTime.Now; WriteFlowerLine(); Console.WriteLine("In InvokeAsync:"); var context = WebOperationContext.Current; var request = BuildForwardedRequest(context, msg); Console.WriteLine("...calling {0}", request.RequestUri); HttpResponseMessage response; using (var client = new HttpClient()) { response = await client.SendAsync(request, CancellationToken.None); } Console.WriteLine("...and done {0:N0} ms...", DateTime.Now.Subtract(ti0).TotalMilliseconds); Console.WriteLine("...reading and creating response..."); CopyHttpResponseMessageToOutgoingResponse(response, context.OutgoingResponse); var stream = response.Content != null ? await response.Content.ReadAsStreamAsync() : null; var message = StreamMessageHelper.CreateMessage(MessageVersion.None, "GETRESPONSE", stream ?? new MemoryStream()); Console.WriteLine("...and done (total time: {0:N0} ms).", DateTime.Now.Subtract(ti0).TotalMilliseconds); return(message); } catch (Exception ex) { WriteException(ex); throw; } }
public async Task <Message> InvokeAsync(Message msg) { var context = WebOperationContext.Current; object value; Stream s = null; if (msg.Properties.TryGetValue("WebBodyFormatMessageProperty", out value)) { var prop = value as WebBodyFormatMessageProperty; if (prop != null && (prop.Format == WebContentFormat.Json || prop.Format == WebContentFormat.Raw)) { s = StreamMessageHelper.GetStream(msg); } } else { var ms = new MemoryStream(); using (var xw = XmlDictionaryWriter.CreateTextWriter(ms, Encoding.UTF8, false)) { msg.WriteBodyContents(xw); } ms.Seek(0, SeekOrigin.Begin); s = ms; } var request = MakeHttpRequestMessageFrom(context.IncomingRequest, s, _config.BufferRequestContent); var response = await _serverInvoker.SendAsync(request, CancellationToken.None); CopyHttpResponseMessageToOutgoingResponse(response, context.OutgoingResponse); var stream = response.Content != null ? await response.Content.ReadAsStreamAsync() : null; return(StreamMessageHelper.CreateMessage(MessageVersion.None, "GETRESPONSE", stream ?? new MemoryStream())); }
public Message EndInvoke(IAsyncResult ar) { var t = ar as Task <Stream>; var stream = t.Result; return(StreamMessageHelper.CreateMessage(MessageVersion.None, "GETRESPONSE", stream ?? new MemoryStream())); }
public async Task <Message> GetAsync() { var context = WebOperationContext.Current; var request = MakeHttpRequestMessageFrom(context.IncomingRequest, null, _config.BufferRequestContent); var response = await _serverInvoker.SendAsync(request, CancellationToken.None); CopyHttpResponseMessageToOutgoingResponse(response, context.OutgoingResponse); var stream = response.Content != null ? await response.Content.ReadAsStreamAsync() : null; return(StreamMessageHelper.CreateMessage(MessageVersion.None, "GETRESPONSE", stream ?? new MemoryStream())); }
System.ServiceModel.Channels.Message IRESTLightswitch.GetLightswitchState() { System.ServiceModel.Channels.Message response = StreamMessageHelper.CreateMessage(OperationContext.Current.IncomingMessageVersion, "GETRESPONSE", this.WriteImageToStream); HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty(); responseProperty.Headers.Add("Content-Type", "image/jpeg"); response.Properties.Add(HttpResponseMessageProperty.Name, responseProperty); Console.WriteLine("Sent Light switch state to the caller"); return(response); }
public async Task <Message> GetAsync() { var webOperContext = WebOperationContext.Current; var ms = new MemoryStream(); var onSendingHeadersHandlers = new List <Tuple <Action <object>, object> >(); var owinContext = MakeOwinContextFrom(webOperContext.IncomingRequest, Stream.Null, ms, onSendingHeadersHandlers); await _next.Invoke(owinContext.Environment); onSendingHeadersHandlers.Reverse(); foreach (var t in onSendingHeadersHandlers) { t.Item1(t.Item2); } CopyOwinContextToOutgoingResponse(owinContext, webOperContext.OutgoingResponse); ms.Seek(0, SeekOrigin.Begin); return(StreamMessageHelper.CreateMessage(MessageVersion.None, "GETRESPONSE", ms)); }
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { if (channel.State != CommunicationState.Opened) { throw new InvalidOperationException(); } // local cancellation source dependent on outer token that nukes the channel // on abort. That renders the channel unusable for further operations while a // timeout expiry might not. using (var ct = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken)) { ct.Token.Register(() => channel.Abort()); // create and send message var msg = StreamMessageHelper.CreateMessage(MessageVersion.Default, string.Empty, new MemoryStream(buffer, offset, count)); msg.Headers.Action = null; return(Task.Factory.FromAsync(channel.BeginSend, channel.EndSend, msg, TimeSpan.FromMilliseconds(WriteTimeout), null)); } }
public async Task <Message> InvokeAsync(Message msg) { var webOperContext = WebOperationContext.Current; object value; Stream s = null; if (msg.Properties.TryGetValue("WebBodyFormatMessageProperty", out value)) { var prop = value as WebBodyFormatMessageProperty; if (prop != null && (prop.Format == WebContentFormat.Json || prop.Format == WebContentFormat.Raw)) { s = StreamMessageHelper.GetStream(msg); } } else { var ms = new MemoryStream(); using (var xw = XmlDictionaryWriter.CreateTextWriter(ms, Encoding.UTF8, false)) { msg.WriteBodyContents(xw); } ms.Seek(0, SeekOrigin.Begin); s = ms; } var outputStream = new MemoryStream(); var onSendingHeadersHandlers = new List <Tuple <Action <object>, object> >(); var owinContext = MakeOwinContextFrom(webOperContext.IncomingRequest, s, outputStream, onSendingHeadersHandlers); await _next.Invoke(owinContext.Environment); onSendingHeadersHandlers.Reverse(); foreach (var t in onSendingHeadersHandlers) { t.Item1(t.Item2); } outputStream.Seek(0, SeekOrigin.Begin); CopyOwinContextToOutgoingResponse(owinContext, webOperContext.OutgoingResponse); return(StreamMessageHelper.CreateMessage(MessageVersion.None, "GETRESPONSE", outputStream)); }