/// <summary> /// Attaches a <see cref="StreamingRequestHandler"/> to process requests via the connected named pipe /// and begins listening for incoming traffic. /// </summary> /// <param name="bot">The bot to use when processing messages.</param> /// <param name="middleware">The middleware the bot will execute as part of the pipeline.</param> /// <param name="onTurnError">Callback to execute when an error occurs while executing the pipeline.</param> public void InitializeNamedPipeServer(IBot bot, IList <IMiddleware> middleware = null, Func <ITurnContext, Exception, Task> onTurnError = null) { if (bot == null) { throw new ArgumentNullException(nameof(bot)); } middleware = middleware ?? new List <IMiddleware>(); var handler = new StreamingRequestHandler(onTurnError, bot, middleware); StartServer(handler); }
private async Task CreateStreamingServerConnectionAsync(Func <ITurnContext, Exception, Task> onTurnError, List <Builder.IMiddleware> middlewareSet, HttpContext httpContext) { var handler = new StreamingRequestHandler(onTurnError, httpContext.RequestServices, middlewareSet); var socket = await httpContext.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false); try { await handler.StartAsync(socket).ConfigureAwait(false); } catch (Exception ex) { httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; await httpContext.Response.WriteAsync("Unable to create transport server.").ConfigureAwait(false); throw ex; } }
private void StartServer(StreamingRequestHandler handler) { try { Task.Run(() => handler.StartAsync(_pipeName)); } catch (Exception ex) { /* The inability to establish a named pipe connection is not a terminal condition, * and should not interrupt the bot's initialization sequence. We log the failure * as informative but do not throw an exception or cause a disruption to the bot, * as either would require developers to spend time and effort on a feature they * may not care about or intend to make use of. * As our support for named pipe bots evolves we will likely be able to restrict * connection attempts to when they're likely to succeed, but for now it's possible * a bot will check for a named pipe connection, find that one does not exist, and * simply continue to serve as an HTTP and/or WebSocket bot, none the wiser. */ _logger.LogInformation(string.Format("Failed to create server: {0}", ex)); } }