Can a WCF OperationContract method's WebGet attribute have multiple ResponseFormat types?

前端 未结 2 1176
醉酒成梦
醉酒成梦 2021-02-01 21:50

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

I want to reuse a

2条回答
  •  悲&欢浪女
    2021-02-01 22:13

    You can do this

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

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

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

提交回复
热议问题