当远程 XML 源不可用时处理 XmlDataSource

发布于 2024-08-28 23:39:10 字数 75 浏览 8 评论 0原文

使用 XmlDataSource 时,是否有好的方法来处理远程 XML 文件不可用时引起的异常?我对 .NET 和使用 C# 有点陌生。

When using an XmlDataSource is there good way to handle exceptions that are caused when the remote XML file is unavailable? I'm somewhat new to .NET and using C#.

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

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

发布评论

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

评论(3

痴意少年 2024-09-04 23:39:10

当出现这样的异常时,真正由您决定什么适合您的应用程序。你唯一不应该做的就是忽略它。

您可以选择的选项包括:

  • 自动重试多次,以防连接问题是暂时的
  • 将适当的错误消息返回给用户,并可能记录或通过电子邮件发送异常
  • 使用以前缓存的 XML 文件版本,直到可以获取新副本
  • 让异常冒泡到调用层并让它处理它(也许首先记录它)

您可能还需要做的一件事是清理Finally 块中的所有资源(例如打开的连接)。

It's really up to you to determine what is suitable for your application when an exception like this is raised. The only thing you shouldn't do is ignore it.

Options you have include:

  • Automatically retry a number of times, in case the connection problem is transitory
  • Return an appropriate error message to the user and perhaps log or email the exception
  • Use a previously cached version of the XML file until a fresh copy can be fetched
  • Let the exception bubble up to the calling layer and let it deal with it (perhaps logging it first)

One thing you may also need to do is clean up any resources (e.g. open connections) in a Finally block.

时光病人 2024-09-04 23:39:10

我尝试了捕获异常来触发不同处理的方法,但由于某种原因它不起作用。我仍然得到一个杀死页面的异常,而不是导致 ErrorMessage 显示和 Repeater1 隐藏的异常:
异常详细信息:System.Net.WebException:远程服务器返回错误:(404) 未找到。

为什么我不能捕获异常然后采取不同的操作?

protected void Page_PreRender(object sender, System.EventArgs e)
{
    try
    {
        RssSource.DataFile = "http://www.example.com/rss/feed/index1.aspx";
        RssSource.XPath = "/rss/channel/item[position()<3]";
        RssSource.EnableCaching = true;
        RssSource.CacheDuration = 43200;
        RssSource.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
    }
    catch (Exception ex)
    {
        ErrorMessage.Visible = true;
        Repeater1.Visible = false;

    }
}

<asp:Label ID="ErrorMessage" runat="server" Text="News not unavailable" Visible="false" /> 
<asp:XmlDataSource ID="RssSource" runat="server" />
<asp:Repeater ID="repeater1" runat="server" DataSourceID="RssSource">
    <ItemTemplate>
        <p><%# XPath("description")%></p>
    </ItemTemplate>
</asp:Repeater>  

I tried the approach of catching the exception to trigger different handling, but it doesn't work for some reason. Instead of the exception causing the ErrorMessage to show and the Repeater1 to be hidden, I still just get an exception that kills the page:
Exception Details: System.Net.WebException: The remote server returned an error: (404) Not Found.

Why can't I catch the exception and then take a different action?

protected void Page_PreRender(object sender, System.EventArgs e)
{
    try
    {
        RssSource.DataFile = "http://www.example.com/rss/feed/index1.aspx";
        RssSource.XPath = "/rss/channel/item[position()<3]";
        RssSource.EnableCaching = true;
        RssSource.CacheDuration = 43200;
        RssSource.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
    }
    catch (Exception ex)
    {
        ErrorMessage.Visible = true;
        Repeater1.Visible = false;

    }
}

<asp:Label ID="ErrorMessage" runat="server" Text="News not unavailable" Visible="false" /> 
<asp:XmlDataSource ID="RssSource" runat="server" />
<asp:Repeater ID="repeater1" runat="server" DataSourceID="RssSource">
    <ItemTemplate>
        <p><%# XPath("description")%></p>
    </ItemTemplate>
</asp:Repeater>  
小巷里的女流氓 2024-09-04 23:39:10

我假设这个问题已经解决,但我会回答这个问题,以防其他人遇到这个问题并偶然发现这篇文章。

您还需要在 try 块中绑定到 XmlDataSource...

            try
            {

                xdsRSS.DataFile = Configuration.BeaconConfigurationSection.Current.SyndicatedJobs.RssUrl;
                xdsRSS.XPath = Configuration.BeaconConfigurationSection.Current.SyndicatedJobs.XPath;
                xdsRSS.EnableCaching = true;
                xdsRSS.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
                xdsRSS.CacheDuration = 6000;
                dlRSS.DataSource = xdsRSS;
                dlRSS.DataBind();
            }
            catch
            {
                dlRSS.Visible = false;
                pnlLinkToJobSite.Visible = true;
            }

...确保从转发器中删除 DataSourceID 属性,然后就可以开始了。

I'm assuming this has been resolved, but am answering it in case someone else has the issue and stumbles upon this post.

You need to also bind to your XmlDataSource within your try block...

            try
            {

                xdsRSS.DataFile = Configuration.BeaconConfigurationSection.Current.SyndicatedJobs.RssUrl;
                xdsRSS.XPath = Configuration.BeaconConfigurationSection.Current.SyndicatedJobs.XPath;
                xdsRSS.EnableCaching = true;
                xdsRSS.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
                xdsRSS.CacheDuration = 6000;
                dlRSS.DataSource = xdsRSS;
                dlRSS.DataBind();
            }
            catch
            {
                dlRSS.Visible = false;
                pnlLinkToJobSite.Visible = true;
            }

...make sure to remove the DataSourceID property from your repeater, and you should be good to go.

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