public static async Task RenderToStreamAsync(this PageResult pageResult, Stream stream) { try { if (pageResult.ResultOutput != null) { if (pageResult.LastFilterError != null) { throw new ScriptException(pageResult); } await stream.WriteAsync(MemoryProvider.Instance.ToUtf8Bytes(pageResult.ResultOutput.AsSpan())); return; } await pageResult.Init(); await pageResult.WriteToAsync(stream); if (pageResult.LastFilterError != null) { throw new ScriptException(pageResult); } } catch (Exception e) { if (ShouldRethrow(e)) { throw; } throw HandleException(e, pageResult); } }
public static async Task RenderAsync(this PageResult pageResult, Stream stream, CancellationToken token = default) { if (pageResult.ResultOutput != null) { await stream.WriteAsync(MemoryProvider.Instance.ToUtf8Bytes(pageResult.ResultOutput.AsSpan()), token : token); return; } await pageResult.Init(); await pageResult.WriteToAsync(stream, token); if (pageResult.LastFilterError != null) { throw new ScriptException(pageResult); } }
public override async Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token) { var argValue = block.Argument.GetJsExpressionAndEvaluate(scope); var args = argValue as Dictionary <string, object> ?? new Dictionary <string, object>(); var format = scope.Context.PageFormats.First().Extension; if (args.TryGetValue(ScriptConstants.Format, out var oFormat)) { format = oFormat.ToString(); args.Remove(ScriptConstants.Format); } var htmlDecode = false; if (args.TryGetValue(nameof(htmlDecode), out var oHtmlDecode) && oHtmlDecode is bool b) { htmlDecode = b; args.Remove(nameof(htmlDecode)); } var context = scope.CreateNewContext(args); var unrenderedBody = new SharpPartialPage(scope.Context, "eval-page", block.Body, format, args); using (var ms = MemoryStreamFactory.GetStream()) { var captureScope = scope.ScopeWith(outputStream: ms, scopedParams: args); await scope.PageResult.WritePageAsync(unrenderedBody, captureScope, token); var renderedBody = await ms.ReadToEndAsync(); if (htmlDecode) { renderedBody = renderedBody.HtmlDecode(); } var pageResult = new PageResult(context.OneTimePage(renderedBody)) { Args = args, }; await pageResult.WriteToAsync(scope.OutputStream, token); } }
public static void RenderToStream(this PageResult pageResult, Stream stream) { try { try { if (pageResult.ResultOutput != null) { if (pageResult.LastFilterError != null) { throw new ScriptException(pageResult); } stream.WriteAsync(MemoryProvider.Instance.ToUtf8Bytes(pageResult.ResultOutput.AsSpan())).Wait(); return; } pageResult.Init().Wait(); pageResult.WriteToAsync(stream).Wait(); if (pageResult.LastFilterError != null) { throw new ScriptException(pageResult); } } catch (AggregateException e) { var ex = e.UnwrapIfSingleException(); throw ex; } } catch (Exception e) { if (ShouldRethrow(e)) { throw; } throw HandleException(e, pageResult); } }
public static async Task <Tuple <bool, object> > EvaluateResultAsync(this PageResult pageResult) { try { await pageResult.WriteToAsync(Stream.Null); if (pageResult.LastFilterError != null) { throw new ScriptException(pageResult); } return(new Tuple <bool, object>(pageResult.ReturnValue != null, pageResult.ReturnValue?.Result)); } catch (Exception e) { if (ShouldRethrow(e)) { throw; } throw HandleException(e, pageResult); } }
public static bool EvaluateResult(this PageResult pageResult, out object returnValue) { try { pageResult.WriteToAsync(Stream.Null).Wait(); if (pageResult.LastFilterError != null) { throw new ScriptException(pageResult); } returnValue = pageResult.ReturnValue?.Result; return(pageResult.ReturnValue != null); } catch (Exception e) { if (ShouldRethrow(e)) { throw; } throw HandleException(e, pageResult); } }