private T GetService <T>(Type serviceClass, ServiceConfiguration serviceConfiguration = null) where T : IServiceModule { NonNull <Type>(serviceClass, "serviceClass"); if (serviceInstanceCache.IsCached(serviceClass)) { return((T)serviceInstanceCache.GetCachedInstance(serviceClass)); } // Try to instantiate it IServiceModule service = TryToInstantiate(serviceClass); if (service == null) { // There are no services registered for this interface. throw new ServiceModuleInstanceNotFoundException(String.Format("No instance has been registered for interface {0}", serviceClass.Name)); } serviceConfiguration = ResolveServiceConfiguration(service, serviceConfiguration); if (service.RequiresConfiguration && serviceConfiguration == null) { throw new ConfigurationNotFoundException(String.Format("No configuration has been found for service {0}", service.Type)); } service.Configure(this, serviceConfiguration); serviceInstanceCache.Add <T>(service); return((T)service); }
internal void SetInstance(Type type, IServiceModule instance) { if (!dictionnary.ContainsKey(type)) { dictionnary.Add(type, instance); } }
public Bootstrapper(IServiceCollection services, IServiceModule customDbModule = null) { this.services = services; modules = new[] { customDbModule ?? new SqliteDatabase() }; }
private ServiceConfiguration ResolveServiceConfiguration(IServiceModule serviceModule, ServiceConfiguration conf) { ServiceConfiguration result = conf; if (conf == null) { ServiceConfiguration[] confs = GetServiceConfigurationsByType(serviceModule.Type); if (confs != null && confs.Length != 0) { result = confs[0]; } } return(result); }
public Startup(IHostingEnvironment env) { this.env = env; var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); this.config = builder.Build(); this.repositoryModule = new DataAccessModule(); this.serviceModule = new ServiceModule(); }
public void Add <T>(IServiceModule serviceModule) { if (serviceModule.Id != null) { cacheById[serviceModule.Id] = serviceModule; } Type type = typeof(T); if (!cacheByType.ContainsKey(type)) { // We always cache the first instance of a service by type cacheByType[typeof(T)] = serviceModule; } }
/// <summary> /// Gets a specific service from the current builder. /// </summary> /// <typeparam name="T">The type of the service.</typeparam> /// <returns>The service from the builder.</returns> public T GetService <T>() where T : IServiceModule { if (dictionnary.TryGetValue(typeof(T), out IServiceModule serviceCache)) { return((T)serviceCache); } else { if (serviceResolvers.TryGetValue(typeof(T), out Func <IServiceModule> serviceResolver)) { IServiceModule service = serviceResolver.Invoke(); dictionnary.Add(typeof(T), service); return((T)service); } else { throw new KeyNotFoundException(); } } }
/// <summary> /// Initializes services using dependency injection. /// </summary> private void InjectServices() { IEnumerable <FieldInfo> nonPublicFields = GetType().GetAllFields(BindingFlags.Instance | BindingFlags.NonPublic).Reverse(); foreach (FieldInfo nonPublicField in nonPublicFields) { if (nonPublicField.GetCustomAttribute(typeof(InjectableServiceAttribute)) is InjectableServiceAttribute injectableServiceAttribute) { IServiceModule service = unityServiceLocator.GetInstance(nonPublicField.FieldType); if (service != null) { nonPublicField.SetValue(this, service); } else { logger.Warning($"The service '{nonPublicField.Name}' of '{GetType().Name}' does not exist ({gameObject.name})"); } } } }
/// <summary> /// Returns instance of a service module. /// </summary> /// <typeparam name="T">service module type</typeparam> /// <param name="serviceClass">service module class type</param> /// <param name="serviceConfiguration">service configuration</param> /// <returns></returns> private T GetInstance <T>(Type serviceClass, ServiceConfiguration serviceConfiguration) where T : IServiceModule { Contract.Requires(serviceClass != null); if (services.ContainsKey(serviceClass)) { return((T)services[serviceClass]); } IServiceModule serviceModule = Activator.CreateInstance(serviceClass) as IServiceModule; var serviceCfg = serviceConfiguration; if (serviceCfg == null) { serviceCfg = GetServiceConfiguration(serviceModule.Type); } if (serviceCfg == null && serviceModule.RequiresConfiguration) { throw new ConfigurationNotFoundException($"{serviceModule.Type} not found on " + ConfigFileName); } serviceModule.Configure(this, serviceCfg); services[serviceClass] = serviceModule; return((T)serviceModule); }
/// Register a set of services public void Register(IServiceModule module) { module.Register(this); }
public static IServiceCollection RegisterServiceModule <T>(this IServiceCollection services) where T : class, IServiceModule { IServiceModule serviceModule = Activator.CreateInstance <T>(); return(services.RegisterServiceModule(serviceModule)); }
public static IServiceCollection RegisterModule(this IServiceCollection services, IServiceModule module) { return(module.Register(services)); }
public static void AddServiceModule(this IServiceCollection serviceCollection, IServiceModule serviceModule) => serviceModule.RegisterTypes(serviceCollection);
public void UnregisterModule(IServiceModule serviceModule) { Modules.Remove(serviceModule); }
public void RegisterModule(IServiceModule serviceModule) { Modules.Add(serviceModule); }
private static void CreateServiceHost(IServiceModule serviceModule, string endpointAddress) { var address = new Uri(BaseServiceUri, endpointAddress); var serviceHost = new ServiceHost(serviceModule.GetImplementation(), address); var quotas = new XmlDictionaryReaderQuotas { MaxStringContentLength = int.MaxValue, MaxArrayLength = int.MaxValue, MaxDepth = int.MaxValue }; var binding = new WSHttpBinding(SecurityMode.None) { MaxReceivedMessageSize = int.MaxValue, ReaderQuotas = quotas, ReceiveTimeout = new TimeSpan(long.MaxValue), SendTimeout = new TimeSpan(long.MaxValue), }; serviceHost.AddServiceEndpoint(serviceModule.ContractType, binding, ""); serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior()); serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true }); #if DEBUG serviceHost.Description.Behaviors.Remove(typeof(ServiceDebugBehavior)); serviceHost.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true }); #endif serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint()); foreach (ServiceEndpoint endpoint in serviceHost.Description.Endpoints) { endpoint.Behaviors.Add(new ReferencePreservingDataContractFormatEndpointBehavior()); } serviceHost.Open(); ServiceHosts.Add(serviceHost); }
public static IServiceCollection RegisterServiceModule(this IServiceCollection services, IServiceModule serviceModule) { serviceModule.Register(services); return(services); }