Ejemplo n.º 1
0
        void ITransport.SendRequestAsync(ServiceRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            Uri            requestUri  = new Uri(uri.Replace(TOKEN_ACTION, request.Action).Replace(TOKEN_SERVICE, request.Service));
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(requestUri);

            httpRequest.Method = "POST";
            httpRequest.Accept = httpRequest.ContentType = RpcUtils.HTTP_RPC_MIME_TYPE;
#if !SILVERLIGHT && !CF2
            httpRequest.AutomaticDecompression = DecompressionMethods.None;
#endif
#if !SILVERLIGHT
            httpRequest.UserAgent = "protobuf-net";
            httpRequest.Headers.Add(RpcUtils.HTTP_RPC_VERSION_HEADER, "0.1");
#endif
            Action <Exception> handler = delegate(Exception ex) {
                request.OnException(ex);
            };
            Action <WebResponse> onResponse = delegate(WebResponse webResponse)
            {
                try
                {
                    using (Stream stream = webResponse.GetResponseStream())
                    {
                        object result = RpcUtils.UnpackArgs(stream, request.Method, request.Args, RpcUtils.IsResponseArgument);
                        request.OnResponse(result);
                    }
                }
                catch (Exception ex) { handler(ex); }
            };
            Action <Stream> onGetRequest = delegate(Stream stream)
            {
                try
                {
                    RpcUtils.PackArgs(stream, request.Method, null, request.Args, RpcUtils.IsRequestArgument);
                    stream.Close();
                    AsyncUtility.RunAsync(httpRequest.BeginGetResponse, httpRequest.EndGetResponse, onResponse, handler);
                }
                catch (Exception ex) {
                    //Trace.WriteLine(ex, GetType() + ":" + request.Method.DeclaringType.Name);
                    handler(ex);
                }
            };
            AsyncUtility.RunAsync(httpRequest.BeginGetRequestStream, httpRequest.EndGetRequestStream, onGetRequest, handler);
        }