问题描述
您好,
我为基于Socket的C#服务器创建了一个控制台应用程序。
现在,我想要这个控制台应用程序作为Windows服务运行。我试图为此创建一个Windows服务。但是服务器没有响应来自浏览器的请求。
我读过有关Windows服务的内容,到目前为止我的理解是Windows服务在不同于Windows应用程序的会话中运行[例如,浏览器在我的情况下,因此无法响应它。
要删除此依赖项,我尝试使用&strong; ProcessStartInfo
的属性将控制台交互重定向到内部流类。
我可以看到我的服务在Windows任务管理器中运行,但它没有响应我的浏览器请求。
我正在为此服务提供我的代码片段。
使用System.Diagnostics;使用System.ServiceProcess
;
命名空间TestWindowsService
{
public partial class Service1:ServiceBase
{
public Service1()
{
InitializeComponent() ;
}
protected override void OnStart(string [] args)
{
Library.WriteErrorLog(" Test window service is started');
string path = @" C:\\Users \\NKR8 \\Documents\\ Visis Studio 2015 \\Projects \\ReqtifyExternalServer \\\\\ \\\bin\\Debug\\Server.exe英寸;
ProcessStartInfo info = new ProcessStartInfo(path);
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
info.ErrorDialog = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(info);
}
protected override void OnStop()
{
Library.WriteErrorLog(""测试窗口服务已停止");
}
}
}
我的代码有什么问题?
提前谢谢!
Navnath
Hello,
I have created a console application for a Socket based C# server.
Now, I want this console application to be run as Windows service. I tried to create a Windows Service for that. But Server does not respond to requests from browsers.
I read things about Windows Service and my understanding up to now is that Windows service is running in different session than Windows application [For example, Browser in my case] and hence can not respond to it.
To remove this dependency, I tried to redirect the console interactions to internal stream using properties of ProcessStartInfoclass.
I can see my service running in Windows Task Manager, but it is not responding to my browser requests.
I am putting my Code snippet for the service here.
using System.Diagnostics; using System.ServiceProcess; namespace TestWindowsService { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { Library.WriteErrorLog("Test window service is started"); string path = @"C:\\Users\\NKR8\\Documents\\Visual Studio 2015\\Projects\\ReqtifyExternalServer\\Server\\bin\\Debug\\Server.exe"; ProcessStartInfo info = new ProcessStartInfo(path); info.UseShellExecute = false; info.RedirectStandardError = true; info.RedirectStandardInput = true; info.RedirectStandardOutput = true; info.CreateNoWindow = true; info.ErrorDialog = false; info.WindowStyle = ProcessWindowStyle.Hidden; System.Diagnostics.Process.Start(info); } protected override void OnStop() { Library.WriteErrorLog("Test window service is stopped"); } } }
What is wrong in my code?
Thank you in advance!
Navnath
这篇关于将C#Server作为Windows服务运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!