示例#1
0
        //After configure called
        public virtual void OnAfterInit()
        {
            AfterInitAt = DateTime.UtcNow;

            if (config.EnableFeatures != Feature.All)
            {
                if ((Feature.Xml & config.EnableFeatures) != Feature.Xml)
                {
                    config.IgnoreFormatsInMetadata.Add("xml");
                    Config.PreferredContentTypes.Remove(MimeTypes.Xml);
                }
                if ((Feature.Json & config.EnableFeatures) != Feature.Json)
                {
                    config.IgnoreFormatsInMetadata.Add("json");
                    Config.PreferredContentTypes.Remove(MimeTypes.Json);
                }
                if ((Feature.Jsv & config.EnableFeatures) != Feature.Jsv)
                {
                    config.IgnoreFormatsInMetadata.Add("jsv");
                    Config.PreferredContentTypes.Remove(MimeTypes.Jsv);
                }
                if ((Feature.Csv & config.EnableFeatures) != Feature.Csv)
                {
                    config.IgnoreFormatsInMetadata.Add("csv");
                    Config.PreferredContentTypes.Remove(MimeTypes.Csv);
                }
                if ((Feature.Html & config.EnableFeatures) != Feature.Html)
                {
                    config.IgnoreFormatsInMetadata.Add("html");
                    Config.PreferredContentTypes.Remove(MimeTypes.Html);
                }
                if ((Feature.Soap11 & config.EnableFeatures) != Feature.Soap11)
                {
                    config.IgnoreFormatsInMetadata.Add("soap11");
                }
                if ((Feature.Soap12 & config.EnableFeatures) != Feature.Soap12)
                {
                    config.IgnoreFormatsInMetadata.Add("soap12");
                }
            }

            if ((Feature.Html & config.EnableFeatures) != Feature.Html)
            {
                Plugins.RemoveAll(x => x is HtmlFormat);
            }

            if ((Feature.Csv & config.EnableFeatures) != Feature.Csv)
            {
                Plugins.RemoveAll(x => x is CsvFormat);
            }

            if ((Feature.Markdown & config.EnableFeatures) != Feature.Markdown)
            {
                Plugins.RemoveAll(x => x is MarkdownFormat);
            }

            if ((Feature.PredefinedRoutes & config.EnableFeatures) != Feature.PredefinedRoutes)
            {
                Plugins.RemoveAll(x => x is PredefinedRoutesFeature);
            }

            if ((Feature.Metadata & config.EnableFeatures) != Feature.Metadata)
            {
                Plugins.RemoveAll(x => x is MetadataFeature);
                Plugins.RemoveAll(x => x is NativeTypesFeature);
            }

            if ((Feature.RequestInfo & config.EnableFeatures) != Feature.RequestInfo)
            {
                Plugins.RemoveAll(x => x is RequestInfoFeature);
            }

            if ((Feature.Razor & config.EnableFeatures) != Feature.Razor)
            {
                Plugins.RemoveAll(x => x is IRazorPlugin);    //external
            }
            if ((Feature.ProtoBuf & config.EnableFeatures) != Feature.ProtoBuf)
            {
                Plugins.RemoveAll(x => x is IProtoBufPlugin); //external
            }
            if ((Feature.MsgPack & config.EnableFeatures) != Feature.MsgPack)
            {
                Plugins.RemoveAll(x => x is IMsgPackPlugin);  //external
            }
            if (config.HandlerFactoryPath != null)
            {
                config.HandlerFactoryPath = config.HandlerFactoryPath.TrimStart('/');
            }

            if (config.UseCamelCase)
            {
                JsConfig.EmitCamelCaseNames = true;
            }

            var specifiedContentType = config.DefaultContentType; //Before plugins loaded

            var plugins = Plugins.ToArray();

            delayLoadPlugin = true;
            LoadPluginsInternal(plugins);

            AfterPluginsLoaded(specifiedContentType);

            if (!TestMode && Container.Exists <IAuthSession>())
            {
                throw new Exception(ErrorMessages.ShouldNotRegisterAuthSession);
            }

            if (!Container.Exists <IAppSettings>())
            {
                Container.Register(AppSettings);
            }

            if (!Container.Exists <ICacheClient>())
            {
                if (Container.Exists <IRedisClientsManager>())
                {
                    Container.Register(c => c.Resolve <IRedisClientsManager>().GetCacheClient());
                }
                else
                {
                    Container.Register <ICacheClient>(ServiceExtensions.DefaultCache);
                }
            }

            if (!Container.Exists <MemoryCacheClient>())
            {
                Container.Register(ServiceExtensions.DefaultCache);
            }

            if (Container.Exists <IMessageService>() &&
                !Container.Exists <IMessageFactory>())
            {
                Container.Register(c => c.Resolve <IMessageService>().MessageFactory);
            }

            if (Container.Exists <IUserAuthRepository>() &&
                !Container.Exists <IAuthRepository>())
            {
                Container.Register <IAuthRepository>(c => c.Resolve <IUserAuthRepository>());
            }

            if (config.LogUnobservedTaskExceptions)
            {
                System.Threading.Tasks.TaskScheduler.UnobservedTaskException += (sender, args) =>
                {
                    args.SetObserved();
                    args.Exception.Handle(ex =>
                    {
                        lock (AsyncErrors)
                        {
                            AsyncErrors.Add(DtoUtils.CreateErrorResponse(null, ex).GetResponseStatus());
                            return(true);
                        }
                    });
                };
            }

            foreach (var callback in AfterInitCallbacks)
            {
                try
                {
                    callback(this);
                }
                catch (Exception ex)
                {
                    OnStartupException(ex);
                }
            }

            ReadyAt = DateTime.UtcNow;
        }
