我寻找解决方案,但找不到。这是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有关,但没有什么可以确定的。
对于控制台项目,相同的代码处于工作状态。我不知道在这里还能放什么代码。但请让我知道,如果需要进一步的细节。
发布于 2018-09-04 15:14:15
这个简单的答案是你的AsynchronousSocketListener
不是异步的,也不是线程化的,或者任何类似的东西。从本质上讲,您的服务启动超时,并且永远不会命中
EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);
因为它本质上是永远阻塞的
这个错误说明了一切
等待来自TCPService服务的事务响应时,
超时(30000毫秒)。
OnStart
应该只开始这项工作。这通常意味着产生一个新线程来完成实际工作。简而言之,预计OnStart
会迅速完成。
您需要重构代码以在新的线程或任务中运行AsynchronousSocketListener
https://stackoverflow.com/questions/52160622
复制相似问题