使用 .net mvc 将 xml 写入文件时出现问题 - 超时?

发布于 2024-08-26 06:30:26 字数 1215 浏览 5 评论 0原文

嘿,写入 xml 文件时遇到问题。 对于通过浏览器的单个请求来说效果很好,但是当我使用 Charles 之类的东西同时执行 5-10 个重复请求时,其中几个请求将会失败。 跟踪只是显示 500 错误,里面没有内容,基本上我认为他们开始超时等待写入访问或其他东西...

这个方法位于我的存储库类中,也尝试将存储库实例作为单例,但没有似乎有什么不同..

任何帮助将不胜感激。干杯

    public void Add(Request request) {
        try {
            XDocument requests;
            XmlReader xmlReader;
            using (xmlReader = XmlReader.Create(_requestsFilePath)) {
                requests = XDocument.Load(xmlReader);
                XElement xmlRequest = new XElement("request",
                            new XElement("code", request.code),
                            new XElement("date", request.date),
                            new XElement("email", new XCData(request.email)),
                            new XElement("name", new XCData(request.name)),
                            new XElement("recieveOffers", request.recieveOffers)
                        );
                requests.Root.Element("requests").Add(xmlRequest);
                xmlReader.Close();
            }
            requests.Save(_requestsFilePath);
        } catch (Exception ex) {
            HttpContext.Current.Trace.Warn("Error writing to file: "+ex);
        }
    }

Hey, so having an issue with writing out to an xml file.
Works fine for single requests via the browser, but when I use something like Charles to perform 5-10 repeated requests concurrently several of them will fail.
The trace simply shows a 500 error with no content inside, basically I think they start timing out waiting for write access or something...

This method is inside my repository class, have also attempted to have repository instance as a singleton but doesn't appear to make any difference..

Any help would be much appreciated. Cheers

    public void Add(Request request) {
        try {
            XDocument requests;
            XmlReader xmlReader;
            using (xmlReader = XmlReader.Create(_requestsFilePath)) {
                requests = XDocument.Load(xmlReader);
                XElement xmlRequest = new XElement("request",
                            new XElement("code", request.code),
                            new XElement("date", request.date),
                            new XElement("email", new XCData(request.email)),
                            new XElement("name", new XCData(request.name)),
                            new XElement("recieveOffers", request.recieveOffers)
                        );
                requests.Root.Element("requests").Add(xmlRequest);
                xmlReader.Close();
            }
            requests.Save(_requestsFilePath);
        } catch (Exception ex) {
            HttpContext.Current.Trace.Warn("Error writing to file: "+ex);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的呼吸 2024-09-02 06:30:26

您的设计存在一些问题。首先,在并发操作的上下文中,可以同时从同一源读取数据,但是当发生写入时,您需要停止所有其他读取和写入的发生,否则会遇到竞争条件。

您应该更改的第一件事是摆脱 catch big Exception ,因为这会阻止您看到完整的错误。该问题可能是内存不足错误,因为您在加载相同数据集 5-10 次时耗尽了 RAM。除非您知道错误是什么,否则您将无法正确解决它,并且猜测也无济于事。

如果堆栈溢出停止对我超时,我将复制并粘贴我的答案!

There are a couple problems with your design here. First off, in contexts with concurrent operations, it is okay to read from the same source at the same time, but when a write occurs, you need to stop all other reading and writing from happening, otherwise you run into race conditions.

The first thing you should change is getting rid of the catch big Exception because that is preventing you from seeing the full error. The problem could be an out of memory error because you capped out your RAM while loading the same data set 5-10 times. Unless you know what the error is you wont be able to address it correctly, and guessing wont help.

and if stack overflow stops timing out on me, I will copy and paste my answer here!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文