【问题标题】:Using wcf to expose SyndicationFeedFormatter OR IList<SyndicationItem>使用 wcf 公开 SyndicationFeedFormatter 或 IList<SyndicationItem>
【发布时间】:2009-05-09 03:01:49
【问题描述】:

我想公开一个带有 wcf 和 basicHttpBinding 的 SyndicationFeedFormatter。我继续收到如下所示的错误。我已经包含了接口/类和 web.config wcf 配置。

我曾尝试公开 SyndicationFeedFormatter 以及 IList,但无法克服以下错误。有没有人能够做到这一点或有人确认问题出在哪里?

谢谢 - 戴夫

错误信息

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"> -->

         <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>

【问题讨论】:

    标签: .net wcf silverlight


    【解决方案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);
       }
    }
    

    【讨论】:

    • 我使用的是 basicHttpBinding,因为我打算使用 Silverlight 作为 wcf 服务的客户端,据我所知,SL 只能使用 basicHttpBinding。我不想将联合项目公开为 rss 或 atom(至少目前不是)。但是,我想在 SL(作为集合)中使用 Syndicated Items 的集合,我可以将其用于在 SL 中进行绑定。我不熟悉返回类型 Message 但我会试一试。 ty
    • 大卫:我明白了。 Message 类型是 WCF 中的通用消息表示形式。将其视为原始 xml 无类型消息,并在 System.ServiceModel.Channels 中定义。希望有帮助!
    【解决方案2】:

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

    作为不相关的旁注:

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

    到: 从 i in Enumerable.Range(1, 5)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 2011-08-08
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多