Ejemplo n.º 1
0
        private void HandleContext(HttpListenerContext context)
        {
            try
            {
                string traceId, contextId;
                bool?  isActive;
                RequestExtensions.ExtractFromHttpHeaders(context.Request.Headers, out traceId, out contextId, out isActive);
                using (var serverContext = Trace.ContinueContext(traceId, contextId, isActive ?? false, isRoot: false))
                {
                    serverContext.RecordTimepoint(Timepoint.ServerReceive);
                    serverContext.RecordAnnotation(Annotation.RequestUrl, context.Request.Url.ToString());

                    using (var streamReader = new StreamReader(context.Request.InputStream, Encoding.UTF8))
                    {
                        var message = streamReader.ReadToEnd();
                        taskQueue.PushTask(new RemoteTask(message));
                        context.Response.StatusCode = 202;
                        context.Response.Close();
                    }

                    serverContext.RecordTimepoint(Timepoint.ServerSend);
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Unhandled server exception: {0}", e);
            }
            finally
            {
                synchronizer.ServerEndProcessQuerySignal.Set();
            }
        }
Ejemplo n.º 2
0
        public static void ClientServerCase()
        {
            const string url      = "http://localhost:12346/";
            var          listener = new HttpListener();

            listener.Prefixes.Add(url);
            listener.Start();

            var serverTask = Task.Factory.StartNew(() =>
            {
                var context = listener.GetContext();
                var request = context.Request;
                string traceId, contextId;
                bool?isActive;
                RequestExtensions.ExtractFromHttpHeaders(request.Headers, out traceId, out contextId, out isActive);
                using (var serverContext = Trace.ContinueContext(traceId, contextId, isActive ?? false, isRoot: false))
                {
                    serverContext.RecordTimepoint(Timepoint.ServerReceive);
                    context.Response.Close();
                    serverContext.RecordTimepoint(Timepoint.ServerSend);
                }
            });

            using (var clientContext = Trace.CreateRootContext("Client"))
            {
                clientContext.RecordTimepoint(Timepoint.ClientSend);
                clientContext.RecordAnnotation(Annotation.RequestUrl, url);
                var clientRequest = (HttpWebRequest)WebRequest.Create(url);
                clientRequest.SetTracingHeaders(clientContext);
                clientRequest.GetResponse();
                serverTask.Wait();
                clientContext.RecordTimepoint(Timepoint.ClientReceive);
            }

            listener.Close();
        }