Unknown error using Digg API and URI handler (silverlight)

百般思念 提交于 2020-01-06 14:11:06

问题


For class we have to follow a tutorial to create a silverlight website that searches DIGG for a given topic. (Using this tutorial as a base: http://weblogs.asp.net/scottgu/archive/2010/02/22/first-look-at-silverlight-2.aspx)

We have to use the following code to fetch the information from DIGG.

    private void buttonSearch_Click(object sender, RoutedEventArgs e)
        {
            string topic = textboxSearchTopic.Text;

            WebClient digg = new WebClient();
            digg.DownloadStringCompleted +=
                              new DownloadStringCompletedEventHandler(digg_DownloadStringCompleted);
            digg.DownloadStringAsync(
                         new Uri("http://services.digg.com/1.0/story.getAll?count=10&topic="+topic)); 
}

void digg_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
            if (e.Error != null)
            {
                DisplayStories(e.Result);             
            }
}

private void DisplayStories(string xmlContent)
        {
            XDocument document = XDocument.Parse(xmlContent);

            var stories = from story in document.Descendants("story")
                          where story.Element("thumbnail")!=null
                          select new DiggStory
                         {
                             Id = (string)story.Attribute("id"),
                             Title = (string)story.Element("title"),
                             Description = (string)story.Element("description"),
                             ThumbNail = (string)story.Element("thumbnail").Attribute("src"),
                             HrefLink = (string)story.Attribute("link"),
                             NumDiggs = (int)story.Attribute("diggs")
                         };
         gridStories.ItemsSource = stories;
        }

And when bushing the buttonSearch, We get the error:

An exception occurred during the operation, making the result invalid.  Check InnerException for exception details.

   at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
   at System.Net.OpenReadCompletedEventArgs.get_Result()
   at DiggSample.Views.Search.Digg_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OpenReadOperationCompleted(Object arg)

I already know the Digg API is outdated, but I do not think this error has anything to do with it. (We even get a local XML file, which we can use but it still doesn't work)

I have no idea what is causing this and we aren't get much help from our teacher, so I hope somebody can help us.

Thanks, Thomas


回答1:


For this section of code:

if (e.Error != null)
{
    DisplayStories(e.Result);             
}

You're saying to display the stories if e.Error is not null. I think you want to switch the condition to say e.Error == null, as that would mean there was no error and its safe to use the result. You may want to put a breakpoint at the condition to inspect the value of e.Error to see if you have an exception there.

Edit:

When you changed the condition to e.Error == null and nothing happened, that's because the error was non-null, so your DisplayStories(e.Result) statement never fired.

The exception in question, the SecurityException, happens because Silverlight in-browser applications do not allow you to make calls to external websites unless that website has a Silverlight cross-domain policy file. Unfortunately, Digg's policy file no longer allows cross-domain access, which means that you won't be able to make this call unless you run your app with full trust out-of-browser. See Network Security Access Restriction in Silverlight for more details.

To run your app as an out-of-browser app with full trust, in visual studio, right-click your project and choose properties. On the "Silverlight" tab, check that box that says "enable running out of browser." Then click the button that says "Out of browser settings." In the dialog, check the box that says "require elevated trust when running outside the browser." In the "Debug" tab, for "Start Action," choose "Out of browser application" and select your project from the dropdown.

When you run this way, you should no longer get the SecurityException.



来源:https://stackoverflow.com/questions/7817619/unknown-error-using-digg-api-and-uri-handler-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!