ServiceStack RSS序列化问题

时间:2015-11-18 17:20:28

标签: rss servicestack

我正在尝试为ServiceStack服务创建RSS源。我尽可能地遵循各种例子。我的问题是我没有输出,我不知道如何解决问题。我怀疑我在序列化上做错了什么。这是(我的简化版)

我的DTO是

using System.Collections.Generic;

using ServiceStack;
using Library;
[Route("/MyCollection/Tomorrow/{ID}", "GET, POST")]
[Api("MyCollections Delivery")]
public class MyCollectionTomorrow
    : IReturn<MyCollectionTomorrowResponse>
{
    public long ID { get; set; }
}

public class MyCollectionTomorrowResponse : IHasResponseStatus
{
    public long ID { get; set; }

    public List<MyCollection> Result { get; set; }

    public ResponseStatus ResponseStatus { get; set; }
}

public class MyCollection
{

    public string Description { get; set; }

    public string MyCollectionDayOfWeek { get; set; }

    public DateTime MyCollectionDate { get; set; }

    public bool Assisted { get; set; }

    public string RoundType { get; set; }

    public string Description { get; set; }
}

我的服务是

using System;

using Library;

using ServiceStack;
using ServiceStack.Configuration;

using System;

using Library;

using ServiceStack;
using ServiceStack.Configuration;

using MyCollection.Tomorrow;
using MyCollections.Tomorrow;

public class MyCollectionTomorrowService : Service
{
    public object Any(WasteCollectionTomorrow request)
    {

        int id;

        var param = new CollectionTomorrow();
            param.ID = ID;

            var response = client.Get<CollectionTomorrowResponse>(param);
            return response;
        }
        catch (Exception ex)
        {

            var response = new CollectionTomorrowResponse();
            response.Result = null
            var status = new ResponseStatus { Message = ex.Message, StackTrace = ex.StackTrace };
            response.ResponseStatus = status;
            return response;
        }

    }

}

我的媒体类型是

namespace DataFeedServices
{
using System;
using System.IO;
using System.ServiceModel.Syndication;
using System.Text;
using System.Xml;

using ServiceStack;
using ServiceStack.Data;
using ServiceStack.Web;

using MyCollections.Tomorrow;

public class RssFormat
{
    private const string RssContentType = "application/rss+xml";

    public static void Register(IAppHost appHost)
    {
        appHost.ContentTypes.Register(RssContentType, SerializeToStream, DeserializeFromStream);
    }

    public static void SerializeToStream(IRequest req, object response, Stream stream)
    {
        StreamWriter sw = null;

        try
        {
            var syndicationFeedResponse = response as MyCollectionResponse;

            sw = new StreamWriter(stream);
            if (response != null)
            {
                    WriteRssCollectionFeed(sw, syndicationFeedResponse);
            }

        }
        finally
        {
            if (sw != null)
            {
                sw.Dispose();
            }
        }
    }

     public static void WriteRssCollectionFeed(StreamWriter sw, MyCollectionResponse Mycollections)
    {
        const string Baseuri = "example.com";

        try
        {
            var uri = new Uri(Baseuri);

            var syndicationFeed = new SyndicationFeed(
                "MyCollection Service",
                "Mycollections " ,
                uri);

            syndicationFeed.Authors.Add(new SyndicationPerson("email@mysite.com"));

            if (Mycollections.Result != null)
            {

                foreach (var cats in Mycollections.Result)
                {
                    syndicationFeed.Categories.Add(new SyndicationCategory(cats.RoundID));
                }
            }

            syndicationFeed.Generator = "MyApp";

            syndicationFeed.Copyright = new TextSyndicationContent("Copyright 2015");

            syndicationFeed.LastUpdatedTime = DateTime.Now;

            if (Mycollections.Result != null)
            {
                // set items
                foreach (var coll in Mycollections.Result)
                {
                    var item = new SyndicationItem { Title = new TextSyndicationContent(coll.CollectionDate) };

                    item.Links.Add(new SyndicationLink(uri));
                    item.Authors.Add(new SyndicationPerson("email@mysite.com"));

                    var itemContent = new StringBuilder();
                    itemContent.Append("My Item content");

                    item.Content = new TextSyndicationContent(
                        itemContent.ToString(),
                        TextSyndicationContentKind.Plaintext);
                }
            }
            Rss20FeedFormatter rssFeed = syndicationFeed.GetRss20Formatter();

            var xwriter = XmlWriter.Create(sw);

            rssFeed.WriteTo(xwriter);
        }
        catch (Exception)
        {
            throw new Exception("Something bad happened");
        }
    }

    public static object DeserializeFromStream(Type type, Stream stream)
    {
        throw new NotImplementedException();
    }

  }
}

1 个答案:

答案 0 :(得分:1)

由于您的ContentType不可重用并且与特定MyCollectionResponse耦合,因此使用RSS XML返回原始字符串更容易:

[AddHeader(ContentType = "application/rss+xml")]
public object Any(WasteCollectionTomorrow request)
{
    //..
    return rssXml;
}

您也可以使用以下内容将其直接写入响应输出流:

public object Any(WasteCollectionTomorrow request)
{
    //..
    base.Response.ContentType = "application/rss+xml";
    RssFormat.SerializeToStream(response, Response.OutputStream);
    base.Response.EndRequest();
    return null;
}