Exemplo n.º 1
0
        public async Task<XDocument> InvokeAsync(string operationName, IDictionary<string, object> args)
        {
            NatDiscoverer.TraceSource.TraceEvent(TraceEventType.Verbose, 0, "SOAPACTION: **{0}** url:{1}", operationName,
                                                 _url);
            byte[] messageBody = BuildMessageBody(operationName, args);
            HttpWebRequest request = BuildHttpWebRequest(operationName, messageBody);

            if (messageBody.Length > 0)
            {
                using (var stream = await request.GetRequestStreamAsync().ConfigureAwait(false))
                {
                    await stream.WriteAsync(messageBody, 0, messageBody.Length).ConfigureAwait(false);
                }
            }

            using(var response = await GetWebResponse(request).ConfigureAwait(false))
            {
                var stream = response.GetResponseStream();
                var contentLength = response.ContentLength;

                var reader = new StreamReader(stream, Encoding.UTF8);

                var responseBody = contentLength != -1
                                    ? reader.ReadAsMany((int) contentLength)
                                    : reader.ReadToEnd();

                var responseXml = GetXmlDocument(responseBody);

                response.Dispose();
                return responseXml;
            }
        }
Exemplo n.º 2
0
        public Task<XmlDocument> InvokeAsync(string operationName, IDictionary<string, object> args)
        {
            NatDiscoverer.TraceSource.TraceEvent(TraceEventType.Verbose, 0, "SOAPACTION: **{0}** url:{1}", operationName,
                                                 _url);
            byte[] messageBody = BuildMessageBody(operationName, args);
            HttpWebRequest request = BuildHttpWebRequest(operationName, messageBody);

            Task<WebResponse> responseTask;
            if (messageBody.Length > 0)
            {
                Stream requestStream = null;
                responseTask = Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null)
                    .ContinueWith(requestSteamTask =>
                    {
                        requestStream = requestSteamTask.Result;
                        return Task.Factory.FromAsync<byte[], int, int>(requestStream.BeginWrite,
                            requestStream.EndWrite, messageBody, 0, messageBody.Length, null);
                    })
                    .Unwrap()
                    .ContinueWith(streamWriteTask =>
                    {
                        requestStream.Close();
                        return GetWebResponse(request);
                    })
                    .Unwrap();
            }
            else
            {
                responseTask = GetWebResponse(request);
            }

            return responseTask.ContinueWith(task =>
            {
                using (WebResponse response = task.Result)
                {
                    var stream = response.GetResponseStream();
                    var contentLength = response.ContentLength;

                    var reader = new StreamReader(stream, Encoding.UTF8);

                    var responseBody = contentLength != -1
                        ? reader.ReadAsMany((int)contentLength)
                        : reader.ReadToEnd();

                    var responseXml = GetXmlDocument(responseBody);

                    response.Close();
                    return responseXml;
                }
            });
        }