public static void close()
        {
            try
            {
                if (_nchost != null)
                {
                    if (_nchost.HostServer != null)
                    {
                        _nchost.HostServer.Dispose();
                    }
                }

                if (_socketServer != null)
                {
                    _socketServer.Stop();
                }
                if (_managementSocketServer != null)
                {
                    _managementSocketServer.Stop();
                }
                if (_nchost != null)
                {
                    _nchost.StopHosting();
                    _nchost = null;
                }
                CacheServer.ReleaseServiceObject();
                AppUtil.LogEvent(_applicationName, "Cache [ " + cacheName + " ] separate process is stopped successfully.", EventLogEntryType.Information, EventCategories.Information, EventID.GeneralInformation);
            }
            catch (Exception ex)
            {
                AppUtil.LogEvent(_applicationName, "Cache [ " + cacheName + " ] Error: " + ex.ToString(), EventLogEntryType.Error, EventCategories.Error, EventID.GeneralError);
            }
        }
Example #2
0
        /// <summary>
        /// Set things in motion so your service can do its work.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            try
            {
                int serverPort = SocketServer.SocketServer.DEFAULT_SOCK_SERVER_PORT;
                int sendBuffer = SocketServer.SocketServer.DEFAULT_SOCK_BUFFER_SIZE;
                int receiveBuffer = SocketServer.SocketServer.DEFAULT_SOCK_BUFFER_SIZE;
                int managementServerPort = SocketServer.SocketServer.DEFAULT_MANAGEMENT_PORT;

                //Numan Hanif: in case of compact serilization plus DS Provider in same dll we search in deploy dir brute forcely
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Common.Util.AssemblyResolveEventHandler.DeployAssemblyHandler);
                try
                {

                    string configPort = ConfigurationSettings.AppSettings["NCacheServer.Port"];

                    if (configPort.Length > 0)
                    {
                        serverPort = Convert.ToInt32(configPort);
                    }

                    string managePort = ConfigurationSettings.AppSettings["NCacheServer.ManagementPort"];

                    if (managePort.Length > 0)
                    {
                        managementServerPort = Convert.ToInt32(managePort);
                    }

                    string configSendBuffer = ConfigurationSettings.AppSettings["NCacheServer.SendBufferSize"];

                    if (configSendBuffer.Length > 0)
                    {
                        sendBuffer = Convert.ToInt32(configSendBuffer);
                    }

                    string configReceiveBuffer = ConfigurationSettings.AppSettings["NCacheServer.ReceiveBufferSize"];

                    if (configReceiveBuffer.Length > 0)
                    {
                        receiveBuffer = Convert.ToInt32(configReceiveBuffer);
                    }
                }
                catch (Exception)
                {
                }

                _nchost = new CacheHost();

                _socketServer = new SocketServer.SocketServer(serverPort, sendBuffer, receiveBuffer);

                try
                {
                    int period = -1;
                    string intervalString = ConfigurationSettings.AppSettings["GCCollectInterval"];
                    if (intervalString.Length > 0)
                    {
                        period = Convert.ToInt32(intervalString);
                    }
                    _socketServer.StartGCTimer(5, period);
                }
                catch (Exception)
                { }

                IPAddress clusterIp = null;
                if (_clusterIp != null && _clusterIp != string.Empty)
                {
                    try
                    {
                        clusterIp = IPAddress.Parse(_clusterIp);
                    }
                    catch (Exception)
                    {
                        throw new Exception("Invalid BindToIP address specified");
                    }
                }

                ValidateMapAddress("NCacheServer.MgmtEndPoint");
                ValidateMapAddress("NCacheServer.ServerEndPoint");

                _nchost.RegisterMonitorServer();
                //muds:
                //start the socket server separately.
                //Start the NCache Management socket server seperately
                _managementSocketServer = new SocketServer.SocketServer(managementServerPort, sendBuffer, receiveBuffer);

                _managementSocketServer.Start(clusterIp, Alachisoft.NCache.Common.Logger.LoggerNames.CacheManagementSocketServer, "NManagement Service", CommandManagerType.NCacheManagement);
                _socketServer.Start(clusterIp, Alachisoft.NCache.Common.Logger.LoggerNames.SocketServerLogs, "NCache Service", CommandManagerType.NCacheClient);

                CacheServer.SocketServerPort = managementServerPort;
                _nchost.CacheServer.SynchronizeClientConfig();
                _nchost.CacheServer.ClusterIP = _clusterIp;
                _nchost.CacheServer.Renderer = _socketServer;
                Alachisoft.NCache.SocketServer.CacheProvider.Provider = _nchost.CacheServer;
            }
            catch (Exception ex)
            {
                AppUtil.LogEvent(_cacheserver, ex.ToString(), EventLogEntryType.Error, EventCategories.Error, EventID.GeneralError);
                throw ex;
            }
        }
        public static void StartCacheHost()
        {
            cacheName = cacheName.ToLower();
            string    _clientServerIp = string.Empty;
            IPAddress clusterIp       = null;
            IPAddress clientServerIp  = null;

            try
            {
                Alachisoft.NCache.Common.Util.AssemblyResolveEventHandler.CacheName = cacheName;
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                AppDomain.CurrentDomain.AssemblyResolve    += new ResolveEventHandler(Alachisoft.NCache.Common.Util.AssemblyResolveEventHandler.DeployAssemblyHandler);
                int sendBuffer    = SocketServer.SocketServer.DEFAULT_SOCK_BUFFER_SIZE;
                int receiveBuffer = SocketServer.SocketServer.DEFAULT_SOCK_BUFFER_SIZE;
                if (ServiceConfiguration.SendBufferSize > 0)
                {
                    sendBuffer = ServiceConfiguration.SendBufferSize;
                }
                if (ServiceConfiguration.ReceiveBufferSize > 0)
                {
                    receiveBuffer = ServiceConfiguration.ReceiveBufferSize;
                }
                _nchost       = new Alachisoft.NCache.Management.CacheHost();
                _socketServer = new SocketServer.SocketServer(ServiceConfiguration.Port, sendBuffer, receiveBuffer);
                _nchost.HostServer.Renderer = _socketServer;
#if !NETCORE && !NETCOREAPP2_0
                try
                {
                    string        serviceEXE1 = Path.Combine(AppUtil.InstallDir, "bin");
                    string        serviceEXE2 = Path.Combine(serviceEXE1, "service");
                    string        serviceEXE3 = Path.Combine(serviceEXE2, "Alachisoft.NCache.Service.exe");
                    Configuration config      = ConfigurationManager.OpenExeConfiguration(serviceEXE3);
                }
                catch (Exception ex) { return; }
#else
                try
                {
                    string        serviceEXE1 = Path.Combine(AppUtil.InstallDir, "bin");
                    string        serviceEXE2 = Path.Combine(serviceEXE1, "service");
                    string        serviceEXE3 = Path.Combine(serviceEXE2, "Alachisoft.NCache.Daemon.dll");
                    Configuration config      = ConfigurationManager.OpenExeConfiguration(serviceEXE3);
                }
                catch (Exception ex) { return; }
#endif

                try
                {
                    clusterIp = ServiceConfiguration.BindToIP;
                }
                catch (Exception)
                {
                    ErrorCode = 5;
                    throw new Exception("Invalid BindToClusterIP address specified.");
                }


                try
                {
                    clientServerIp = ServiceConfiguration.BindToIP;
                }
                catch (Exception)
                {
                    ErrorCode = 6;
                    throw new Exception("Invalid BindToClientServerIP address specified");
                }

                try
                {
                    if (!Helper.isPortFree(managementPort, clusterIp))
                    {
                        ErrorCode = 8;
                        throw new Exception("Management listener can not be started. The port might already be in use");
                    }
                }
                catch (Exception ex)
                {
                    if (ex.ToString().Contains("Specified argument was out of the range of valid values"))
                    {
                        ErrorCode = 10;
                        throw new Exception("The port is outside the range of valid values for Management.");
                    }
                    else
                    {
                        throw;
                    }
                }


                _socketServer.CacheName = cacheName;
                Alachisoft.NCache.SocketServer.CacheProvider.Provider = _nchost.HostServer;
                _managementSocketServer = new SocketServer.SocketServer(managementPort, sendBuffer, receiveBuffer);


                _managementSocketServer.Start(clusterIp, Alachisoft.NCache.Common.Logger.LoggerNames.CacheManagementSocketServer, "NManagement", CommandManagerType.NCacheHostManagement, ConnectionManagerType.Management);
                _socketServer.Start(clientServerIp, Alachisoft.NCache.Common.Logger.LoggerNames.SocketServerLogs, "Cache Host", CommandManagerType.NCacheClient, ConnectionManagerType.HostClient);

                CacheServer.SocketServerPort  = managementPort;
                CacheServer.ConnectionManager = SocketServer.SocketServer.HostClientConnectionManager;
                _nchost.HostServer.SynchronizeClientConfig();
                _nchost.HostServer.ClusterIP = clusterIp.ToString();
                _nchost.HostServer.Renderer.ManagementIPAddress = clusterIp.ToString();
                OnCacheStopped cacheStoppedDelegate = new OnCacheStopped(onCacheStopped);
                _nchost.HostServer.RegisterCacheStopCallback(cacheStoppedDelegate);

                //for debugging purposes, comment this line
                _nchost.HostServer.InitiateCacheHostStopThread();
                AppUtil.LogEvent(_applicationName, "Cache [ " + cacheName + " ] separate process is started successfully.", EventLogEntryType.Information, EventCategories.Information, EventID.GeneralInformation);
            }
            catch (Exception e)
            {
                throwError(e.ToString(), false);
            }

            try
            {
                // Start the watcher to check if service configuration file gets changed.
                // For hot applicable

                FileSystemWatcher configWatcher = new FileSystemWatcher();
                string            path          = Path.Combine(AppUtil.InstallDir, "bin");
                configWatcher.Path                = Path.Combine(path, "service");
                configWatcher.Filter              = "*.config";
                configWatcher.NotifyFilter        = NotifyFilters.LastWrite;
                configWatcher.Changed            += ServiceConfigChanged;
                configWatcher.EnableRaisingEvents = true;
            }
            catch (Exception)
            {
            }
        }
        private static void StartCacheHost()
        {
            string    _clientServerIp = string.Empty;
            IPAddress clusterIp       = null;
            IPAddress clientServerIp  = null;

            try
            {
                AssemblyResolveEventHandler.CacheName       = cacheName;
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                AppDomain.CurrentDomain.AssemblyResolve    += new ResolveEventHandler(Alachisoft.NCache.Common.Util.AssemblyResolveEventHandler.DeployAssemblyHandler);
                int sendBuffer    = SocketServer.SocketServer.DEFAULT_SOCK_BUFFER_SIZE;
                int receiveBuffer = SocketServer.SocketServer.DEFAULT_SOCK_BUFFER_SIZE;


                if (ServiceConfiguration.SendBufferSize > 0)
                {
                    sendBuffer = ServiceConfiguration.SendBufferSize;
                }

                if (ServiceConfiguration.ReceiveBufferSize > 0)
                {
                    receiveBuffer = ServiceConfiguration.ReceiveBufferSize;
                }


                _nchost       = new Management.CacheHost();
                _socketServer = new SocketServer.SocketServer(ServiceConfiguration.Port, sendBuffer, receiveBuffer);
                string        part1      = Path.Combine(AppUtil.InstallDir, "bin");
                string        part2      = Path.Combine(part1, "service");
                string        serviceEXE = Path.Combine(part2, "Alachisoft.NCache.Service.exe");
                Configuration config     = ConfigurationManager.OpenExeConfiguration(serviceEXE);

                try
                {
                    clusterIp = ServiceConfiguration.BindToIP;
                }
                catch (Exception)
                {
                    ErrorCode = 5;
                    throw new Exception("Invalid BindToClusterIP address specified.");
                }


                try
                {
                    clientServerIp = ServiceConfiguration.BindToIP;
                }
                catch (Exception)
                {
                    ErrorCode = 6;
                    throw new Exception("Invalid BindToClientServerIP address specified");
                }

                try
                {
                    if (!Helper.isPortFree(managementPort, clusterIp))
                    {
                        ErrorCode = 8;
                        throw new Exception("Management listener can not be started. The port might already be in use");
                    }
                }
                catch (Exception ex)
                {
                    if (ex.ToString().Contains("Specified argument was out of the range of valid values"))
                    {
                        ErrorCode = 10;
                        throw new Exception("The port is outside the range of valid values for Management.");
                    }
                    else
                    {
                        throw;
                    }
                }

                _socketServer.CacheName = cacheName;
                CacheProvider.Provider  = _nchost.HostServer;
                _managementSocketServer = new SocketServer.SocketServer(managementPort, sendBuffer, receiveBuffer);


                _managementSocketServer.Start(clusterIp, Common.Logger.LoggerNames.CacheManagementSocketServer, "NManagement", CommandManagerType.NCacheHostManagement, ConnectionManagerType.Management);
                _socketServer.Start(clientServerIp, Common.Logger.LoggerNames.SocketServerLogs, "Cache Host", CommandManagerType.NCacheClient, ConnectionManagerType.HostClient);


                CacheServer.SocketServerPort  = managementPort;
                CacheServer.ConnectionManager = _socketServer.HostClientConnectionManager;

                _nchost.HostServer.SynchronizeClientConfig();
                _nchost.HostServer.ClusterIP = clusterIp.ToString();
                _nchost.HostServer.Renderer  = _socketServer;
                _nchost.HostServer.Renderer.ManagementIPAddress = clusterIp.ToString();
                OnCacheStopped cacheStoppedDelegate = new OnCacheStopped(onCacheStopped);
                _nchost.HostServer.RegisterCacheStopCallback(cacheStoppedDelegate);

                _nchost.HostServer.InitiateCacheHostStopThread();
                AppUtil.LogEvent(_applicationName, "Cache [ " + cacheName + " ] separate process is started successfully.", EventLogEntryType.Information, EventCategories.Information, EventID.GeneralInformation);
            }
            catch (Exception e)
            {
                throwError(e.ToString(), false);
            }
        }