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));
            }
        }
Exemple #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));
        }
        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);
        }
        /// <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);
        }
        /// <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();
        }
        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);
        }
 private ServiceUnitResponse HandleError(ServiceUnitContext context,
                                         IUriProcessResolver resolver,
                                         ServiceUnitResponse response)
 {
     if (resolver != null)
     {
         foreach (var handler in resolver.HandleErrorPipeline)
         {
             handler.Invoke(context, response);
         }
     }
     return(response);
 }