/// <summary> /// Starts the server application. /// </summary> /// <param name="configuration">The configuration.</param> public void StartService(ApplicationConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } lock (m_lock) { if (m_state == ReverseConnectManagerState.Started) { throw new ServiceResultException(StatusCodes.BadInvalidState); } try { OnUpdateConfiguration(configuration); StartService(); // monitor the configuration file. if (!String.IsNullOrEmpty(configuration.SourceFilePath)) { m_configurationWatcher = new ConfigurationWatcher(configuration); m_configurationWatcher.Changed += OnConfigurationChanged; } } catch (Exception e) { Utils.Trace(e, "Unexpected error starting reverse connect manager."); m_state = ReverseConnectManagerState.Errored; ServiceResult error = ServiceResult.Create(e, StatusCodes.BadInternalError, "Unexpected error starting application"); throw new ServiceResultException(error); } } }
public async Task Watcher() { // Retrieve the connection string from the configuration store. // You can get the string from your Azure portal. var connectionString = Environment.GetEnvironmentVariable("APP_CONFIG_CONNECTION"); // Instantiate a client that will be used to call the service. var client = new ConfigurationClient(connectionString); // Setup the watcher to watch "key1" and "key2" var watcher = new ConfigurationWatcher(client, "key1", "key2"); watcher.SettingChanged += (sender, e) => { Console.WriteLine($"old value: {e.Older.Value}"); Console.WriteLine($"new value: {e.Newer.Value}"); }; // Print errors occuring during watching watcher.Error += (sender, e) => { Console.WriteLine($"Error {e.Message}"); }; // start watching watcher.Start(); // watch for 1 second await Task.Delay(1000); // stop watching await watcher.Stop(); }
private static T GetSourcedObject <T>(string configSource) where T : class { T sourcedObject = default(T); Type objectType = typeof(T); try { XmlSerializer ser = new XmlSerializer(objectType); if (!String.IsNullOrEmpty(configSource)) { string path = GetFilePath(configSource); XmlReader reader = XmlReader.Create(path); sourcedObject = ser.Deserialize(reader) as T; reader.Close(); ConfigurationWatcher.WatchFile(path, ReloadConfig); } } catch (Exception ex) { if (log.IsErrorEnabled) { log.ErrorFormat("Error getting sourced config of type {0}: {1}", objectType.FullName, ex); } //we want callers to know there was a problem, also all of the config file needs to be loaded, //for relay to function throw; } return(sourcedObject); }
private void SpawnConfigurationWatcher(CancellationToken cancellationToken) { Task.Run(async() => { var configurationWatcher = new ConfigurationWatcher(configurationManager, configurationPathProvider, logger); return(await configurationWatcher.StartWatch(cancellationToken)); }); }
private static SearchConfiguration LoadConfiguration() { var watcher = new ConfigurationWatcher <SearchConfiguration>( GetConfigurationPath(), XMLIO.Load <SearchConfiguration>, XMLIO.Save, () => SearchConfiguration.Default); return(watcher.GetConfiguration()); }
private static T GetServerSourcedObject <T>(string configSource) where T : class { Type objectType = typeof(T); ConfigurationWatcher.WatchRemoteSection(configSource, ReloadConfig); XmlNode objectNode = ConfigurationClient.GetSectionXml(configSource); XmlSerializer ser = new XmlSerializer(objectType); T config = ser.Deserialize(new XmlNodeReader(objectNode)) as T; return(config); }
private static SearchConfiguration LoadConfiguration() { _configPathResolver = new ConfigurationPathResolver(); ConfigFilePath = _configPathResolver.GetConfigurationPath(); var watcher = new ConfigurationWatcher <SearchConfiguration>( ConfigFilePath, XMLIO.Load <SearchConfiguration>, XMLIO.Save, () => SearchConfiguration.Default); return(watcher.GetConfiguration()); }
/// <summary> /// Application_End method /// </summary> protected void Application_End() { serviceEventSource.ApplicationEnding(this.GetType().Name, JWTestExtensionResourceProviderApplication.CurrentVersion); ConfigurationWatcher.Stop(); if (eventSource != null) { eventSource.Dispose(); eventSource = null; } serviceEventSource.ApplicationEnded(this.GetType().Name, JWTestExtensionResourceProviderApplication.CurrentVersion); }
/// <summary> /// /// </summary> /// <param name="serverEndPoint"></param> /// <param name="configurationWatcher"></param> public ConfigFileServer(EndPoint serverEndPoint, ConfigurationWatcher configurationWatcher) { this.sessions = new Dictionary <ulong, KeyValuePair <string, Encoding> >(); this.requestHandler = new ServerRequestHandler(serverEndPoint, this, new Protocol()); this.requestHandler.OnMessageReceived += this.RequestHandler_OnMessageReceived; this.requestHandler.OnConnectionClosed += this.RequestHandler_OnConnectionClosed; this.configurationWatcher = configurationWatcher; this.configurationWatcher.OnAppFileChanged += this.ConfigurationWatcher_OnAppFileChanged; this.configurationWatcher.OnAppFileDeleted += this.ConfigurationWatcher_OnAppFileDeleted; this.configurationWatcher.OnAppFileRenamed += this.ConfigurationWatcher_OnAppFileRenamed; this.configurationWatcher.OnShareFileChanged += this.ConfigurationWatcher_OnShareFileChanged; this.configurationWatcher.OnShareFileDeleted += this.ConfigurationWatcher_OnShareFileDeleted; this.configurationWatcher.OnShareFileRenamed += this.ConfigurationWatcher_OnShareFileRenamed; }
private static void WatchConfig(RelayNodeConfig mainConfig) { //either way, watch the file that represents it, since it might switch between local and not ConfigurationSection relayNodeConfigSection = ConfigurationFile.GetSection(ConfigSectionName); string configSource = String.Empty; if (relayNodeConfigSection != null) { configSource = relayNodeConfigSection.SectionInformation.ConfigSource; } string configPath = String.Empty; if (!String.IsNullOrEmpty((configSource))) { //if there's no config source, then the info is embedded in the app config, and that's the file we need to watch if (HttpContext.Current == null) { //but if there's an httpcontext, then we're in a web context, and you can't update an IIS config file without bouncing //the appdomain, so there's no point in watching it. configPath = Path.Combine(BasePath, configSource); } } else { configPath = Path.GetFullPath(ConfigurationFile.FilePath); } if (!string.IsNullOrEmpty(configPath)) { ConfigurationWatcher.WatchFile(configPath, ReloadConfig); } //if it came from config server, then register reload notification there if (mainConfig.UseConfigurationServer) { ConfigurationWatcher.WatchRemoteSection(mainConfig.ConfigurationServerSectionName, ReloadConfig); } }
/// <summary> /// Starts watching a file on the filesystem. /// </summary> /// <param name="configName">Name of the configuration to watch.</param> internal static void StartWatching(String configName) { ConfigurationWatcher watcher = new ConfigurationWatcher(configName); }
public static void PutWatch(String configName) { ConfigurationWatcher.StartWatching(configName); }
/// <summary> /// Starts the server application. /// </summary> /// <param name="configuration">The configuration.</param> protected override void StartApplication(ApplicationConfiguration configuration) { base.StartApplication(configuration); lock (m_lock) { try { // create the datastore for the instance. m_serverInternal = new ServerInternalData( ServerProperties, configuration, MessageContext, new CertificateValidator(), InstanceCertificate); // create the manager responsible for providing localized string resources. ResourceManager resourceManager = CreateResourceManager(m_serverInternal, configuration); // create the manager responsible for incoming requests. RequestManager requestManager = CreateRequestManager(m_serverInternal, configuration); // create the master node manager. MasterNodeManager masterNodeManager = CreateMasterNodeManager(m_serverInternal, configuration); // add the node manager to the datastore. m_serverInternal.SetNodeManager(masterNodeManager); // put the node manager into a state that allows it to be used by other objects. masterNodeManager.Startup(); // create the manager responsible for handling events. EventManager eventManager = CreateEventManager(m_serverInternal, configuration); // creates the server object. m_serverInternal.CreateServerObject( eventManager, resourceManager, requestManager); // do any additional processing now that the node manager is up and running. OnNodeManagerStarted(m_serverInternal); // create the manager responsible for aggregates. m_serverInternal.AggregateManager = CreateAggregateManager(m_serverInternal, configuration); // start the session manager. SessionManager sessionManager = CreateSessionManager(m_serverInternal, configuration); sessionManager.Startup(); // start the subscription manager. SubscriptionManager subscriptionManager = CreateSubscriptionManager(m_serverInternal, configuration); subscriptionManager.Startup(); // add the session manager to the datastore. m_serverInternal.SetSessionManager(sessionManager, subscriptionManager); ServerError = null; // setup registration information. lock (m_registrationLock) { m_bindingFactory = BindingFactory.Create(configuration, MessageContext); m_maxRegistrationInterval = configuration.ServerConfiguration.MaxRegistrationInterval; ApplicationDescription serverDescription = ServerDescription; m_registrationInfo = new RegisteredServer(); m_registrationInfo.ServerUri = serverDescription.ApplicationUri; m_registrationInfo.ServerNames.Add(serverDescription.ApplicationName); m_registrationInfo.ProductUri = serverDescription.ProductUri; m_registrationInfo.ServerType = serverDescription.ApplicationType; m_registrationInfo.GatewayServerUri = null; m_registrationInfo.IsOnline = true; m_registrationInfo.SemaphoreFilePath = null; // add all discovery urls. string computerName = Utils.GetHostName(); for (int ii = 0; ii < BaseAddresses.Count; ii++) { UriBuilder uri = new UriBuilder(BaseAddresses[ii].DiscoveryUrl); if (String.Compare(uri.Host, "localhost", StringComparison.OrdinalIgnoreCase) == 0) { uri.Host = computerName; } m_registrationInfo.DiscoveryUrls.Add(uri.ToString()); } // build list of registration endpoints. m_registrationEndpoints = new ConfiguredEndpointCollection(configuration); EndpointDescription endpoint = configuration.ServerConfiguration.RegistrationEndpoint; if (endpoint == null) { endpoint = new EndpointDescription(); endpoint.EndpointUrl = Utils.Format(Utils.DiscoveryUrls[0], "localhost"); endpoint.SecurityLevel = 0; endpoint.SecurityMode = MessageSecurityMode.SignAndEncrypt; endpoint.SecurityPolicyUri = SecurityPolicies.Basic128Rsa15; endpoint.Server.ApplicationType = ApplicationType.DiscoveryServer; } m_registrationEndpoints.Add(endpoint); m_minRegistrationInterval = 1000; m_lastRegistrationInterval = m_minRegistrationInterval; // start registration timer. if (m_registrationTimer != null) { m_registrationTimer.Dispose(); m_registrationTimer = null; } if (m_maxRegistrationInterval > 0) { m_registrationTimer = new Timer(OnRegisterServer, this, m_minRegistrationInterval, Timeout.Infinite); } } // set the server status as running. SetServerState(ServerState.Running); // all initialization is complete. OnServerStarted(m_serverInternal); // monitor the configuration file. if (!String.IsNullOrEmpty(configuration.SourceFilePath)) { m_configurationWatcher = new ConfigurationWatcher(configuration); m_configurationWatcher.Changed += new EventHandler<ConfigurationWatcherEventArgs>(this.OnConfigurationChanged); } } catch (Exception e) { Utils.Trace(e, "Unexpected error starting application"); m_serverInternal = null; ServiceResult error = ServiceResult.Create(e, StatusCodes.BadInternalError, "Unexpected error starting application"); ServerError = error; throw new ServiceResultException(error); } } }
/// <summary> /// 启动服务 /// </summary> /// <param name="configuration"></param> protected override void StartApplication(ApplicationConfiguration configuration) { lock (m_lock) { try { // create the datastore for the instance. m_serverInternal = new ServerInternalData( ServerProperties, configuration, MessageContext, new CertificateValidator(), InstanceCertificate); // create the manager responsible for providing localized string resources. ResourceManager resourceManager = CreateResourceManager(m_serverInternal, configuration); // create the manager responsible for incoming requests. RequestManager requestManager = new RequestManager(m_serverInternal); // create the master node manager. MasterNodeManager masterNodeManager = new MasterNodeManager(m_serverInternal, configuration, null); // add the node manager to the datastore. m_serverInternal.SetNodeManager(masterNodeManager); // put the node manager into a state that allows it to be used by other objects. masterNodeManager.Startup(); // create the manager responsible for handling events. EventManager eventManager = new EventManager(m_serverInternal, (uint)configuration.ServerConfiguration.MaxEventQueueSize); // creates the server object. m_serverInternal.CreateServerObject( eventManager, resourceManager, requestManager); // create the manager responsible for aggregates. m_serverInternal.AggregateManager = CreateAggregateManager(m_serverInternal, configuration); // start the session manager. SessionManager sessionManager = new SessionManager(m_serverInternal, configuration); sessionManager.Startup(); // start the subscription manager. SubscriptionManager subscriptionManager = new SubscriptionManager(m_serverInternal, configuration); subscriptionManager.Startup(); // add the session manager to the datastore. m_serverInternal.SetSessionManager(sessionManager, subscriptionManager); ServerError = null; // set the server status as running. SetServerState(ServerState.Running); // monitor the configuration file. if (!String.IsNullOrEmpty(configuration.SourceFilePath)) { var m_configurationWatcher = new ConfigurationWatcher(configuration); m_configurationWatcher.Changed += new EventHandler <ConfigurationWatcherEventArgs>(this.OnConfigurationChanged); } CertificateValidator.CertificateUpdate += OnCertificateUpdate; //60s后开始清理过期服务列表,此后每60s检查一次 m_timer = new Timer(ClearNoliveServer, null, 60000, 60000); Console.WriteLine("Discovery服务已启动完成,请勿退出程序!!!"); } catch (Exception e) { Utils.Trace(e, "Unexpected error starting application"); m_serverInternal = null; ServiceResult error = ServiceResult.Create(e, StatusCodes.BadInternalError, "Unexpected error starting application"); ServerError = error; throw new ServiceResultException(error); } } }