/// <summary> /// Envia a requisição. /// </summary> /// <param name="connection"></param> /// <param name="request"></param> /// <returns></returns> public static EventHandler SendRequest(object connection, System.Net.HttpWebRequest request) { if (request.IsAborted()) { return(null); } object obj2 = connection; lock (obj2) { if (connection.GetState().TrySetBusy()) { connection.SetStatus(System.Net.WebExceptionStatus.Success); var initConn = connection.GetInitConn(); System.Threading.Thread workerThread = null; workerThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(state => { System.Threading.Thread.CurrentThread.Name = _isFirstSendRequest ? "WCF FirstSend" : "WCF SendRequest"; _isFirstSendRequest = false; try { initConn(state); } catch (Exception ex) { try { Console.WriteLine("WCF SendRequest ERROR " + ex.ToString()); } catch { } } })); workerThread.Start(request); new System.Threading.Timer(o => { if (workerThread != null && workerThread.IsAlive) { workerThread.Abort(); } }, null, request.ReadWriteTimeout + 1000, System.Threading.Timeout.Infinite); } else { object queue = connection.GetQueue(); lock (queue) { connection.GetQueue().Enqueue(request); } } } return(connection.GetAbortHandler()); }
/// <summary> /// Inicia o processo assincrono de recuperação da stream de requisição. /// </summary> /// <param name="request"></param> /// <param name="callback"></param> /// <param name="state"></param> /// <returns></returns> public static IAsyncResult BeginGetRequestStream(System.Net.HttpWebRequest request, AsyncCallback callback, object state) { try { if (request.IsAborted()) { throw new System.Net.WebException("The request was canceled.", System.Net.WebExceptionStatus.RequestCanceled); } bool flag = (((request.Method != "GET") && (request.Method != "CONNECT")) && (request.Method != "HEAD")) && (request.Method != "TRACE"); if (request.Method == null || !flag) { throw new System.Net.ProtocolViolationException("Cannot send data when method is: " + request.Method); } if ((request.ContentLength == -1L && !request.SendChunked) && (request.KeepAlive)) { throw new System.Net.ProtocolViolationException("Content-Length not set"); } string transferEncoding = request.TransferEncoding; if ((!request.SendChunked && (transferEncoding != null)) && (transferEncoding.Trim() != string.Empty)) { throw new System.Net.ProtocolViolationException("SendChunked should be true."); } object locker = request.GetLock(); lock (locker) { if (request.IsResponseCalled()) { throw new InvalidOperationException("The operation cannot be performed once the request has been submitted."); } if (request.GetAsyncWrite() != null) { throw new InvalidOperationException("Cannot re-call start of asynchronous method while a previous call is still in progress."); } request.SetAsyncWrite(CreateWebAsyncResult(request, callback, state)); request.SetInitialMethod(request.Method); var writeStream = request.GetWriteStream(); IAsyncResult asyncWrite = null; if (request.HaveRequest() && (writeStream != null)) { asyncWrite = request.GetAsyncWrite(); asyncWrite.SetCompleted(true, writeStream); asyncWrite.DoCallback(); return(request.GetAsyncWrite()); } request.SetGotRequestStream(true); asyncWrite = request.GetAsyncWrite(); if (!request.IsRequestSent()) { request.SetRequestSent(true); request.SetRedirects(0); var servicePoint = request.GetServicePoint(); request.SetServicePoint(servicePoint); request.SetAbortHandler(servicePoint.SendRequest(request, request.GetConnectionGroup())); } return(asyncWrite); } } catch (Exception ex) { throw new Exception(ex.ToString(), ex); } }