コード例 #1
0
        /// <summary />
        protected override async Task HandleResponse(RestExecutionContext context, HttpStatusCode statusCode, byte[] message)
        {
            // TODO: What about binary files? :/
            string asString = Encoding.UTF8.GetString(message);

            await Journal(context, true, statusCode, asString);
        }
コード例 #2
0
 /// <summary />
 protected override async Task HandleResponse(RestExecutionContext context, HttpStatusCode statusCode, byte[] message)
 {
     await Task.Run(() =>
     {
         Debug.WriteLine(string.Format(
                             CultureInfo.InvariantCulture,
                             "r:{0}\r\n{1}",
                             context.ActivityId,
                             Encoding.UTF8.GetString(message)));
     });
 }
コード例 #3
0
 /// <summary />
 protected override async Task HandleRequest(RestExecutionContext context, byte[] message)
 {
     await Task.Run(() =>
     {
         Debug.WriteLine(string.Format(
                             CultureInfo.InvariantCulture,
                             "R:{0} - {1} {2}\r\n{3}",
                             context.ActivityId,
                             context.Method,
                             context.RequestUri.AbsolutePath,
                             Encoding.UTF8.GetString(message)));
     });
 }
コード例 #4
0
        private async Task Journal(RestExecutionContext context, bool direction, HttpStatusCode?statusCode, string message)
        {
            const string Database = "SqlServerLogging";

            if (ConfigurationManager.ConnectionStrings[Database] == null)
            {
                throw new ZincException(ER.Rest_SqlServer_ConnectionMissing, Database);
            }


            /*
             *
             */
            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = ConfigurationManager.ConnectionStrings[Database].ConnectionString;

            try
            {
                await conn.OpenAsync();
            }
            catch (SqlException ex)
            {
                throw new ZincException(ER.Rest_SqlServer_Open, ex, Database);
            }
            catch (ConfigurationErrorsException ex)
            {
                throw new ZincException(ER.Rest_SqlServer_Open, ex, Database);
            }
            catch (InvalidOperationException ex)
            {
                throw new ZincException(ER.Rest_SqlServer_Open, ex, Database);
            }


            /*
             *
             */
            object accessToken;
            object statusCodeStr;

            if (context.AccessToken != null)
            {
                accessToken = context.AccessToken;
            }
            else
            {
                accessToken = DBNull.Value;
            }

            if (statusCode.HasValue == true)
            {
                statusCodeStr = statusCode.ToString();
            }
            else
            {
                statusCodeStr = DBNull.Value;
            }


            /*
             *
             */
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "insert into ZN_REST_JOURNAL ( Application, ActivityId, AccessToken, ExecutionId, Method, URI, Direction, StatusCode, JsonMessage, Moment ) "
                              + "values ( @Application, @ActivityId, @AccessToken, @ExecutionId, @Method, @URI, @Direction, @StatusCode, @JsonMessage, @Moment ) ";
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.Add("@Application", SqlDbType.VarChar).Value          = App.Name;
            cmd.Parameters.Add("@ActivityId", SqlDbType.UniqueIdentifier).Value  = context.ActivityId;
            cmd.Parameters.Add("@AccessToken", SqlDbType.VarChar).Value          = accessToken;
            cmd.Parameters.Add("@ExecutionId", SqlDbType.UniqueIdentifier).Value = context.ExecutionId;
            cmd.Parameters.Add("@Method", SqlDbType.NVarChar).Value      = context.Method.ToString();
            cmd.Parameters.Add("@URI", SqlDbType.NVarChar).Value         = context.RequestUri.ToString();
            cmd.Parameters.Add("@Direction", SqlDbType.Bit).Value        = direction;
            cmd.Parameters.Add("@StatusCode", SqlDbType.NVarChar).Value  = statusCodeStr;
            cmd.Parameters.Add("@JsonMessage", SqlDbType.NVarChar).Value = message;
            cmd.Parameters.Add("@Moment", SqlDbType.DateTime).Value      = DateTime.UtcNow;

            try
            {
                await cmd.ExecuteNonQueryAsync();
            }
            catch (SqlException ex)
            {
                throw new ZincException(ER.Rest_SqlServer_ExecuteNonQuery, ex, Database);
            }


            /*
             *
             */
            try
            {
                conn.Close();
            }
            catch (SqlException)
            {
            }
        }
コード例 #5
0
        /// <summary />
        protected override async Task HandleRequest(RestExecutionContext context, byte[] message)
        {
            string asString = Encoding.UTF8.GetString(message);

            await Journal(context, false, null, asString);
        }
コード例 #6
0
        /// <summary>
        /// Mandatory <see cref="DelegatingHandler" /> for Zinc services, since it will
        /// ensure that the expected headers are in place.
        /// </summary>
        /// <param name="request">The HTTP request message to send to the server.</param>
        /// <param name="cancellationToken">A cancellation token to cancel operation.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        /// <remarks>
        /// When configuring the WebAPI pipeline, <see cref="ExecutionHandler" /> must
        /// always be placed first.
        /// </remarks>
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            #region Validations

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            #endregion


            /*
             * Ensures that the request contains valid headers for ActivityId and
             * ExecutionId.
             */
            RestExecutionContext ctx = new RestExecutionContext();
            ctx.ExecutionId = Guid.NewGuid();
            ctx.ActivityId  = Guid.Empty;
            ctx.Method      = request.Method;
            ctx.RequestUri  = request.RequestUri;
            ctx.MomentStart = PreciseDateTime.UtcNow;


            /*
             *
             */
            if (request.Headers.Contains(ZnHeaders.ActivityId) == true)
            {
                var  h = request.Headers.First(x => x.Key == ZnHeaders.ActivityId);
                var  v = h.Value.First();
                Guid activityId;

                try
                {
                    activityId = new Guid(v);
                }
                catch (FormatException)
                {
                    activityId = Guid.Empty;
                }
                catch (ArgumentNullException)
                {
                    activityId = Guid.Empty;
                }

                ctx.ActivityId = activityId;
            }


            /*
             *
             */
            if (request.Headers.Authorization != null &&
                request.Headers.Authorization.Scheme == "Bearer")
            {
                ctx.AccessToken = request.Headers.Authorization.Parameter;
            }


            /*
             *
             */
            request.Properties[RestExecutionContext.PropertyName] = ctx;


            /*
             * Defer execution to the actual implementation.
             */
            var response = await base.SendAsync(request, cancellationToken);


            /*
             *
             */
            response.Headers.Add(ZnHeaders.ExecutionId, ctx.ExecutionId.ToString("D"));
            response.Headers.Add(ZnHeaders.MomentStart, ctx.MomentStart.ToString("O", CultureInfo.InvariantCulture));
            response.Headers.Add(ZnHeaders.MomentEnd, DateTime.UtcNow.ToString("O", CultureInfo.InvariantCulture));

            return(response);
        }
コード例 #7
0
 /// <summary />
 protected abstract Task HandleResponse(RestExecutionContext context, HttpStatusCode statusCode, byte[] message);
コード例 #8
0
 /// <summary />
 protected abstract Task HandleRequest(RestExecutionContext context, byte[] message);