コード例 #1
0
ファイル: ExecServerApp.cs プロジェクト: c00t/stride
        /// <summary>
        /// Runs ExecServer in server mode (waiting for connection from ExecServer clients)
        /// </summary>
        /// <param name="entryAssemblyPath">Path to the client assembly in case we need to start another instance of same process.</param>
        /// <param name="executablePath">Path of the executable to run from this ExecServer instance</param>
        private int RunServer(string entryAssemblyPath, string executablePath, int serverInstanceIndex)
        {
            var address = GetEndpointAddress(executablePath, serverInstanceIndex);

            // TODO: The setting of disabling caching should be done per EXE (via config file) instead of global settings for ExecServer
            var useAppDomainCaching = Environment.GetEnvironmentVariable(DisableExecServerAppDomainCaching) != "true";

            // Start WCF pipe for communication with process
            var execServerApp = new ExecServerRemote(entryAssemblyPath, executablePath, true, useAppDomainCaching, serverInstanceIndex == 0);
            var host          = new ServiceHost(execServerApp);

            host.AddServiceEndpoint(typeof(IExecServerRemote), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
            {
                MaxReceivedMessageSize = int.MaxValue,
                SendTimeout            = TimeSpan.FromHours(1),
                ReceiveTimeout         = TimeSpan.FromHours(1),
            }, address);

            try
            {
                host.Open();
            }
            catch (AddressAlreadyInUseException)
            {
                // Uncomment the following line to see which process got a ExitCodeServerAlreadyInUse
                // File.WriteAllText(Path.Combine(Environment.CurrentDirectory, $"test_ExecServer{Process.GetCurrentProcess().Id}.log"), $"Exit code: {ExitCodeServerAlreadyInUse}\r\n");

                // Silently exit if the server is already running
                return(ExitCodeServerAlreadyInUse);
            }

            Console.WriteLine("Server [{0}] is running", executablePath);

            // Register for shutdown
            execServerApp.ShuttingDown += (sender, args) => host.Close();

            // Wait for the server to shutdown
            execServerApp.Wait();

            return(0);
        }