示例#2
0
 public virtual void OnStartupException(Exception ex)
 {
     this.StartUpErrors.Add(DtoUtils.CreateErrorResponse(null, ex).GetResponseStatus());
 }
        //After configure called
        public virtual void InitFinal()
        {
            if (Config.EnableFeatures != Feature.All)
            {
                if ((Feature.Xml & Config.EnableFeatures) != Feature.Xml)
                {
                    Config.IgnoreFormatsInMetadata.Add("xml");
                    Config.PreferredContentTypes.Remove(MimeTypes.Xml);
                }
                if ((Feature.Json & Config.EnableFeatures) != Feature.Json)
                {
                    Config.IgnoreFormatsInMetadata.Add("json");
                    Config.PreferredContentTypes.Remove(MimeTypes.Json);
                }
                if ((Feature.Jsv & Config.EnableFeatures) != Feature.Jsv)
                {
                    Config.IgnoreFormatsInMetadata.Add("jsv");
                    Config.PreferredContentTypes.Remove(MimeTypes.Jsv);
                }
                if ((Feature.Csv & Config.EnableFeatures) != Feature.Csv)
                {
                    Config.IgnoreFormatsInMetadata.Add("csv");
                    Config.PreferredContentTypes.Remove(MimeTypes.Csv);
                }
                if ((Feature.Html & Config.EnableFeatures) != Feature.Html)
                {
                    Config.IgnoreFormatsInMetadata.Add("html");
                    Config.PreferredContentTypes.Remove(MimeTypes.Html);
                }
                if ((Feature.Soap11 & Config.EnableFeatures) != Feature.Soap11)
                {
                    Config.IgnoreFormatsInMetadata.Add("soap11");
                }
                if ((Feature.Soap12 & Config.EnableFeatures) != Feature.Soap12)
                {
                    Config.IgnoreFormatsInMetadata.Add("soap12");
                }
            }

            if ((Feature.Html & Config.EnableFeatures) != Feature.Html)
            {
                Plugins.RemoveAll(x => x is HtmlFormat);
            }

            if ((Feature.Csv & Config.EnableFeatures) != Feature.Csv)
            {
                Plugins.RemoveAll(x => x is CsvFormat);
            }

            if ((Feature.PredefinedRoutes & Config.EnableFeatures) != Feature.PredefinedRoutes)
            {
                Plugins.RemoveAll(x => x is PredefinedRoutesFeature);
            }

            if ((Feature.Metadata & Config.EnableFeatures) != Feature.Metadata)
            {
                Plugins.RemoveAll(x => x is MetadataFeature);
                Plugins.RemoveAll(x => x is NativeTypesFeature);
            }

            if ((Feature.RequestInfo & Config.EnableFeatures) != Feature.RequestInfo)
            {
                Plugins.RemoveAll(x => x is RequestInfoFeature);
            }

            if ((Feature.Razor & Config.EnableFeatures) != Feature.Razor)
            {
                Plugins.RemoveAll(x => x is IRazorPlugin);    //external
            }
            if ((Feature.ProtoBuf & Config.EnableFeatures) != Feature.ProtoBuf)
            {
                Plugins.RemoveAll(x => x is IProtoBufPlugin); //external
            }
            if ((Feature.MsgPack & Config.EnableFeatures) != Feature.MsgPack)
            {
                Plugins.RemoveAll(x => x is IMsgPackPlugin);  //external
            }
            var plugins = Plugins.ToArray();

            delayLoadPlugin = true;
            LoadPluginsInternal(plugins);

            AfterPluginsLoaded();

            Metadata.Config.IgnoreFormats = Config.IgnoreFormatsInMetadata;

            if (!TestMode && Container.Exists <IAuthSession>())
            {
                throw new Exception(ErrorMessages.ShouldNotRegisterAuthSession);
            }

            if (!Container.Exists <ICacheClient>())
            {
                if (Container.Exists <IRedisClientsManager>())
                {
                    Container.Register(c => c.Resolve <IRedisClientsManager>().GetCacheClient());
                }
                else
                {
                    Container.Register <ICacheClient>(new MemoryCacheClient());
                }
            }

            if (!Container.Exists <MemoryCacheClient>())
            {
                Container.Register(new MemoryCacheClient());
            }

            if (Container.Exists <IMessageService>() &&
                !Container.Exists <IMessageFactory>())
            {
                Container.Register(c => c.Resolve <IMessageService>().MessageFactory);
            }

            if (Container.Exists <IUserAuthRepository>() &&
                !Container.Exists <IAuthRepository>())
            {
                Container.Register <IAuthRepository>(c => c.Resolve <IUserAuthRepository>());
            }

            if (Config.UseJsObject)
            {
                JS.Configure();
            }

            if (Config.LogUnobservedTaskExceptions)
            {
                TaskScheduler.UnobservedTaskException += (sender, args) =>
                {
                    args.SetObserved();
                    args.Exception.Handle(ex =>
                    {
                        lock (AsyncErrors)
                        {
                            AsyncErrors.Add(DtoUtils.CreateErrorResponse(null, ex).GetResponseStatus());
                            return(true);
                        }
                    });
                };
            }

            foreach (var callback in AfterInitCallbacks)
            {
                callback(this);
            }
            ////Register any routes configured on Routes
            //foreach (var restPath in Routes)
            //{
            //    ServiceController.RegisterRestPath(restPath);

            //    //Auto add Route Attributes so they're available in T.ToUrl() extension methods
            //    restPath.RequestType
            //        .AddAttributes(new RouteAttribute(restPath.Path, restPath.AllowedVerbs)
            //        {
            //            Priority = restPath.Priority,
            //            Summary = restPath.Summary,
            //            Notes = restPath.Notes,
            //        });
            //}

            HttpHandlerFactory.Init();
        }