使用wcf公开SyndicationFeedFormatter或IList

时间:2023-01-15 15:50:31

I would like to expose a SyndicationFeedFormatter with wcf and basicHttpBinding. I continue to get errors like that shown below. I have included the interface/class and the web.config wcf configuration.

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

I have tried to expose SyndicationFeedFormatter as well as IList but am unable to get past the following error. Has anyone been able to do this or someone confirm what the problem is?

我试图暴露SyndicationFeedFormatter以及IList但我无法通过以下错误。有没有人能够这样做或有人确认问题是什么?

thx - dave

thx - 戴夫

Error message

System.ServiceModel.Dispatcher.NetDispatcherFaultException: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:GetFeaturesResult. The InnerException message was 'Error in line 1 position 123. Element 'http://tempuri.org/:GetFeaturesResult' contains data of the 'http://schemas.datacontract.org/2004/07/System.ServiceModel.Syndication:Rss20FeedFormatter' data contract

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'数据合约

My interface/contract looks like

我的界面/合同看起来像

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

    [OperationContract]
    SyndicationFeedFormatter GetFeatures();

    [OperationContract]
    IList<SyndicationItem> GetFeatures2();

}

My method looks like ....

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

    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;

    }

My web.config looks like the following

我的web.config如下所示

binding="webHttpBinding" contract="SLNavigationApp.Web.IGetData"> -->

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>

2 个解决方案

#1


I'm confused: Are you using WebHttpBinding, or the BasicHttpBinding? You should definitely be using the former, and that should just work.

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

If you're trying to use BasicHttpBinding, do you mind sharing why? The SyndicationFeedFormatter classes aren't DataContracts or XmlSerializable (which is what you'd need to support for BasicHttpBinding), so it would likely not work in that case unless you did a little bit of extra work. What I'd likely try to get around this would be to simply change my ServiceContract to return Message objects instead, like this:

如果您正在尝试使用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);
   }
}

#2


Cool, I was wondering why my service wasn't producing browser-readable content. I was also using basicHttpBinding (because all the rest of the calls are from Silverlight).

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

As an unrelated sidenote:

作为无关的旁注:

You can change: from i in new int[] { 1, 2, 3, 4, 5 }

您可以在新的int [] {1,2,3,4,5}中从i更改:

To: from i in Enumerable.Range(1, 5)

收件人:来自Enumerable.Range(1,5)

This can be very handy! I usually use it as "Enumerable.Range(1, pagecount)" in an XLINQ query when I'm pulling paged XML data from a server.

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

#1


I'm confused: Are you using WebHttpBinding, or the BasicHttpBinding? You should definitely be using the former, and that should just work.

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

If you're trying to use BasicHttpBinding, do you mind sharing why? The SyndicationFeedFormatter classes aren't DataContracts or XmlSerializable (which is what you'd need to support for BasicHttpBinding), so it would likely not work in that case unless you did a little bit of extra work. What I'd likely try to get around this would be to simply change my ServiceContract to return Message objects instead, like this:

如果您正在尝试使用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);
   }
}

#2


Cool, I was wondering why my service wasn't producing browser-readable content. I was also using basicHttpBinding (because all the rest of the calls are from Silverlight).

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

As an unrelated sidenote:

作为无关的旁注:

You can change: from i in new int[] { 1, 2, 3, 4, 5 }

您可以在新的int [] {1,2,3,4,5}中从i更改:

To: from i in Enumerable.Range(1, 5)

收件人:来自Enumerable.Range(1,5)

This can be very handy! I usually use it as "Enumerable.Range(1, pagecount)" in an XLINQ query when I'm pulling paged XML data from a server.

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