Example #1
0
        public void Send <I, O>(string service, string method, I payload, ClientTransportCallback <O> callback, C ctx)
        {
            try {
                var request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}", endpoint, service, method));
                request.Timeout = Timeout * 1000;
                request.Method  = payload == null ? "GET" : "POST";
                if (HttpHeaders != null)
                {
                    foreach (var key in HttpHeaders.AllKeys)
                    {
                        foreach (var value in HttpHeaders.GetValues(key))
                        {
                            request.Headers.Add(key, value);
                        }
                    }
                }

                if (payload != null)
                {
                    var data = Marshaller.Marshal <I>(payload);
                    if (data == null)
                    {
                        throw new TransportException("HttpTransport only supports Marshallers which return a string.");
                    }

                    request.Method        = "POST";
                    request.ContentType   = "application/json";
                    request.ContentLength = data.Length;

                    using (var stream = request.GetRequestStream()) {
                        stream.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
                    }
                }

                using (var response = (HttpWebResponse)request.GetResponse()) {
                    using (var respStream = response.GetResponseStream()) {
                        using (var reader = new StreamReader(respStream, Encoding.UTF8)) {
                            string jsonString = reader.ReadToEnd();
                            if (string.IsNullOrEmpty(jsonString))
                            {
                                throw new TransportException("Empty Response");
                            }

                            var data = Marshaller.Unmarshal <O>(jsonString);
                            callback.Success(data);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                callback.Failure(
                    new TransportException("Unexpected exception occured during async request.", ex)
                    );
            }
        }
Example #2
0
        public void Send <I, O>(string service, string method, I payload, ClientTransportCallback <O> callback, C ctx)
        {
            try {
                var request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}", endpoint, service, method));
                request.Timeout = Timeout * 1000;
                request.Method  = payload == null ? "GET" : "POST";
                if (HttpHeaders != null)
                {
                    foreach (var key in HttpHeaders.AllKeys)
                    {
                        foreach (var value in HttpHeaders.GetValues(key))
                        {
                            request.Headers.Add(key, value);
                        }
                    }
                }

                var state = new RequestState <O>(request, callback, null);
                if (payload == null)
                {
                    request.BeginGetResponse(ProcessResponse <O>, state);
                }
                else
                {
                    var data = Marshaller.Marshal <I>(payload);
                    state.JsonString    = data;
                    request.ContentType = "application/json";
                    request.BeginGetRequestStream(ProcessStreamRequest <O>, state);
                }
            }
            catch (Exception ex)
            {
                callback.Failure(
                    new TransportException("Unexpected exception occured during async request.", ex)
                    );
            }
        }
Example #3
0
 public RequestState(HttpWebRequest request, ClientTransportCallback <O> callback, string jsonString)
 {
     Request    = request;
     Callback   = callback;
     JsonString = jsonString;
 }