コード例 #1
0
        /// <summary>
        /// Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
        /// </summary>
        /// <param name="stream">The stream to write data to.</param>
        /// <param name="buffer">The buffer to write data from.</param>
        /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> from which to begin copying bytes to the stream.</param>
        /// <param name="count">The maximum number of bytes to write.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="buffer"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="offset"/> or <paramref name="count"/> is negative.</exception>
        /// <exception cref="ArgumentException">Thrown if the sum of <paramref name="offset"/> and <paramref name="count"/> is larger than the <paramref name="buffer"/> length.</exception>
        /// <exception cref="NotSupportedException">Thrown if the stream does not support writing.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the stream is currently in use by a previous write operation.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the stream has been disposed.</exception>
        /// <returns>An <see cref="IAsyncOperation"/> that represents the asynchronous write operation.</returns>
        public static IAsyncOperation WriteAsync(this Stream stream, byte[] buffer, int offset, int count)
        {
            var op = new ApmResult <Stream, VoidResult>(stream);

            stream.BeginWrite(buffer, offset, count, OnWriteCompleted, op);
            return(op);
        }
コード例 #2
0
        public async Task Interceptor_Captures_Async_Exception()
        {
            ApmResult.Clear();
            DependencyReplacement.SetResolver <IQuotientCalculator>(() => new QuotientImplementationFake(ResultTypeEnum.AsyncException));

            // Act
            var response = await ApiClient.PostAsync(CALCULATOR_URL, ContentHelper.GetStringContent(new { dividend = 1, divisor = 999 }));

            response.StatusCode.Should().Be(500);
            var responseBody = await response.Content.ReadAsStringAsync();

            System.Threading.Thread.Sleep(12000);

            // Assert
            responseBody.Should().Contain("System.InvalidOperationException: Fake Async exception");
            ApmResult.Transactions.Should().HaveCount(1);
            ApmResult.Transactions.First().Name.Should().Be("POST Calculator/Division");
            ApmResult.Spans.Should().HaveCount(2);

            ApmResult.Spans[1].Name.Should().Be("CalculatorService.Quotient");
            ApmResult.Spans[1].Outcome.Should().Be(Outcome.Failure);
            ApmResult.Spans[1].TryGetLabel <string>("AsyncTask", out var label).Should().BeTrue();
            label.Should().Be("Faulted");

            ApmResult.Errors.Should().HaveCount(1);
            ApmResult.Errors.First().Exception.Type.Should().Be("System.InvalidOperationException");
        }
コード例 #3
0
        /// <summary>
        /// Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
        /// </summary>
        /// <param name="stream">The stream to read data from.</param>
        /// <param name="buffer">The buffer to write the data into.</param>
        /// <param name="offset">The byte offset in <paramref name="buffer"/> at which to begin writing data from the stream.</param>
        /// <param name="count">The maximum number of bytes to read.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="buffer"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="offset"/> or <paramref name="count"/> is negative.</exception>
        /// <exception cref="ArgumentException">Thrown if the sum of <paramref name="offset"/> and <paramref name="count"/> is larger than the <paramref name="buffer"/> length.</exception>
        /// <exception cref="NotSupportedException">Thrown if the stream does not support reading.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the stream is currently in use by a previous read operation.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the stream has been disposed.</exception>
        /// <returns>An <see cref="IAsyncOperation{TResult}"/> that represents the asynchronous read operation. The value of the result
        /// parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested
        /// if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream
        /// has been reached.</returns>
        public static IAsyncOperation <int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count)
        {
            var op = new ApmResult <Stream, int>(stream);

            stream.BeginRead(buffer, offset, count, OnReadCompleted, op);
            return(op);
        }
コード例 #4
0
        /// <summary>
        /// Begins an asynchronous request for a remote host connection.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <param name="remoteEP">An <see cref="EndPoint"/> that represents the remote host.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="remoteEP"/> is <see langword="null"/>.</exception>
        /// <exception cref="InvalidOperationException">The <paramref name="socket"/> is listening.</exception>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation ConnectAsync(this Socket socket, EndPoint remoteEP)
        {
            var op = new ApmResult <Socket, VoidResult>(socket);

            socket.BeginConnect(remoteEP, OnConnectCompleted, op);
            return(op);
        }
