示例#1
0
        private void Dispatch(HttpListenerContext context)
        {
            try
            {
                var request = context.Request;
                using (var reader = new StreamReader(request.InputStream))
                {
                    var body    = reader.ReadToEnd();
                    var message = JsonConvert.DeserializeObject <HttpPipelineMessage>(body);

                    HttpPipelineControlResponse responseMessage = new HttpPipelineControlResponse();

                    if (MessageReceivedHandler != null)
                    {
                        try
                        {
                            responseMessage = MessageReceivedHandler(message);
                        }
                        catch (Exception ex)
                        {
                            PowerLogger.LogLine("Receive handler threw an exception");
                            responseMessage.ExceptionInfo = ex.ToString();
                            responseMessage.StatusCode    = HttpStatusCode.BadRequest;
                        }
                    }

                    var resposneMessageContents = JsonConvert.SerializeObject(responseMessage, HttpPipelineMessage.CommonSettings);
                    using (var writer = new StreamWriter(context.Response.OutputStream))
                    {
                        writer.Write(resposneMessageContents);
                    }

                    if (responseMessage.Close)
                    {
                        PowerLogger.LogLine("Closing listener because a response message told us to");
                        Stop();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ListenException != null)
                {
                    ListenException(ex);
                }
                else
                {
                    throw;
                }
            }
        }
        public override void Accept(object o)
        {
            PowerLogger.LogLine("Sending object over the wire: " + o);
            HttpPipelineControlResponse response = null;

            try
            {
                response = Sender.SendObject(o);
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ConnectFailure)
                {
                    try
                    {
                        throw new ArgException("Could not connect to external program '" + exe + "', possibly because it was not built with PowerArgs, or it does not have remote pipelining enabled.  TODO - Help URL here.", ex);
                    }
                    catch (Exception toBubble)
                    {
                        this.Manager.BubbleAsyncException(toBubble);
                    }
                }
                else
                {
                    this.Manager.BubbleAsyncException(ex);
                }
            }
            catch (Exception ex)
            {
                this.Manager.BubbleAsyncException(ex);
            }

            if (response == null)
            {
                return;
            }

            if (response.StatusCode != HttpStatusCode.Accepted)
            {
                this.Manager.BubbleAsyncException(new ArgException("The external program '" + exe + "' could not accept the object of type: " + o.GetType().FullName + "\n\n" + response.ExceptionInfo));
            }
        }