0

我踩到了一些很难重现的非常奇怪的错误,我真的无法重现它。我们在大约 300 多台 PC 上安装了窗口服务。该服务使用的配置文件(xml)有时会变得清晰(其中一些)。完全清除没有 xml 标签,什么都没有,0kb。我不知道什么会导致这样的问题。我们的日志中没有记录异常。即使在此配置变得清晰之后,它仍在运行,但它没有与我们的 Web 服务通信。这是用于 xml 序列化和反序列化的类。我找不到可能导致这种行为的原因。当然问题可能不在这个特定的类中。有什么建议么??也许一些提示可以导致文件变得清晰。当对此文件的任何操作是通过使用此类时。

对不起,我的英语不好。

[XmlRootAttribute("xmlconfig", Namespace = "DSD_Config", IsNullable = false)]
public class xmlconfig
{
    [XmlElementAttribute(IsNullable = false)]
    public string ProgramApteczny { get; set; }
    public string Server { get; set; }
    public string Database { get; set; }
    public string User { get; set; }
    public string Password { get; set; }
    public string DSDUser { get; set; }
    public string DSDPassword { get; set; }
    public string DSDServerAdres { get; set; }

    //public static string cofFile = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\DSD_Agent\\config.xml";
    public static string cofFile = Application.StartupPath + "\\config.xml";

    public xmlconfig()
    {
    }

    public xmlconfig(string sProgramApteczny, string sServer, string sDatabase, string sUser, string sPassword, string sDSDUser, string sDSDPassword, string sDSDServerAdres)
    {
        ProgramApteczny = sProgramApteczny;
        Server = sServer;
        Database = sDatabase;
        User = sUser;
        Password = sPassword;
        DSDUser = sDSDUser;
        DSDPassword = sDSDPassword;
        DSDServerAdres = sDSDServerAdres;
    }

    public static void readXMLConfig(out xmlconfig configFile)
    {
        XmlSerializer oSerializer = new XmlSerializer(typeof(xmlconfig));
        configFile = new xmlconfig(); 

        try
        {
            using (FileStream fs = new FileStream(cofFile, FileMode.Open, FileAccess.Read))
            {
                 configFile = (xmlconfig)oSerializer.Deserialize(fs);

                 try
                 {
                     configFile.Password = Encryption.DecryptString(configFile.Password);
                 }
                 catch (Exception)
                 {
                     configFile.Password = configFile.Password;
                 }

                 try
                 {
                     configFile.DSDPassword = Encryption.DecryptString(configFile.DSDPassword);
                 }
                 catch (Exception)
                 {
                     configFile.DSDPassword = configFile.DSDPassword;
                 }
            }
        }
        catch 
        {
            configFile = null;
        }
    }

    public static void writeXMLConfig(string sProgramApteczny, string sServer, string sDatabase, string sUser, string sPassword, string sDSDUser, string sDSDPassword)
    {
        xmlconfig oxmlconfig = new xmlconfig();
        readXMLConfig(out oxmlconfig);

        sPassword = Encryption.EncryptString(sPassword);
        sDSDPassword = Encryption.EncryptString(sDSDPassword);

        if (oxmlconfig == null)
        {
            oxmlconfig = new xmlconfig(sProgramApteczny, sServer, sDatabase, sUser, sPassword, sDSDUser, sDSDPassword, "");
        }
        else
        {
            oxmlconfig.ProgramApteczny = sProgramApteczny;
            oxmlconfig.Server = sServer;
            oxmlconfig.Database = sDatabase;
            oxmlconfig.User = sUser;
            oxmlconfig.Password = sPassword;
            oxmlconfig.DSDUser = sDSDUser;
            oxmlconfig.DSDPassword = sDSDPassword;
        }

        XmlSerializer oSerializer = new XmlSerializer(typeof(xmlconfig));
        TextWriter oStreamWriter = null;
        try
        {
            oStreamWriter = new StreamWriter(cofFile, false);
            oSerializer.Serialize(oStreamWriter, oxmlconfig);
        }
        catch (Exception oException)
        {
            WriteToLog(DateTime.Now, "Aplikacja wygenerowała następujący wyjątek: " + oException.Message);
            // Console.WriteLine("Aplikacja wygenerowała następujący wyjątek: " + oException.Message);
        }
        finally
        {
            if (null != oStreamWriter)
            {
                oStreamWriter.Close();
            }
        }
    }

    public static void writeXMLDSDConfig(string sDSDServerAdres)
    {
        xmlconfig oxmlconfig = new xmlconfig();
        readXMLConfig(out oxmlconfig);

        if (oxmlconfig == null)
        {
            throw new Exception("Aplikacja wygenerowała następujący wyjątek: Musisz zdefiniować wszystkie parametry");
        }
        else
        {
            oxmlconfig.DSDPassword = Encryption.EncryptString(oxmlconfig.DSDPassword);
            oxmlconfig.Password = Encryption.EncryptString(oxmlconfig.Password);
            oxmlconfig.DSDServerAdres = sDSDServerAdres;
        }

        XmlSerializer oSerializer = new XmlSerializer(typeof(xmlconfig));
        TextWriter oStreamWriter = null;
        try
        {
            oStreamWriter = new StreamWriter(cofFile, false);
            oSerializer.Serialize(oStreamWriter, oxmlconfig);
        }
        catch (Exception oException)
        {
            throw new Exception("Aplikacja wygenerowała następujący wyjątek: " + oException.Message);
        }
        finally
        {
            if (null != oStreamWriter)
            {
                oStreamWriter.Close();
            }
        }
    }
}
4

1 回答 1

0

我猜,xml文件被多台计算机/Windows服务访问;如果是这样?

读取和写入文件时看起来没有使用锁定机制。一种可能的解决方法是使用单个实例类,它只允许一个线程一次读取/写入文件。

bool isInProgress=false

Public void Read()    
{    
    if(isInProgress==false)    
    {    
        isInProgress=true;  

        try    
        {    
            //Do reading or writing
        }    
        finally
        {
            isInProgress=false;
        }
    }    
}
于 2013-05-24T12:15:33.327 回答