private async Task ProcessRequest(HttpListenerContext context, CancellationToken cancelRequest)
        {
            if (m_operations == null)
            {
                Interlocked.CompareExchange(ref m_operations, await PartitionOperations.CreateAsync(StateManager), null);
            }

            String output = null;

            try {
                HttpListenerRequest request = context.Request;
                if (request.HttpMethod == "GET")
                {
                    // Read request from client:
                    String jsonRpc = context.Request.QueryString["jsonrpc"];

                    // Process request to get response:
                    JsonRpcRequest  jsonRequest  = JsonRpcRequest.Parse(jsonRpc);
                    JsonRpcResponse jsonResponse = await jsonRequest.InvokeAsync(s_jsSerializer, m_operations, cancelRequest);

                    output = jsonResponse.ToString();
                }
            }
            catch (Exception ex) { output = ex.ToString(); }
            // Write response to client:
            using (var response = context.Response) {
                if (output != null)
                {
                    response.AppendHeader("Access-Control-Allow-Origin", null);
                    Byte[] outBytes = Encoding.UTF8.GetBytes(output);
                    response.OutputStream.Write(outBytes, 0, outBytes.Length);
                }
            }
        }
Пример #2
0
        // My endpoint listener calls this method for each client request
        private async Task ProcessInputRequest(HttpListenerContext context, CancellationToken cancelRequest)
        {
            String output = null;

            if (firstTime)
            {
                firstTime = false; output = await PrimeAsync(m_selfUrl, cancelRequest);
            }
            try {
                HttpListenerRequest request = context.Request;
                foreach (String key in request.QueryString)
                {
                    String queryValue = request.QueryString[key];
                    switch (key.ToLowerInvariant())
                    {
                    case "prime":
                        output = await PrimeAsync(m_selfUrl, cancelRequest);

                        break;

                    case "jsonrpc": // Process request to get response:
                        JsonRpcRequest  jsonRequest  = JsonRpcRequest.Parse(queryValue);
                        JsonRpcResponse jsonResponse = await jsonRequest.InvokeAsync(s_jsSerializer, m_operations, cancelRequest);

                        output = jsonResponse.ToString();
                        break;
                    }
                }
            }
            catch (Exception ex) { output = ex.ToString(); }
            // Write response to client:
            using (var response = context.Response) {
                if (output != null)
                {
                    response.AppendHeader("Access-Control-Allow-Origin", null);
                    Byte[] outBytes = Encoding.UTF8.GetBytes(output);
                    response.OutputStream.Write(outBytes, 0, outBytes.Length);
                }
            }
        }