c# - Windows 服务:服务无法启动。服务进程无法连接到服务控制器
问题描述
我搜索了敌人的解决方案,但无法得到它。这是windows服务的代码。
protected override void OnStart(string[] args)
{
Debugger.Launch();
try {
AsynchronousSocketListener.StartListening();
// Log an event to indicate successful start.
EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);
}
catch(Exception ex)
{
// Log the exception.
EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
}
这是AsynchronousSocketListner 类
static string constr = "Integrated Security=SSPI;Persist Security Info=False;Data Source=WIN-OTVR1M4I567;Initial Catalog=CresijCam";
//string test = constr;
// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);
private AsynchronousSocketListener()
{
}
public static void StartListening()
{
// Establish the local endpoint for the socket.
// The DNS name of the computer
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 1200);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(200);
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
string me = e.Message;
}
}
我每次都会收到不同的错误消息:
等待来自 TCPService 服务的事务响应时达到超时(30000 毫秒)。
无法启动服务。服务进程无法连接到服务控制器
我不知道我得到的错误来自哪里。我知道一件事,服务还没有运行。它在这个方法 startListening() 中。我使用 Debugger.launch() 进行了调试。但我没有到达特定的线路。我也认为这与某处的 TCP 有关,但不能确定。
控制台项目的相同代码处于工作状态。我不知道在这里放什么其他代码。但是,如果需要更多详细信息,请告诉我。
解决方案
这个简单的答案是你AsynchronousSocketListener
不是异步或线程或任何类型的。本质上,您的服务Start正在超时,并且永远不会命中
EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);
因为它本质上是永远阻塞
这个错误说明了一切
等待来自 TCPService 服务的事务响应时达到超时(30000 毫秒)。
OnStart
应该只开始工作。这通常意味着产生一个新线程来完成实际工作。总之,预计OnStart
及时完成。
您将需要重构代码以AsynchronousSocketListener
在新线程或任务中运行
推荐阅读
- python - 无法在 Windows 上使用 pip 安装 pytorch
- ember.js - 如何从测试组件触发 Ember 组件中的函数?
- c# - 如何在 Func 委托列表上使用 LINQ where 子句
- android - 如何测试需要授权的屏幕?Espresso + MockWebServer
- c# - 使用空响应执行 PostAsync
- python - 无法使用 virtualenv 在 sublime3 中取消 python 构建
- django - 如何在 django 中使用创建的模型对象中的选择创建下拉菜单?
- google-cloud-platform - 将 Stackdriver 跟踪信息导出到 BigQuery
- excel - Web Scraping:按钮单击和帮助导航路径
- apache-spark - Apache Spark,NameError:未定义名称“flatMap”