I have a Windows Service that does the following when started. When running via a Console application it works fine, but once I put in a Windows Service I get the below exception. Here is what I have tried so far:
- Disabled the firewall, also tried adding explicit exclusions for the exe, port, and protocol
- Checked CAS Policy Config, shows unrestricted rights
- Configured the Service to run as an Administrator Account, Local System, Local Service, and Network Service, each with the same result
- Tried different ports
- Also tried 127.0.0.1 just to see... same issue
This is wrecking my head, so any help would be greatly appreciated:
The Code:
var _listener = new TcpListener(endpoint); //192.168.2.2:20000
_listener.Start();
The resulting Exception:
Service cannot be started. System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAdd开发者_运维技巧ress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()
at Server.RequestHandler.StartServicingRequests(IPEndPoint endpoint)
at Server.Server.StartServer(String[] args)
at Server.Server.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
I recommend you bind to an endpoint with IPAddress.Any (0.0.0.0) and a specific port.
The error message is likely because another application already has that port open or has recently had it open exclusively.
More information on the error code is on MSDN.
精彩评论