Web 服务有时会显着变慢

发布于 2024-08-25 00:09:56 字数 3108 浏览 4 评论 0原文

我的公司在使用 C#/ASP.Net 编写的 Web 服务时遇到了问题。该服务接收 SQL Server 中数据的身份密钥以及生成和保存该数据的 PDF 报告的路径。

在大多数情况下,此 Web 服务会非常快速地将结果返回到调用网页,通常最多在几秒钟内。

然而,它似乎偶尔会出现明显的放缓。当发生这种速度减慢时,调用 Web 服务的 Web 应用程序将生成超时错误。我们已经检查过,PDF 确实已创建并保存到服务器,因此看起来 Web 服务最终完成了执行。处理完成似乎需要大约 1 到 2 分钟。 PDF 是使用 Data Dynamics 的 ActiveReports 生成的。

当出现此问题时,对 Web 服务的配置文件进行一些小的更改(即,在连接字符串行中添加一个空格)似乎会重新启动 Web 服务,并且在之后的一段时间内一切都完全正常。

在同一 Web 服务器上运行的其他 Web 应用程序似乎不会遇到这种类型的行为,只会遇到这种特定的 Web 服务。

我在下面添加了网络服务的代码。这是对第三方库的基本调用。我们无法在测试中重现此问题。

我想知道什么可能导致这个问题?

