C# 服务应用程序中的OnStart不起作用

C# 服务应用程序中的OnStart不起作用,c#,windows-services,C#,Windows Services,我正在构建一个服务,该服务将编辑来自我的db数据。我想在服务启动/或recive reuqest时运行Encrypt for connectionString,但无法捕获OnStart 我错了什么 我的班级 public class Service1 : IService1 { protected void OnStart(string[] args) { string stop=""; //can't get here

我正在构建一个服务,该服务将编辑来自我的db数据。我想在服务启动/或recive reuqest时运行Encrypt for connectionString,但无法捕获OnStart

我错了什么

我的班级

  public class Service1 : IService1
    {

        protected void OnStart(string[] args)
        {
            string stop="";
 //can't get here
 //here Encrypt the section.
 // section.SectionInformation.ProtectSection("DataProtectiond");
        }

        public string GetData(int value)
        {
            //my functions
        }
}

调试服务时,很难捕获OnStart方法。无法从Visual Studio运行服务,并且在生产环境中,在附加调试器之前已调用OnStart方法。您可以将以下代码放入OnStart方法中:

System.Diagnostics.Debugger.Launch()
这将弹出一个窗口,您可以在其中选择调试器。如果已打开Visual Studio解决方案,则可以选择此选项,调试器将附加到您的服务

要从Visual Studio运行您的服务,您可以在主应用程序中放置以下内容:

static void Main()
    {
        if (!Environment.UserInteractive)
        {
            // Startup as service. 
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new MyServices() 
            };
            ServiceBase.Run(ServicesToRun);
        }
        else
        {
           //Start things locally here, 
           //for example the same things you do in the OnStart() method of your service
        }
    }

现在,您只需从Visual Studio运行该程序,但当您将其安装到生产服务器上时,它也将作为一项服务运行…

这是一项windows服务吗?是的,这项服务将安装在服务器Pc上并发送数据。那么,为什么要使用asp.net和wcf标记??