private static void StartServer() { //try //{ // Server.ClientServicePort = int.Parse(Configuration.GetSection("ClientServicePort").Value); // Server.ConfigServicePort = int.Parse(Configuration.GetSection("ConfigServicePort").Value); //} //catch (Exception ex) //{ // Logger.Debug("配置文件读取失败:" + ex.ToString()); // return; //} NSPServerConfig serverConfig = null; //初始化配置 if (!File.Exists(CONFIG_FILE_PATH)) { serverConfig = new NSPServerConfig(); serverConfig.SaveChanges(CONFIG_FILE_PATH); } else { serverConfig = ConfigHelper.ReadAllConfig <NSPServerConfig>(CONFIG_FILE_PATH); } foreach (var(user, userbound) in serverConfig.BoundConfig.UserPortBounds) { if (userbound.Bound != null & userbound.Bound.Count > 0) { NetworkUtil.AddUsedPorts(userbound.Bound); } } Server srv = new Server(new Log4netLogger()); int retryCount = 0; while (true) { var watch = new Stopwatch(); try { watch.Start(); srv//.SetWebPort(int.Parse(Configuration.GetSection("WebAPIPort").Value)) .SetConfiguration(serverConfig) .SetAnonymousLogin(true) .SetServerConfigPath(CONFIG_FILE_PATH) .Start() .Wait(); } catch (Exception ex) { Logger.Debug(ex.ToString()); } finally { watch.Stop(); } //短时间多次出错则终止服务器 if (watch.Elapsed > TimeSpan.FromSeconds(10)) { retryCount = 0; } else { retryCount++; } if (retryCount > 10) { break; } } Logger.Debug("NSmart server terminated. Press any key to continue."); try { //只是为了服务器挂了不那么快退出进程而已 Console.Read(); } catch { // ignored } }