public void CreateStreamingRequestHandler(WebSocket socket, Activity activity, string audience = null)
            {
                var srh = new StreamingRequestHandler(ConnectedBot, this, socket, audience, Logger);

                // Prepare StreamingRequestHandler for BotFrameworkHttpAdapterBase.ProcessStreamingActivityAsync()
                // Add ConversationId to StreamingRequestHandler's conversations cache
                var cacheField = typeof(StreamingRequestHandler).GetField("_conversations", BindingFlags.NonPublic | BindingFlags.Instance);
                var cache      = (ConcurrentDictionary <string, DateTime>)cacheField.GetValue(srh);

                cache.TryAdd(activity.Conversation.Id, DateTime.Now);

                // Add ServiceUrl to StreamingRequestHandler
                var serviceUrlProp = typeof(StreamingRequestHandler).GetProperty("ServiceUrl");

                serviceUrlProp.DeclaringType.GetProperty("ServiceUrl");
                serviceUrlProp.GetSetMethod(true).Invoke(srh, new object[] { activity.ServiceUrl });

                if (RequestHandlers != null)
                {
                    RequestHandlers.Add(srh);
                    return;
                }

                RequestHandlers = new List <StreamingRequestHandler> {
                    srh
                };
            }
Пример #2
0
        /// <summary>
        /// Process the initial request to establish a long lived connection via a streaming server.
        /// </summary>
        /// <param name="bot">The <see cref="IBot"/> instance.</param>
        /// <param name="httpRequest">The connection request.</param>
        /// <param name="httpResponse">The response sent on error or connection termination.</param>
        /// <returns>Returns on task completion.</returns>
        private async Task ConnectWebSocketAsync(IBot bot, HttpRequest httpRequest, HttpResponse httpResponse)
        {
            if (httpRequest == null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }

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

            ConnectedBot = bot ?? throw new ArgumentNullException(nameof(bot));

            if (!httpRequest.HttpContext.WebSockets.IsWebSocketRequest)
            {
                httpRequest.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                await httpRequest.HttpContext.Response.WriteAsync("Upgrade to WebSocket is required.").ConfigureAwait(false);

                return;
            }

            var claimsIdentity = await AuthenticateRequestAsync(httpRequest).ConfigureAwait(false);

            if (claimsIdentity == null)
            {
                httpRequest.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                await httpRequest.HttpContext.Response.WriteAsync("Request authentication failed.").ConfigureAwait(false);

                return;
            }

            try
            {
                var socket = await httpRequest.HttpContext.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);

                // Set ClaimsIdentity on Adapter to enable Skills and User OAuth in WebSocket-based streaming scenarios.
                var audience = GetAudience(claimsIdentity);

                var requestHandler = CreateStreamingRequestHandler(bot, socket, audience);

                if (RequestHandlers == null)
                {
                    RequestHandlers = new List <StreamingRequestHandler>();
                }

                RequestHandlers.Add(requestHandler);

                await requestHandler.ListenAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                httpRequest.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                await httpRequest.HttpContext.Response.WriteAsync($"Unable to create transport server. Error: {ex.ToString()}").ConfigureAwait(false);

                throw;
            }
        }
Пример #3
0
        /// <summary>
        /// Process the initial request to establish a long lived connection via a streaming server.
        /// </summary>
        /// <param name="bot">The <see cref="IBot"/> instance.</param>
        /// <param name="httpRequest">The connection request.</param>
        /// <param name="httpResponse">The response sent on error or connection termination.</param>
        /// <returns>Returns on task completion.</returns>
        private async Task ConnectWebSocketAsync(IBot bot, HttpRequestMessage httpRequest, HttpResponseMessage httpResponse)
        {
            if (httpRequest == null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }

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

            ConnectedBot = bot ?? throw new ArgumentNullException(nameof(bot));

            if (HttpContext.Current.IsWebSocketRequest || HttpContext.Current.IsWebSocketRequestUpgrading)
            {
                httpResponse.StatusCode = HttpStatusCode.BadRequest;
                httpResponse.Content    = new StringContent("Upgrade to WebSocket is required.");

                return;
            }

            if (!await AuthenticateRequestAsync(httpRequest, httpResponse).ConfigureAwait(false))
            {
                httpResponse.StatusCode = HttpStatusCode.Unauthorized;
                httpResponse.Content    = new StringContent("Request authentication failed.");

                return;
            }

            try
            {
                HttpContext.Current.AcceptWebSocketRequest(async context =>
                {
                    var requestHandler = new StreamingRequestHandler(bot, this, context.WebSocket, Logger);

                    if (RequestHandlers == null)
                    {
                        RequestHandlers = new List <StreamingRequestHandler>();
                    }

                    RequestHandlers.Add(requestHandler);

                    await requestHandler.ListenAsync().ConfigureAwait(false);
                });
            }
            catch (Exception ex)
            {
                httpResponse.StatusCode = HttpStatusCode.InternalServerError;
                httpResponse.Content    = new StringContent($"Unable to create transport server. Error: {ex.ToString()}");

                throw ex;
            }
        }
Пример #4
0
        /// <summary>
        /// Process the initial request to establish a long lived connection via a streaming server.
        /// </summary>
        /// <param name="bot">The <see cref="IBot"/> instance.</param>
        /// <param name="httpRequest">The connection request.</param>
        /// <param name="httpResponse">The response sent on error or connection termination.</param>
        /// <returns>Returns on task completion.</returns>
        private async Task ConnectWebSocketAsync(IBot bot, HttpRequest httpRequest, HttpResponse httpResponse)
        {
            if (httpRequest == null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }

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

            ConnectedBot = bot ?? throw new ArgumentNullException(nameof(bot));

            if (!httpRequest.HttpContext.WebSockets.IsWebSocketRequest)
            {
                httpRequest.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                await httpRequest.HttpContext.Response.WriteAsync("Upgrade to WebSocket is required.").ConfigureAwait(false);

                return;
            }

            if (!await AuthenticateRequestAsync(httpRequest).ConfigureAwait(false))
            {
                httpRequest.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                await httpRequest.HttpContext.Response.WriteAsync("Request authentication failed.").ConfigureAwait(false);

                return;
            }

            try
            {
                var socket = await httpRequest.HttpContext.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);

                var requestHandler = new StreamingRequestHandler(bot, this, socket, Logger);

                if (RequestHandlers == null)
                {
                    RequestHandlers = new List <StreamingRequestHandler>();
                }

                RequestHandlers.Add(requestHandler);

                await requestHandler.ListenAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                httpRequest.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                await httpRequest.HttpContext.Response.WriteAsync($"Unable to create transport server. Error: {ex.ToString()}").ConfigureAwait(false);

                throw ex;
            }
        }