コード例 #1
0
        public ServiceUnitResponse Execute(ServiceUnitContext suContext, IUriProcessResolver resolver)
        {
            Contract.NotNull(suContext, "suContext");
            Contract.NotNull(resolver, "resolver");

            using (var logger = new TraceLogger(suContext))
            {
                return(ExecuteInternal(suContext, resolver));
            }
        }
コード例 #2
0
        /// <summary>
        /// サービスユニットのルートの物理フォルダ名を取得します。
        /// </summary>
        /// <param name="context"><see cref="ServiceUnitContext"/></param>
        /// <returns>物理フォルダ名</returns>
        public static string GetFisicalDirectory(this ServiceUnitContext context)
        {
            Contract.NotNull(context, "context");
            if (string.IsNullOrEmpty(context.ServiceUnitName))
            {
                throw new InvalidOperationException();
            }
            var env = context.ServiceContainer.GetService <IApplicationEnvironment>();

            return(env.MapPath("~/ServiceUnits/" + context.ServiceUnitName));
        }
コード例 #3
0
        private ServiceUnitResponse ExecuteInternal(ServiceUnitContext suContext, IUriProcessResolver resolver)
        {
            var executionType            = resolver.GetExecutionType(suContext);
            ServiceUnitResponse response = null;

            if (executionType == null)
            {
                response = new ServiceUnitResponse(HttpStatusCode.NotFound);
                HandleError(suContext, resolver, response);
                return(response);
            }

            var instance = resolver.CreateInstance(suContext, executionType);

            if (instance == null)
            {
                response = new ServiceUnitResponse(HttpStatusCode.NotFound);
                HandleError(suContext, resolver, response);
                return(response);
            }

            var action = resolver.GetAction(suContext, instance);

            if (action == null)
            {
                response = new ServiceUnitResponse(HttpStatusCode.NotFound);
                HandleError(suContext, resolver, response);
                return(response);
            }

            try
            {
                response = resolver.InvokeAction(suContext, action);
                if (response == null)
                {
                    return(new ServiceUnitResponse(HttpStatusCode.NotFound));
                }

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    HandleError(suContext, resolver, response);
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(suContext, ex);
                return(HandleError(suContext, resolver, new ServiceUnitResponse(HttpStatusCode.InternalServerError)
                {
                    Data = ex
                }));
            }

            return(response);
        }
コード例 #4
0
        /// <summary>
        /// 指定された <see cref="ServiceUnitContext"/> および <see cref="PathInfo"/> を利用してインスタンスを初期化します。
        /// </summary>
        /// <param name="context">実行コンテキスト</param>
        /// <param name="info">パス情報</param>
        public ServiceUnitRequest(ServiceUnitContext context, PathInfo info)
        {
            Contract.NotNull(context, "context");
            Contract.NotNull(info, "info");

            this.Path                = info.Path;
            this.ProcessPath         = info.ProcessPath;
            this.ProcessType         = info.ProcessType;
            this.SpecificProcessPath = info.SpecificProcessPath;
            this.Context             = context;
            this.Query               = new ReadOnlyDictionary <string, string>(info.Query);
        }
コード例 #5
0
        /// <summary>
        /// <see cref="ServiceUnitContext"/> を指定してインスタンスを初期化します。
        /// </summary>
        /// <param name="context"><see cref="ServiceUnitContext"/></param>
        public TraceLogger(ServiceUnitContext context)
        {
            Contract.NotNull(context, "context");

            this.context = context;
            if (context.LogContext.Logger == null)
            {
                return;
            }
            this.logger = context.LogContext.Logger;
            this.start  = DateTime.Now;
            stopwatch.Start();
        }
コード例 #6
0
        private void WriteErrorLog(ServiceUnitContext serviceUnitContext, Exception ex)
        {
            var data = new LogData()
            {
                Exception = ex,
                Uri       = serviceUnitContext.Request.Path,
                User      = serviceUnitContext.User == null ? null : serviceUnitContext.User.Identity.Name,
                LogName   = "error",
                LogId     = serviceUnitContext.Id
            };

            serviceUnitContext.LogContext.Logger.Error(data);
        }
コード例 #7
0
 private ServiceUnitResponse HandleError(ServiceUnitContext context,
                                         IUriProcessResolver resolver,
                                         ServiceUnitResponse response)
 {
     if (resolver != null)
     {
         foreach (var handler in resolver.HandleErrorPipeline)
         {
             handler.Invoke(context, response);
         }
     }
     return(response);
 }