使用wcf公开SyndicationFeedFormatter或IList <syndicationitem> </syndicationitem>

时间:2009-05-09 03:01:50

标签: .net wcf silverlight

我想用wcf和basicHttpBinding公开一个SyndicationFeedFormatter。我继续得到如下所示的错误。我已经包含了interface / class和web.config wcf配置。

我试图公开SyndicationFeedFormatter以及IList,但我无法通过以下错误。有没有人能够做到这一点或有人确认问题是什么?

thx - dave

错误消息

System.ServiceModel.Dispatcher.NetDispatcherFaultException:格式化程序在尝试反序列化消息时引发异常:尝试反序列化参数http://tempuri.org/:GetFeaturesResult时出错。 InnerException消息是'第1行第123位错误。元素'http://tempuri.org/:GetFeaturesResult'包含'http://schemas.datacontract.org/2004/07/System.ServiceModel.Syndication:Rss20FeedFormatter'数据合同的数据

我的界面/合约看起来像

[ServiceContract]
[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
public interface IGetData {

    [OperationContract]
    SyndicationFeedFormatter GetFeatures();

    [OperationContract]
    IList<SyndicationItem> GetFeatures2();

}

我的方法看起来像......

    public SyndicationFeedFormatter GetFeatures()() {
        // Generate some items...
        SyndicationFeed feed = new SyndicationFeed() {
            Title = new TextSyndicationContent("Mike's Feed"),
            Description = new TextSyndicationContent("Mike's Feed Description")
        };

        feed.Items = from i in new int[] { 1, 2, 3, 4, 5 }
                     select new SyndicationItem() {
                         Title = new TextSyndicationContent(string.Format("Feed item {0}", i)),
                         Summary = new TextSyndicationContent("Not much to see here"),
                         PublishDate = DateTime.Now,
                         LastUpdatedTime = DateTime.Now,
                         Copyright = new TextSyndicationContent("MikeT!"),
                     };

        return (new Rss20FeedFormatter(feed));
    }

    public IList<SyndicationItem> GetFeatures2() {

        List<string> includeList = new List<string>();
        includeList.Add("Feature");

        IList<SyndicationItem> mylist = ReaderManager.GetFeedByCategory2(includeList, null, null);
        return mylist;

    }

我的web.config如下所示

                                 binding =“webHttpBinding”contract =“SLNavigationApp.Web.IGetData”&gt;                                                                         - &GT;                                                                                      

         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

     </service>
 </services>

 <behaviors>
     <endpointBehaviors>
         <behavior name="webHttpBehavior">
             <webHttp />
         </behavior>
     </endpointBehaviors>
     <serviceBehaviors>
         <behavior name="SLNavigationApp.Web.GetDataBehavior">
             <serviceMetadata httpGetEnabled="true" />
             <serviceDebug includeExceptionDetailInFaults="true" />
         </behavior>
    </serviceBehaviors>
 </behaviors>

2 个答案:

答案 0 :(得分:1)

我很困惑:您使用的是WebHttpBinding还是BasicHttpBinding?你肯定应该使用前者,这应该可行。

如果你正在尝试使用BasicHttpBinding,你介意分享原因吗? SyndicationFeedFormatter类不是DataContracts或XmlSerializable(这是你需要支持BasicHttpBinding的东西),所以除非你做了一些额外的工作,否则它可能不适用于那种情况。我可能试图解决这个问题的方法是简单地改变我的ServiceContract以返回Message对象,如下所示:

[ServiceContract]
[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
public interface IGetData {
    [OperationContract]
    Message GetFeatures();

}

...

SyndicationFeedFormatter frm = new Rss20FeedFormatter(feed);
return Message.CreateMessage(
   MessageVersion.None, "GetFeatures", 
   new FeedBodyWriter(frm)
   );

...

class FeedBodyWriter : BodyWriter {
   SyndicationFeedFormatter formatter;
   public FeedBodyWriter(SyndicationFeedFormatter formatter) : base(false) {
      this.formatter = formatter;
   }
   protected override void OnWriteBodyContents(XmlDictionaryWriter writer) {
      formatter.WriteTo(writer);
   }
}

答案 1 :(得分:0)

很酷,我想知道为什么我的服务没有生成浏览器可读的内容。我也使用basicHttpBinding(因为所有其余的调用都来自Silverlight)。

作为无关的旁注:

您可以更改: 来自i in new int [] {1,2,3,4,5}

要: 来自我在Enumerable.Range(1,5)

这可以非常方便!当我从服务器中提取分页XML数据时,我通常在XLINQ查询中将其用作“Enumerable.Range(1,pagecount)”。