/// <summary> /// Looks for first plugin of this type in Plugins. /// Reflection performance penalty. /// </summary> public T GetPlugin <T>() where T : class, IPlugin { return(Plugins.FirstOrDefault(x => x is T) as T); }
public bool HasPlugin <T>() where T : class, IPlugin { return(Plugins.FirstOrDefault(x => x is T) != null); }
//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('/'); } 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>(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>()); } foreach (var callback in AfterInitCallbacks) { try { callback(this); } catch (Exception ex) { OnStartupException(ex); } } ReadyAt = DateTime.UtcNow; }
//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; } if (config.EnableOptimizations) { MemoryStreamFactory.UseRecyclableMemoryStream = 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) { 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); } ReadyAt = DateTime.UtcNow; }
/// <summary> /// Initializes the AppHost. /// Calls the <see cref="Configure"/> method. /// Should be called before start. /// </summary> public virtual ServiceStackHost Init() { if (Instance != null) { throw new InvalidDataException($"ServiceStackHost.Instance has already been set ({Instance.GetType().Name})"); } Service.GlobalResolver = Instance = this; if (ServiceController == null) { ServiceController = CreateServiceController(ServiceAssemblies.ToArray()); } Config = HostConfig.ResetInstance(); OnConfigLoad(); AbstractVirtualFileBase.ScanSkipPaths = Config.ScanSkipPaths; ResourceVirtualDirectory.EmbeddedResourceTreatAsFiles = Config.EmbeddedResourceTreatAsFiles; Config.DebugMode = GetType().GetAssembly().IsDebugBuild(); OnBeforeInit(); ServiceController.Init(); BeforeConfigure.Each(fn => fn(this)); Configure(Container); AfterConfigure.Each(fn => fn(this)); if (Config.StrictMode == null && Config.DebugMode) { Config.StrictMode = true; } if (!Config.DebugMode) { Plugins.RemoveAll(x => x is RequestInfoFeature); } ConfigurePlugins(); List <IVirtualPathProvider> pathProviders = null; if (VirtualFileSources == null) { pathProviders = GetVirtualFileSources().Where(x => x != null).ToList(); VirtualFileSources = pathProviders.Count > 1 ? new MultiVirtualFiles(pathProviders.ToArray()) : pathProviders.First(); } if (VirtualFiles == null) { VirtualFiles = pathProviders?.FirstOrDefault(x => x is FileSystemVirtualFiles) as IVirtualFiles ?? GetVirtualFileSources().FirstOrDefault(x => x is FileSystemVirtualFiles) as IVirtualFiles; } OnAfterInit(); LogInitComplete(); HttpHandlerFactory.Init(); return(this); }
public virtual List <string> GetMetadataPluginIds() { var pluginIds = Plugins.OfType <IHasStringId>().Map(x => x.Id); return(pluginIds); }
//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); } 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('/'); } var specifiedContentType = config.DefaultContentType; //Before plugins loaded ConfigurePlugins(); LoadPlugin(Plugins.ToArray()); pluginsLoaded = true; AfterPluginsLoaded(specifiedContentType); if (!Container.Exists <ICacheClient>()) { if (Container.Exists <IRedisClientsManager>()) { Container.Register(c => c.Resolve <IRedisClientsManager>().GetCacheClient()); } else { Container.Register <ICacheClient>(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>()); } ReadyAt = DateTime.UtcNow; }
//After configure called public void OnAfterInit() { AfterInitAt = DateTime.UtcNow; if (config.EnableFeatures != Feature.All) { if ((Feature.Xml & config.EnableFeatures) != Feature.Xml) { config.IgnoreFormatsInMetadata.Add("xml"); } if ((Feature.Json & config.EnableFeatures) != Feature.Json) { config.IgnoreFormatsInMetadata.Add("json"); } if ((Feature.Jsv & config.EnableFeatures) != Feature.Jsv) { config.IgnoreFormatsInMetadata.Add("jsv"); } if ((Feature.Csv & config.EnableFeatures) != Feature.Csv) { config.IgnoreFormatsInMetadata.Add("csv"); } if ((Feature.Html & config.EnableFeatures) != Feature.Html) { config.IgnoreFormatsInMetadata.Add("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); } 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.ServiceStackHandlerFactoryPath != null) { config.ServiceStackHandlerFactoryPath = config.ServiceStackHandlerFactoryPath.TrimStart('/'); } var specifiedContentType = config.DefaultContentType; //Before plugins loaded ConfigurePlugins(); LoadPlugin(Plugins.ToArray()); pluginsLoaded = true; AfterPluginsLoaded(specifiedContentType); var registeredCacheClient = TryResolve <ICacheClient>(); using (registeredCacheClient) { if (registeredCacheClient == null) { Container.Register <ICacheClient>(new MemoryCacheClient()); } } var registeredMqService = TryResolve <IMessageService>(); var registeredMqFactory = TryResolve <IMessageFactory>(); if (registeredMqService != null && registeredMqFactory == null) { Container.Register(c => registeredMqService.MessageFactory); } ReadyAt = DateTime.UtcNow; }
/// <summary> /// Initializes the AppHost. /// Calls the <see cref="Configure"/> method. /// Should be called before start. /// </summary> public virtual ServiceStackHost Init() { if (Instance != null) { throw new InvalidDataException($"ServiceStackHost.Instance has already been set ({Instance.GetType().Name})"); } Service.GlobalResolver = Instance = this; RegisterLicenseKey(AppSettings.GetNullableString("servicestack:license")); if (ServiceController == null) { ServiceController = CreateServiceController(ServiceAssemblies.ToArray()); } Config = HostConfig.ResetInstance(); OnConfigLoad(); AbstractVirtualFileBase.ScanSkipPaths = Config.ScanSkipPaths; ResourceVirtualDirectory.EmbeddedResourceTreatAsFiles = Config.EmbeddedResourceTreatAsFiles; OnBeforeInit(); ServiceController.Init(); var scanAssemblies = new List <Assembly>(ServiceAssemblies); scanAssemblies.AddIfNotExists(GetType().Assembly); RunConfigureAppHosts(scanAssemblies.SelectMany(x => x.GetTypes()) .Where(x => x.HasInterface(typeof(IConfigureAppHost)))); BeforeConfigure.Each(fn => fn(this)); Configure(Container); AfterConfigure.Each(fn => fn(this)); RunConfigureAppHosts(scanAssemblies.SelectMany(x => x.GetTypes()) .Where(x => x.HasInterface(typeof(IPostConfigureAppHost)))); if (Config.StrictMode == null && Config.DebugMode) { Config.StrictMode = true; } if (!Config.DebugMode) { Plugins.RemoveAll(x => x is RequestInfoFeature); } ConfigurePlugins(); List <IVirtualPathProvider> pathProviders = null; if (VirtualFileSources == null) { pathProviders = GetVirtualFileSources().Where(x => x != null).ToList(); VirtualFileSources = pathProviders.Count > 1 ? new MultiVirtualFiles(pathProviders.ToArray()) : pathProviders.First(); } if (VirtualFiles == null) { VirtualFiles = pathProviders?.FirstOrDefault(x => x is FileSystemVirtualFiles) as IVirtualFiles ?? GetVirtualFileSources().FirstOrDefault(x => x is FileSystemVirtualFiles) as IVirtualFiles; } OnAfterInit(); PopulateArrayFilters(); LogInitComplete(); HttpHandlerFactory.Init(); foreach (var callback in AfterInitCallbacks) { callback(this); } ReadyAt = DateTime.UtcNow; return(this); }