[WebMethod]
public string Publish(int identity, string transactionType, string directory, string filename)
{
    try
    {
        AdpConnection Conn = new AdpConnection(ConfigurationManager.AppSettings["myDBConnString"]);
        AdpCommand Cmd = new AdpCommand("storedproc_GetData", oConn);
        AdpParameter Param;

        Cmd.CommandType = CommandType.StoredProcedure;

        Param = Cmd.CreateParameter("@Identity", DbType.Int32);
        Param.Value = identity;
        Cmd.Parameters.Add(oParam);

        Conn.Open();
        string aResponse = Cmd.ExecuteScalar().ToString();
        Conn.Close();

        if (transactionType == "typeA")
        {
            //Parse response
            DataSet dsResponse = ParseDataResponse(aResponse);
            //dsResponse.WriteXml(@ConfigurationManager.AppSettings["DocsDir"] + identity.ToString() + ".xml");

            DataDynamics.ActiveReports.ActiveReport3 rpt = new DataDynamics.ActiveReports.ActiveReport3();

            rpt.LoadLayout(@ConfigurationManager.AppSettings["myReportPath"] + "TypeA.rpx");
            rpt.AddNamedItem("ReportPath", @ConfigurationManager.AppSettings["myReportPath"]);
            rpt.AddNamedItem("XMLSTRING", FormatXML(dsResponse.GetXml()));
            DataDynamics.ActiveReports.DataSources.XMLDataSource xmlds = new DataDynamics.ActiveReports.DataSources.XMLDataSource();
            xmlds.FileURL = null;
            xmlds.RecordsetPattern = "//DataPatternA";
            xmlds.LoadXML(FormatXML(dsResponse.GetXml()));

            if (!System.IO.Directory.Exists(@ConfigurationManager.AppSettings["DocsDir"] + directory + @"\"))
            {
                System.IO.Directory.CreateDirectory(@ConfigurationManager.AppSettings["DocsDir"] + directory + @"\");
            }

            string sXML = FormatXML(dsResponse.GetXml());
            StreamWriter sw = new StreamWriter(@ConfigurationManager.AppSettings["DocsDir"] + directory + @"\" + filename + ".xml", false);
            sw.Write(sXML);
            sw.Close();

            rpt.DataSource = xmlds;
            rpt.Run(true);

            DataDynamics.ActiveReports.Export.Pdf.PdfExport xPdf = new DataDynamics.ActiveReports.Export.Pdf.PdfExport();


            xPdf.Export(rpt.Document, @ConfigurationManager.AppSettings["DocsDir"] + directory + @"\" + filename + ".pdf");

        }

    }
    catch(Exception ex)
    {
        return "Error: " + ex.ToString();
    }

    return @ConfigurationManager.AppSettings["DocsDir"] + directory + @"\" + filename + ".pdf";
}

My company is running into a problem with a web service that is written in C#/ASP.Net. The service receives an identity key for data in SQL Server and a path to generate and save a PDF report for this data.

In most cases, this web service returns results to the calling web pages very quickly, usually within a few seconds max.

However, it seems to occasionally hit a significant slowdown. The web application calling the web service will generate a timeout error when this slowdown occurs. We have checked and the PDF does get created and saved to the server, so it looks like the web service eventually finishes executing. It seems to take about 1 to 2 minutes for processing to have completed. The PDF is generated using ActiveReports from Data Dynamics.

Wwhen this problem occurs, making a small change to the web service's config file (ie, adding a blank space to a connection string line) seems to restart the web service and everything is perfectly ok for a period of time afterwards.

Other web applications that are running on the same web server do not seem to experience this type of behavior, only this particular web service.

I have added the code for the web service below. It is basic calls to 3rd party libraries. We are not able to recreate this problem in test.

I am wondering what might be causing this issue?

[WebMethod]
public string Publish(int identity, string transactionType, string directory, string filename)
{
    try
    {
        AdpConnection Conn = new AdpConnection(ConfigurationManager.AppSettings["myDBConnString"]);
        AdpCommand Cmd = new AdpCommand("storedproc_GetData", oConn);
        AdpParameter Param;

        Cmd.CommandType = CommandType.StoredProcedure;

        Param = Cmd.CreateParameter("@Identity", DbType.Int32);
        Param.Value = identity;
        Cmd.Parameters.Add(oParam);

        Conn.Open();
        string aResponse = Cmd.ExecuteScalar().ToString();
        Conn.Close();

        if (transactionType == "typeA")
        {
            //Parse response
            DataSet dsResponse = ParseDataResponse(aResponse);
            //dsResponse.WriteXml(@ConfigurationManager.AppSettings["DocsDir"] + identity.ToString() + ".xml");

            DataDynamics.ActiveReports.ActiveReport3 rpt = new DataDynamics.ActiveReports.ActiveReport3();

            rpt.LoadLayout(@ConfigurationManager.AppSettings["myReportPath"] + "TypeA.rpx");
            rpt.AddNamedItem("ReportPath", @ConfigurationManager.AppSettings["myReportPath"]);
            rpt.AddNamedItem("XMLSTRING", FormatXML(dsResponse.GetXml()));
            DataDynamics.ActiveReports.DataSources.XMLDataSource xmlds = new DataDynamics.ActiveReports.DataSources.XMLDataSource();
            xmlds.FileURL = null;
            xmlds.RecordsetPattern = "//DataPatternA";
            xmlds.LoadXML(FormatXML(dsResponse.GetXml()));

            if (!System.IO.Directory.Exists(@ConfigurationManager.AppSettings["DocsDir"] + directory + @"\"))
            {
                System.IO.Directory.CreateDirectory(@ConfigurationManager.AppSettings["DocsDir"] + directory + @"\");
            }

            string sXML = FormatXML(dsResponse.GetXml());
            StreamWriter sw = new StreamWriter(@ConfigurationManager.AppSettings["DocsDir"] + directory + @"\" + filename + ".xml", false);
            sw.Write(sXML);
            sw.Close();

            rpt.DataSource = xmlds;
            rpt.Run(true);

            DataDynamics.ActiveReports.Export.Pdf.PdfExport xPdf = new DataDynamics.ActiveReports.Export.Pdf.PdfExport();


            xPdf.Export(rpt.Document, @ConfigurationManager.AppSettings["DocsDir"] + directory + @"\" + filename + ".pdf");

        }

    }
    catch(Exception ex)
    {
        return "Error: " + ex.ToString();
    }

    return @ConfigurationManager.AppSettings["DocsDir"] + directory + @"\" + filename + ".pdf";
}

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

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

发布评论

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

评论(2

下壹個目標 2024-09-01 00:09:56

只是一个简短的说明:
您不会处置您的 StreamWriter,并且您的服务中可能还有其他一次性对象。这可能会导致您的应用程序内存泄漏,从而导致 IIS 重新启动您的工作进程。
尽管这可能无法解决您的问题,但处理一次性物品将有助于防止未来出现问题!

Just a short note:
You're not disposing your StreamWriter and maybe there are other disposable objects in your service, too. This could cause a memory leak in your app which could lead IIS to restart your worker process.
Even though this probably won't be the solution to your problem, disposing disposable objects will help to prevent future problems!

沫离伤花 2024-09-01 00:09:56

当发生这种情况时,您将必须调试 IIS 以查看真正的问题所在。

您应该使用 IIS 调试诊断工具可帮助您确定正在发生的情况。

我还会阅读 Tess Ferrandez 的关于调试 IIS 问题的博客

You're going to have to debug IIS when this happens to see where the true problem lies.

You should use the IIS Debug Diagnostics Tool to help you determine what's happening.

I would also read Tess Ferrandez's blog on debugging IIS problems.

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