2010-10-15 61 views
1

我從silverlight得到這個錯誤:silverlight error

名爲'GetRssFeed'的操作不符合所需的簽名。返回類型必須是實體,實體集合或其中一種預定義的可序列化類型。

這是我的DomainService類:

// TODO: Create methods containing your application logic. 
    [EnableClientAccess()] 
    public class RssService : DomainService 
    { 
     [Invoke] 
     public XDocument GetRssFeed(string Url) 
     //public string GetRssFeed(string Url) 
     { 
      XDocument RssFeed = XDocument.Load(Url); 
      return RssFeed.Document;//.ToString(); 
     } 
    } 

這是我MainPage.xaml.cs中類:

public MainPage() 
     { 
      InitializeComponent(); 

      RssContext context = new RssContext(); 
      context.GetRssFeed("http://www.nu.nl/feeds/rss/algemeen.rss", GetRssFeedCompleted, null); 
      //XDocument RssFeed = XDocument.Load("http://www.nu.nl/feeds/rss/algemeen.rss"); 

     } 

     void GetRssFeedCompleted(InvokeOperation<XDocument> obj) 
     { 
      //FeedResult.Text = obj.Value; 

     } 

     private IEnumerable<Channel> getChannelQuery(XDocument xdoc) 
     { 
      //XDocument xd = (XDocument)xdoc; 
      return from channels in xdoc.Descendants("channel") 
         select new Channel 
         { 
          Title = channels.Element("title") != null ? channels.Element("title").Value : "", 
          Link = channels.Element("link") != null ? channels.Element("link").Value : "", 
          Description = channels.Element("description") != null ? channels.Element("description").Value : "", 
          Items = from items in channels.Descendants("item") 
              select new Item 
              { 
               Title = items.Element("title") != null ? items.Element("title").Value : "", 
               Link = items.Element("link") != null ? items.Element("link").Value : "", 
               Description = items.Element("description") != null ? items.Element("description").Value : "", 
               Guid = (items.Element("guid") != null ? items.Element("guid").Value : "") 
              } 
         }; 
      } 
    } 

如果我在我的DomainService類改變返回類型的XDocument,我得到錯誤。但是把它製成一個字符串就好了。我googled這個錯誤味精,有一些自定義返回類型的解決方法。但這不是自定義的。 XDocument對象屬於.NET框架。

我該如何解決這個問題?

回答

1

雖然XDocument在.NET Framework中,但XDocument不是可序列化的。要用於Web服務(或DomainService),它需要可序列化。所以,錯誤是有效的。您需要使用可序列化的類型或返回字符串。在上面提供的示例中,返回一個字符串,因爲RSS文檔非常好。

您可能想看看.NET WCF Syndication Framework(使創建/提供RSS/Atom提要相對簡單)。

+0

我想出瞭如何解析字符串到xdocument。這就是我不首先使用字符串的原因。但現在開始工作了,謝謝! – Yustme 2010-10-18 08:38:01