コード例 #5
0
        public async Task Interceptor_Captures_Async_Success()
        {
            ApmResult.Clear();
            DependencyReplacement.SetResolver <IQuotientCalculator>(() => new QuotientImplementationFake(ResultTypeEnum.SyncSuccess));

            // Act
            var response = await ApiClient.PostAsync(CALCULATOR_URL, ContentHelper.GetStringContent(new { dividend = 1, divisor = 100 }));

            response.EnsureSuccessStatusCode();
            var responseBody = await response.Content.ReadAsStringAsync();

            System.Threading.Thread.Sleep(2000);

            // Assert
            responseBody.Should().Be("{\"quotient\":0}");
            ApmResult.Transactions.Should().HaveCount(1);
            ApmResult.Transactions.First().Name.Should().Be("POST Calculator/Division");
            ApmResult.Spans.Should().HaveCount(2);
            ApmResult.Spans[0].Name.Should().Be("ValidationService.IsValidDivision");
            ApmResult.Spans[0].Outcome.Should().Be(Outcome.Success);

            ApmResult.Spans[1].Name.Should().Be("CalculatorService.Quotient");
            ApmResult.Spans[1].Outcome.Should().Be(Outcome.Success);
            ApmResult.Errors.Should().HaveCount(0);
        }
コード例 #6
0
        /// <summary>
        /// Begins an asynchronous operation to accept an incoming connection attempt from a specified socket and
        /// receives the first block of data sent by the client application.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <param name="acceptSocket">The accepted <see cref="Socket"/> object. This value may be <see langword="null"/>.</param>
        /// <param name="receiveSize">The maximum number of bytes to receive.</param>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation{TResult}"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation <Socket> AcceptAsync(this Socket socket, Socket acceptSocket, int receiveSize)
        {
            var op = new ApmResult <Socket, Socket>(socket);

            socket.BeginAccept(acceptSocket, receiveSize, OnAcceptCompleted, op);
            return(op);
        }
コード例 #7
0
        /// <summary>
        /// Begins an asynchronous operation to accept an incoming connection attempt.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation{TResult}"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation <Socket> AcceptAsync(this Socket socket)
        {
            var op = new ApmResult <Socket, Socket>(socket);

            socket.BeginAccept(OnAcceptCompleted, op);
            return(op);
        }
コード例 #8
0
        /// <summary>
        /// Begins to asynchronously receive data from a connected <see cref="Socket"/>.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <param name="buffers">An array of bytes that is the storage location for the received data.</param>
        /// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags"/> values.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="buffers"/> is <see langword="null"/>.</exception>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation{TResult}"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation <int> ReceiveAsync(this Socket socket, IList <ArraySegment <byte> > buffers, SocketFlags socketFlags)
        {
            var op = new ApmResult <Socket, int>(socket);

            socket.BeginReceive(buffers, socketFlags, OnReceiveCompleted, op);
            return(op);
        }
コード例 #9
0
        /// <summary>
        /// Begins to asynchronously receive data from a connected <see cref="Socket"/>.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <param name="buffer">An array of bytes that is the storage location for the received data.</param>
        /// <param name="offset">The zero-based position in the <paramref name="buffer"/> at which to store the received data.</param>
        /// <param name="size">The number of bytes to receive.</param>
        /// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags"/> values.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="buffer"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="offset"/> is less than 0 or <paramref name="offset"/> is greater
        /// than the length of <paramref name="buffer"/> or <paramref name="size"/> is less than 0 or <paramref name="size"/> is greater
        /// than the length of <paramref name="buffer"/> minus the value of the <paramref name="offset"/> parameter.</exception>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation{TResult}"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation <int> ReceiveAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags socketFlags)
        {
            var op = new ApmResult <Socket, int>(socket);

            socket.BeginReceive(buffer, offset, size, socketFlags, OnReceiveCompleted, op);
            return(op);
        }
コード例 #10
0
        /// <summary>
        /// Sends the file <paramref name="fileName"/> to a connected <see cref="Socket"/> object using the <see cref="TransmitFileOptions.UseDefaultWorkerThread"/> flag.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <param name="fileName">A string that contains the path and name of the file to send. This parameter can be <see langword="null"/>.</param>
        /// <exception cref="FileNotFoundException">The file <paramref name="fileName"/> was not found.</exception>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="NotSupportedException">The operating system is not Windows NT or later or rhe <paramref name="socket"/> is not connected to a remote host.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation SendFileAsync(this Socket socket, string fileName)
        {
            var op = new ApmResult <Socket, VoidResult>(socket);

            socket.BeginSendFile(fileName, OnSendFileCompleted, op);
            return(op);
        }
