可一个WCF OperationContract的方法的WebGet属性有多个ResponseFormat类型? [英] Can a WCF OperationContract method's WebGet attribute have multiple ResponseFormat types?

查看:340
本文介绍了可一个WCF OperationContract的方法的WebGet属性有多个ResponseFormat类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ServiceContract描述一个WCF服务中使用的方法。该方法具有其限定UriTemplate和ResponseFormat一个WebGet属性。

I have a ServiceContract describing a method used in a WCF service. The method has a WebGet attribute which defines a UriTemplate and ResponseFormat.

我想重新使用一个单一的方法,并有多个WebGet不同UriTemplates,不同ResponseFormats属性。基本上我希望能够避免多种方法只是为了区分像返回类型为XML与JSON。在所有我到目前为止看到的例子中,我需要创建一个不同的方法,对每个WebGet虽然属性。下面是一个示例OperationContract的

I want to reuse a single method and have multiple WebGet attributes with different UriTemplates and different ResponseFormats. Basically I'm hoping to avoid having multiple methods just to differentiate things like return type being XML vs. JSON. In all of the examples I've seen so far I am required to create a different method for each WebGet attribute though. Here's a sample OperationContract

[ServiceContract]
public interface ICatalogService
{
    [OperationContract]
    [WebGet(UriTemplate = "product/{id}/details?format=xml", ResponseFormat = WebMessageFormat.Xml)]
    Product GetProduct(string id);

    [OperationContract]
    [WebGet(UriTemplate = "product/{id}/details?format=json", ResponseFormat = WebMessageFormat.Json)]
    Product GetJsonProduct(string id);
}

用上面的例子,我想用GetProduct方法对于这两个像这样的XML和JSON返回类型:

Using the example above I'd like to use the GetProduct method for both the xml and json return types like this:

[ServiceContract]
public interface ICatalogService
{
    [OperationContract]
    [WebGet(UriTemplate = "product/{id}/details?format=xml", ResponseFormat = WebMessageFormat.Xml)]
    [WebGet(UriTemplate = "product/{id}/details?format=json", ResponseFormat = WebMessageFormat.Json)]
    Product GetProduct(string id);
}

有没有办法来实现这一目标,所以我没有粘编写不同的方法只是返回不同的ResponseFormats?

Is there a way to achieve this so I'm not stuck writing different methods just to return different ResponseFormats?

谢谢!

推荐答案

您可以做到这一点。

[ServiceContract]
public interface ICatalogService
{
    [OperationContract]
    [WebGet(UriTemplate = "product/{id}/details?format={format}")]
    Stream GetProduct(string id, string format);
}

,然后在code手柄系列化基于关闭该参数指定的值。

And then in your code handle serialization based off the value specified on the parameter.

有关XML编写处理您的序列化一个辅助方法。

For XML write a helper method that handles your serialization.

public static Stream GetServiceStream(string format, string callback, DataTable dt, SyndicationFeed sf)
        {
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
            if (format == "xml")
            {
                XmlSerializer xmls = new XmlSerializer(typeof(DataTable));
                xmls.Serialize(writer, dt);
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            }
            else if (format == "json")
            {
                var toJSON = new JavaScriptSerializer();
                toJSON.RegisterConverters(new JavaScriptConverter[] { new JavaScriptDataTableConverter() });
                writer.Write(toJSON.Serialize(dt));
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/json";
            }
            else if (format == "jsonp")
            {
                var toJSON = new JavaScriptSerializer();
                toJSON.RegisterConverters(new JavaScriptConverter[] { new JavaScriptDataTableConverter() });
                writer.Write(callback + "( " + toJSON.Serialize(dt) + " );");
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/json";
            }
            else if (format == "rss")
            {
                XmlWriter xmlw = new XmlTextWriter(writer);
                sf.SaveAsRss20(xmlw);
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            }
            else if (format == "atom")
            {
                XmlWriter xmlw = new XmlTextWriter(writer);
                sf.SaveAsAtom10(xmlw);
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            }
            else
            {
                writer.Write("Invalid formatting specified.");
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            }

            writer.Flush();
            stream.Position = 0;
            return stream;
        }
}

这篇关于可一个WCF OperationContract的方法的WebGet属性有多个ResponseFormat类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