Beispiel #1
0
        public virtual Task ExecuteAsync(WebSocketContext context, WebSocketObjectResult result)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }

            // Select all the formatters that can be used
            var writeContext = new WebSocketOutputFormatterCanWriteContext
            {
                ContentType = new StringSegment(),
                ObjectType  = result.Value.GetType()
            };

            var selectedFormatters = OutputFormatters.Where(x => x.CanWriteResult(writeContext));

            if (!selectedFormatters.Any())
            {
                return(TaskCache.CompletedTask);
            }

            // Select the first and write the result
            // TODO: Figure out a better way to determine the right formatter?
            return(selectedFormatters.First().WriteResultAsync(context, result.Value, Encoding.UTF8));
        }
 public bool CanWriteResult(WebSocketOutputFormatterCanWriteContext context)
 {
     return(context.ObjectType == typeof(string));
 }