Windows服务

时间:2017-04-01 06:19:20

标签: c# asp.net windows-services

我开发了一个Windows服务.exe,其中包含多个服务,如下所示:

 static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        {
            new Service1(),
            new Service2(),
            new Service3(),
            new Service4()
        };
        ServiceBase.Run(ServicesToRun);
    }

我在生产中遇到致命错误(检查事件日志) stackoverflow 异常:

错误应用程序名称:MyApp.exe,版本:1.0.0.0,时间戳:0x522e8317

错误模块名称:clr.dll,版本:4.0.30319.18047,时间戳:0x58dcff99

异常代码:0xc00000fd

故障偏移:0x004226a2

错误进程id:0xeb4c

错误应用程序启动时间:0x01d2a9de37398bdb

错误应用程序路径:C:\ Users \ Administrator \ Desktop \ MyApp.exe

错误模块路径:C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ clr.dll

报告ID:d52424aa-1a16-11e3-bc4b-002590a4ec55

以下是我的一个Windows服务的代码:

private Timer timer1 = null;
private static object _intervalSync = new object(); 

protected override void OnStart(string[] args)
    {
        try
        {
            timer1 = new Timer();
            this.timer1.Interval = Convert.ToDouble(900000); // 15mins
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
            timer1.Enabled = true;
        }
        catch (Exception ex)
        {
            Logger.LogException(ex);
        }
    }

    protected override void OnStop()
    {
        try
        {
            timer1.Enabled = false;
            timer1.Stop();
        }
        catch (Exception ex)
        {
            Logger.LogException(ex);
        }
    }

    private void timer1_Tick(object sender, ElapsedEventArgs e)
    {       
        if (System.Threading.Monitor.TryEnter(_intervalSync))
        {
            try
            {
               // execute code
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
            finally
            {
                System.Threading.Monitor.Exit(_intervalSync);
            } 
        }
    }

请找到递归代码:

public void fetchInventorySupplyFromAmazon(Parameter parameter, DateTime queryStartDateTime)
    {
        ListInventorySupplyResponse listInventorySupplyResponse = new ListInventorySupplyResponse();
        ListInventorySupplyByNextTokenResponse listInventorySupplyByNextTokenResponse = new ListInventorySupplyByNextTokenResponse();
        FBAInventoryDetailServiceMWS fbaInventoryDetailServiceMWS = new FBAInventoryDetailServiceMWS(parameter);
        SetParameter setParameter = new SetParameter();
        try
        {
            if (parameter.nextToken != null && parameter.nextToken != "")
            {

                listInventorySupplyByNextTokenResponse = fbaInventoryDetailServiceMWS.InvokeListInventorySupplyByNextToken(parameter.nextToken);
                foreach (var inventorySupply in listInventorySupplyByNextTokenResponse.ListInventorySupplyByNextTokenResult.InventorySupplyList.member)
                {
                    insertUpdateInventory(inventorySupply, parameter);
                }
                if (listInventorySupplyByNextTokenResponse.IsSetListInventorySupplyByNextTokenResult())
                {
                    setParameter.SetParameterValue(parameter, listInventorySupplyByNextTokenResponse.ListInventorySupplyByNextTokenResult.NextToken);
                //recursive call
                    fetchInventorySupplyFromAmazon(parameter, queryStartDateTime);
                }
            }
            else
            {
                listInventorySupplyResponse = fbaInventoryDetailServiceMWS.InvokeListInventorySupply(queryStartDateTime);
                foreach (var inventorySupply in listInventorySupplyResponse.ListInventorySupplyResult.InventorySupplyList.member)
                {
                    insertUpdateInventory(inventorySupply, parameter);
                }
                if (listInventorySupplyResponse.ListInventorySupplyResult.NextToken != null && listInventorySupplyResponse.ListInventorySupplyResult.NextToken != "")
                {
                    setParameter.SetParameterValue(parameter, listInventorySupplyResponse.ListInventorySupplyResult.NextToken);
                //recursive call
                    fetchInventorySupplyFromAmazon(parameter, queryStartDateTime);
                }
            }

        }
        catch (MarketplaceWebServiceOrdersException ex)
        {
            if ((!string.IsNullOrEmpty(ex.ErrorCode) ? ex.ErrorCode.ToString().Trim().ToUpper().Equals("NEXTTOKENCORRUPTED") : false) ||
                ex.StatusCode.ToString().Trim().ToUpper().Equals("UNAUTHORIZED"))
            {
                setParameter.SetParameterValue(parameter, null);
                //recursive call
                fetchInventorySupplyFromAmazon(parameter, queryStartDateTime);
            }
            else
            {
                throw;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

任何帮助都会很棒!!

1 个答案:

答案 0 :(得分:0)

问题在于我的业务逻辑。 我更改了 fetchInventorySupplyFromAmazon 函数,以包含 do while循环而不是递归调用。

 public void fetchInventorySupplyFromAmazon(Parameter parameter, DateTime queryStartDateTime)
    {
        ListInventorySupplyResponse listInventorySupplyResponse = new ListInventorySupplyResponse();
        ListInventorySupplyByNextTokenResponse listInventorySupplyByNextTokenResponse = new ListInventorySupplyByNextTokenResponse();
        FBAInventoryDetailServiceMWS fbaInventoryDetailServiceMWS = new FBAInventoryDetailServiceMWS(parameter);
        SetParameter setParameter = new SetParameter();

        try
        {
            parameter.nextToken = null;
            do
            {
                if (string.IsNullOrEmpty(parameter.nextToken))
                {
                    listInventorySupplyResponse = fbaInventoryDetailServiceMWS.InvokeListInventorySupply(queryStartDateTime);
                    if (listInventorySupplyResponse.ListInventorySupplyResult != null)
                    {
                        if (listInventorySupplyResponse.ListInventorySupplyResult.InventorySupplyList != null)
                        {
                            foreach (var inventorySupply in listInventorySupplyResponse.ListInventorySupplyResult.InventorySupplyList.member)
                            {
                                insertUpdateInventory(inventorySupply, parameter);
                            }
                        }

                        setParameter.SetParameterValue(parameter, listInventorySupplyResponse.ListInventorySupplyResult.NextToken);
                    }
                }
                else
                {
                    listInventorySupplyByNextTokenResponse = fbaInventoryDetailServiceMWS.InvokeListInventorySupplyByNextToken(parameter.nextToken);
                    if (listInventorySupplyByNextTokenResponse.ListInventorySupplyByNextTokenResult != null)
                    {
                        if (listInventorySupplyByNextTokenResponse.ListInventorySupplyByNextTokenResult.InventorySupplyList != null)
                        {
                            foreach (var inventorySupply in listInventorySupplyByNextTokenResponse.ListInventorySupplyByNextTokenResult.InventorySupplyList.member)
                            {
                                insertUpdateInventory(inventorySupply, parameter);
                            }
                        }
                        setParameter.SetParameterValue(parameter, listInventorySupplyByNextTokenResponse.ListInventorySupplyByNextTokenResult.NextToken);
                    }
                    else
                    {
                        setParameter.SetParameterValue(parameter, null);
                    }
                }
            } while (!string.IsNullOrEmpty(parameter.nextToken));

        }
        catch (Exception)
        {
            throw;
        }
    }