/// <summary> /// Sends a request to WCF. /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task SendAsync(HttpContext context) { var request = new AspNetCoreRequest(context); buffer.Enqueue(request); await request.Task; }
/// <summary> /// Sends the reply message by writing it back to the HTTP response. /// </summary> /// <param name="message"></param> /// <param name="timeout"></param> /// <returns></returns> internal async Task ReplyAsync(Message message, AspNetCoreRequest request, TimeSpan timeout) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } // write the response body try { ThrowIfDisposedOrNotOpen(); await WriteMessageAsync(request.Context.Response, message); request.TrySetResult(true); } catch (CommunicationException e) { request.TrySetException(e); throw; } catch (Exception e) { request.TrySetException(e); throw new CommunicationException(e.Message, e); } }
/// <summary> /// Sends a request to WCF. /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task SendAsync(HttpContext context) { var request = new AspNetCoreRequest(context); // ensure data makes it into buffer while (!await buffer.SendAsync(request).ConfigureAwait(false)) await Task.Yield(); // wait for completion of request await request.Task; }
/// <summary> /// Attempts to dequeue an incoming request. /// </summary> /// <param name="timeout"></param> /// <returns></returns> protected override async Task <(RequestContext Result, bool Success)> TryReceiveRequestAsync(TimeSpan timeout) { ThrowIfDisposedOrNotOpen(); if (timeout.TotalMilliseconds > int.MaxValue) { timeout = TimeSpan.FromMilliseconds(int.MaxValue); } lock (ThisLock) { try { // request was already handled, let the channel timeout if (request == null) { return(null, true); } // incoming request was canceled if (request.Context.RequestAborted.IsCancellationRequested) { Abort(); } ThrowIfDisposedOrNotOpen(); // read message and generate context var context = new AspNetCoreRequestContext( request, ReadMessage(request.Context.Request), this); // release request, this channel is done return(context, true); } catch (CommunicationException e) { request?.TrySetException(e); throw; } catch (Exception e) { request?.TrySetException(e); throw new CommunicationException(e.Message, e); } finally { // finished with request, forget about it request = null; } } }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="request"></param> /// <param name="lease"></param> /// <param name="encoderFactory"></param> /// <param name="bufferManager"></param> /// <param name="localAddress"></param> /// <param name="parent"></param> public AspNetCoreReplyChannel( AspNetCoreRequest request, AspNetCoreRequestQueueLease lease, MessageEncoderFactory encoderFactory, BufferManager bufferManager, EndpointAddress localAddress, AspNetCoreReplyChannelListener parent) : base(parent) { this.request = request ?? throw new ArgumentNullException(nameof(request)); this.lease = lease ?? throw new ArgumentNullException(nameof(lease)); this.bufferManager = bufferManager ?? throw new ArgumentNullException(nameof(bufferManager)); this.localAddress = localAddress ?? throw new ArgumentNullException(nameof(localAddress)); encoder = encoderFactory.CreateSessionEncoder(); }
/// <summary> /// Sends the reply message by writing it back to the HTTP response. /// </summary> /// <param name="message"></param> /// <param name="timeout"></param> /// <returns></returns> internal async Task ReplyAsync(Message message, AspNetCoreRequest request, TimeSpan timeout) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } try { // incoming request was canceled if (request.Context.RequestAborted.IsCancellationRequested) { throw new CommunicationObjectAbortedException(); } ThrowIfDisposedOrNotOpen(); await WriteMessageAsync(request.Context.Response, message).ConfigureAwait(false); // indicate that request is finished request.TrySetResult(true); } catch (CommunicationException e) { request.TrySetException(e); throw; } catch (Exception e) { request.TrySetException(e); throw new CommunicationException(e.Message, e); } finally { if (lease != null) { lease.Dispose(); lease = null; } } }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="request"></param> /// <param name="message"></param> /// <param name="reply"></param> public AspNetCoreRequestContext(AspNetCoreRequest request, Message message, AspNetCoreReplyChannel reply) { this.request = request ?? throw new ArgumentNullException(nameof(request)); this.message = message ?? throw new ArgumentNullException(nameof(message)); this.reply = reply ?? throw new ArgumentNullException(nameof(reply)); }