从sql更新信息时,TopShelf windows服务内存泄漏

时间:2017-04-19 09:53:41

标签: c# windows-services

我有一个准备好的topshelf的Windows服务。这个项目发送请求web服务和响应xml数据。之后这个xml数据序列化列表对象而不是这些对象从sql更新信息。当启动服务大约8 mb之后增加1.8 gb内存超出范围异常。为什么?

 public class HostService
{

    private readonly Timer _updaterThread;
    private readonly object _lockObject;
    private readonly Manager _manager;
    public HostService()
    {
        _manager = new Manager();
        _lockObject = new object();
        var interval = Convert.ToInt32(ConfigurationManager.AppSettings["UpdateInterval"]);
        _updaterThread = new Timer(interval) { AutoReset = true };
        _updaterThread.Elapsed += UpdateInfo;
    }

    public void Start()
    {
        try
        {
            LoadLogger();
            _updaterThread.Start();

        }
        catch (Exception e)
        {
            FXEventLogger.Instance().AddLog(EventLogEntryType.Error, e);
        }
    }

    private void UpdateInfo(object state, EventArgs ev)
    {
        lock (_lockObject)
        {
            _manager.UpdateFmdProductions();
        }
    }



    public void Stop()
    {
        try
        {

            _updaterThread.Stop();
            FXEventLogger.Instance().AddLog(EventLogEntryType.SuccessAudit, "Service stopped");
            FXEventLogger.Finalize();
        }
        catch (Exception e)
        {
            FXEventLogger.Instance().AddLog(EventLogEntryType.Error, e);
        }
    }

1 个答案:

答案 0 :(得分:0)

** UpdateFmdProductions()使两个进程首先** `public class SmdStatusProvider     {

    public List<MakerInfo> GetMakersInfo()
    {
        SMDStatusServiceClient requester = null;
        try
        {
            requester = new SMDStatusServiceClient();

            var response = requester.Request("//MachineStatus[@MachineType='MAKER']/ShiftStatus");

            return ParseMakersInfo(response);
        }
        catch (Exception e)
        {
            FXEventLogger.Instance().AddLog(EventLogEntryType.Error, e);

            return null;
        }
        finally
        {
            requester.Close();
            GC.SuppressFinalize(this);
        }
    }


    private List<MakerInfo> ParseMakersInfo(string shiftStatesXml)
    {
        using (var sr = new StringReader(shiftStatesXml))
        {
            var serializer = new XmlSerializer(typeof(List<MakerInfo>), new XmlRootAttribute("result"));

            var _makersInfo = (List<MakerInfo>) serializer.Deserialize(sr);

            sr.Dispose();

            return _makersInfo;
        }
    }
}`

public class FMDRepository:SqlServerClient     {

    public FMDRepository(string connectionString)
    {
        ConnectionString = connectionString;
    }

    public bool UpdateRealProductions(IEnumerable<MakerInfo> makersInfo)
    {
        if (makersInfo == null) return false;

        OpenConnection();

        foreach (var makerInfo in makersInfo)
        {
            var success = ExecuteNonQuery(
                  String.Format("UPDATE [Shooter] SET [RealProduction]={0}, [LastUpdate]='{1:yyyy-MM-dd HH:mm:ss}' WHERE [Machine]='{2}'",
                      Math.Round(makerInfo.RealProduction, 0), DateTime.Now,
                      makerInfo.Machine));
            if (success == -1)
            {
                CloseConnection();
                return false;
            }
        }
        CloseConnection();
        return true;
    }
}