コード例 #11
0
        /// <summary>
        /// Sends the file <paramref name="fileName"/> to a connected <see cref="Socket"/> object.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <param name="fileName">A string that contains the path and name of the file to send. This parameter can be <see langword="null"/>.</param>
        /// <param name="preBuffer">A byte array that contains data to be sent before the file is sent. This parameter can be <see langword="null"/>.</param>
        /// <param name="postBuffer">A byte array that contains data to be sent after the file is sent. This parameter can be <see langword="null"/>.</param>
        /// <param name="flags">A bitwise combination of <see cref="TransmitFileOptions"/> values.</param>
        /// <exception cref="FileNotFoundException">The file <paramref name="fileName"/> was not found.</exception>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="NotSupportedException">The operating system is not Windows NT or later or rhe <paramref name="socket"/> is not connected to a remote host.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation SendFileAsync(this Socket socket, string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
        {
            var op = new ApmResult <Socket, VoidResult>(socket);

            socket.BeginSendFile(fileName, preBuffer, postBuffer, flags, OnSendFileCompleted, op);
            return(op);
        }
コード例 #12
0
        /// <summary>
        /// Begins an asynchronous request to disconnect from a remote endpoint.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <param name="reuseSocket"><see langword="true"/> if this socket can be reused after the connection is closed; otherwise, <see langword="false"/>.</param>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="NotSupportedException">The operating system is Windows 2000 or earlier, and this method requires Windows XP.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation DisconnectAsync(this Socket socket, bool reuseSocket)
        {
            var op = new ApmResult <Socket, VoidResult>(socket);

            socket.BeginDisconnect(reuseSocket, OnDisconnectCompleted, op);
            return(op);
        }
コード例 #13
0
        /// <summary>
        /// Begins an asynchronous request for a remote host connection. The host is specified by an <see cref="IPAddress"/> array and a port number.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <param name="addresses">At least one <see cref="IPAddress"/>, designating the remote host.</param>
        /// <param name="port">The port number of the remote host.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="addresses"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="port"/> number is invalid.</exception>
        /// <exception cref="InvalidOperationException">The <paramref name="socket"/> is listening.</exception>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation ConnectAsync(this Socket socket, IPAddress[] addresses, int port)
        {
            var op = new ApmResult <Socket, VoidResult>(socket);

            socket.BeginConnect(addresses, port, OnConnectCompleted, op);
            return(op);
        }
コード例 #14
0
        /// <summary>
        /// Begins an asynchronous request for a remote host connection. The host is specified by a host name and a port number.
        /// </summary>
        /// <param name="socket">The target socket.</param>
        /// <param name="host">The name of the remote host.</param>
        /// <param name="port">The port number of the remote host.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="host"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="port"/> number is invalid.</exception>
        /// <exception cref="InvalidOperationException">The <paramref name="socket"/> is listening.</exception>
        /// <exception cref="SocketException">An error occurred when attempting to access the <paramref name="socket"/>.</exception>
        /// <exception cref="ObjectDisposedException">Thrown if the <paramref name="socket"/> has been closed.</exception>
        /// <returns>Returns <see cref="IAsyncOperation"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation ConnectAsync(this Socket socket, string host, int port)
        {
            var op = new ApmResult <Socket, VoidResult>(socket);

            socket.BeginConnect(host, port, OnConnectCompleted, op);
            return(op);
        }
コード例 #15
0
        /// <summary>
        /// Begins an asynchronous request for an Internet resource.
        /// </summary>
        /// <param name="webRequest">The source <see cref="WebRequest"/>.</param>
        /// <exception cref="NotImplementedException">Thrown if an attempt is made to access the method, when the method is not overridden in a descendant class.</exception>
        /// <returns>Returns <see cref="IAsyncOperation{TResult}"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation <WebResponse> GetResponseAsync(this WebRequest webRequest)
        {
            var op = new ApmResult <WebRequest, WebResponse>(webRequest);

            webRequest.BeginGetResponse(OnGetResponseCompleted, op);
            return(op);
        }
コード例 #16
0
        /// <summary>
        /// Returns a <see cref="Stream"/> for writing data to the Internet resource as an asynchronous operation.
        /// </summary>
        /// <param name="webRequest">The source <see cref="WebRequest"/>.</param>
        /// <exception cref="NotImplementedException">Thrown if an attempt is made to access the method, when the method is not overridden in a descendant class.</exception>
        /// <returns>Returns <see cref="IAsyncOperation{TResult}"/> representing the asynchronous operation.</returns>
        public static IAsyncOperation <Stream> GetRequestStreamAsync(this WebRequest webRequest)
        {
            var op = new ApmResult <WebRequest, Stream>(webRequest);

            webRequest.BeginGetRequestStream(OnGetRequestStreamCompleted, op);
            return(op);
        }