C# 窗口服务onStart()可以';t调用mongoDB distinct查询结果所需的另一种方法

C# 窗口服务onStart()可以';t调用mongoDB distinct查询结果所需的另一种方法,c#,.net,json,mongodb,windows-services,C#,.net,Json,Mongodb,Windows Services,我正在开发一个需要每小时运行一次的函数,我采用了服务方式来编写。但现在我在调试模式帮助下运行服务时遇到了问题。 在我看来,每件事都很好,代码从调试部分开始,计算开始,但当我试图从db中获取不同的元素时,服务代码停止了,这就是问题所在 //main static class Program { /// <summary> /// The main entry point fo

我正在开发一个需要每小时运行一次的函数,我采用了服务方式来编写。但现在我在调试模式帮助下运行服务时遇到了问题。
在我看来,每件事都很好,代码从调试部分开始,计算开始,但当我试图从db中获取不同的元素时,服务代码停止了,这就是问题所在

//main
            static class Program
            {
                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                static void Main()
                {
        #if DEBUG

                    Service1 myService = new Service1();
                    myService.onDebug();

        #else
                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[] 
                    { 
                        new Service1() 
                    };
                    ServiceBase.Run(ServicesToRun);
        #endif
                }
            }


            //*********************************************************

            //Service.cs code is here


            public Service1()
                    {
                        InitializeComponent();
                    }

                    public void onDebug()
                    {
                        OnStart(null); //untill now working fine
                    }

                    protected override void OnStart(string[] args)
                    {
                        try
                        {
        //this is the function i want to run every hour, i have made successfully perfect in a console application but in window service its not working

                            var hourlyTimer = new System.Threading.Timer(e => hourlyCalculateSummaryForAllDevices(), null, TimeSpan.FromMinutes(0), TimeSpan.FromMinutes(0));
                        }
                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                        }
                        System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "ServiceStarted.txt");

                    }

                    protected override void OnStop()
                    {
                        System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "HourlySummaryServiceStopped.txt");
                    }
                    //*********************************************************
                    //hourly Summary calculation functions
                    public static void hourlyCalculateSummaryForAllDevices()
                    {
                        findDistinctAssets(); //ok f11 pressed when cursor come here
                    }

                    static async void findDistinctAssets()
                    {
                        var collection = _database.GetCollection<BsonDocument>(telemetryCollectionName);
                        var filter = new BsonDocument();
                        var cursor = await collection.DistinctAsync<Int64>("AssetName", filter);
                        //while (await cursor.MoveNextAsync()) //here the service code stopped automatically, im stuck why?
                        //{
                            var assets = cursor.Current;
                            foreach (var asset in assets)
                            {
                                CalculateSummaryOfAssets(Convert.ToInt64(asset));
                            }

                            Console.Read();
                        //}
                    }
//main
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
静态void Main()
{
#如果调试
Service1 myService=newservice1();
myService.onDebug();
#否则
ServiceBase[]ServicesToRun;
ServicesToRun=新的ServiceBase[]
{ 
新服务1()
};
ServiceBase.Run(ServicesToRun);
#恩迪夫
}
}
//*********************************************************
//Service.cs代码在这里
公共服务1()
{
初始化组件();
}
公共漏洞()
{
OnStart(null);//直到现在工作正常
}
启动时受保护的覆盖无效(字符串[]args)
{
尝试
{
//这是我想每小时运行的功能,我已经在控制台应用程序中成功地完善了它,但在窗口服务中它不起作用
var hourlyTimer=new System.Threading.Timer(e=>hourlyCalculateSummaryForAllDevices(),null,TimeSpan.frommins(0),TimeSpan.frommins(0));
}
捕获(例外情况除外)
{
字符串msg=例如消息;
}
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory+“ServiceStarted.txt”);
}
受保护的覆盖void OnStop()
{
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory+“HourlySummaryServiceStopped.txt”);
}
//*********************************************************
//每小时汇总计算功能
公共静态空时计算所有设备的摘要()
{
FindDistincAssets();//光标移到此处时按下ok f11
}
静态异步void findDistincAssets()
{
var collection=\u database.GetCollection(telemetryCollectionName);
var filter=新的BsonDocument();
var cursor=await collection.DistinctAsync(“AssetName”,filter);
//while(wait cursor.MoveNextAsync())//这里服务代码自动停止,我卡住了为什么?
//{
var资产=游标。当前;
foreach(资产中的var资产)
{
计算资产汇总(转换为64(资产));
}
Console.Read();
//}
}

windows服务没有GUI。So
Console.Read()
将停止服务并且不会继续。谢谢,但是我已经从那里删除了console.read(),并且在执行静态异步void finddistincassets(){var collection=_database.GetCollection(telemetryCollectionName);var filter=new BsonDocument();var cursor=wait collection.DistinctAsync时它仍然停止运行(“AssetName”,filter);…尽管我已经在控制台应用程序上成功运行了这段代码