简体   繁体   English

WCF REST for XML选项

[英]WCF REST for XML options

I am making a WCF REST service, in which I return back some XML. 我正在制作WCF REST服务,在其中返回一些XML。

I have an interface with the method 我有方法的接口

 [OperationContract]
 [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml, UriTemplate = "?listcameras", ResponseFormat = WebMessageFormat.Xml)]
 List<TItem> ListItems();

where TITem 在哪里

 [DataContract(Name = "SomeContract", Namespace = "")]
 public class TITem
 {
     [DataMember]
     public string Member1
     {
         get;
         set;
     }

     [DataMember]
     public string Member2
     {
         get;
         set;
     }
 }

When calling this method I then get returned back in XML from a HTTPWebRequest, a list of type TITem's in XML. 当调用此方法时,我随后从HTTPWebRequest返回XML格式的HTTPWebRequest,这是XML中TITem类型的列表。 Notice above I have to make Namespace = "" else I can't seem to use the XDocument. 注意,上面我必须使Namespace =“”,否则我似乎无法使用XDocument。 Descendants method and get a matching name. 后代方法并获得匹配的名称。

I just want some opinions if this is the best way to get back XML from some WCF Service. 如果这是从某些WCF服务取回XML的最佳方法,我只想提出一些意见。 I do want my WCF Service to be extensible and possibly return more than just XML in the future 我确实希望WCF服务是可扩展的,并且将来可能返回的不仅仅是XML。

There is no problem with using a namespace that's string.Empty. 使用string.Empty的命名空间没有问题。 The whole point of using a namespace at all is to distinguish it from other data contracts that have the same DataContract name but a different DataContract namespace. 使用名称空间的全部目的是将其与具有相同DataContract名称但具有不同DataContract名称空间的其他数据协定区分开来。 This is particularly important because DataContract names/namespaces are in no way necessarily related to the names/namespaces as derived from their CLR type representation. 这一点特别重要,因为DataContract名称/命名空间与从其CLR类型表示形式派生的名称/命名空间完全没有关系。

In other words, if you're sure you will NOT have other types with the same DataContract name in a different namespace in your application -- and if you believe that you will not one in the future because of versioning considerations -- you can continue to use string.Empty as the data contract namespace. 换句话说,如果您确定在应用程序的不同名称空间中不会有其他具有相同DataContract名称的类型- 并且,如果您出于版本控制的考虑,相信以后不会再使用该类型-您可以继续使用string.Empty作为数据协定名称空间。

Beyond just the operation attribute (WebGet/WebInvoke), you also need to consider what binding and behavior you're using now and that you'll use in the future. 除了操作属性(WebGet / WebInvoke)外,您还需要考虑现在正在使用以及将来将要使用的绑定和行为。 For example, right now, you may be using WebHttpBinding and WebHttpBehavior. 例如,现在,您可能正在使用WebHttpBinding和WebHttpBehavior。 You can control what data formats are supported at the WebHttpBinding level as well -- XML, JSON, Binary, etc. You can also control what TYPE of XML is returned from WebHttpBehavior -- the XML can be "bare" or it can be "wrapped", which has extra nesting. 您还可以控制WebHttpBinding级别支持的数据格式-XML,JSON,Binary等。您还可以控制WebHttpBehavior返回的XML类型-XML可以是“裸露”或“包裹”,其中包含额外的嵌套。 Finally, you can even plug in your own DataContractSerializerOperationBehavior at some point to plug in an entirely new serializer or data format of your own, should you so choose (though if you do that, you will need to implement your own serializer, formatter, behavior, binding, encoder, etc. like it has been done for SOAP and JSON.) 最后,您甚至可以在某个时候插入自己的DataContractSerializerOperationBehavior来插入全新的序列化器或自己的数据格式(如果您愿意的话)(尽管这样做,您将需要实现自己的序列化器,格式器,行为,绑定,编码器等。就像针对SOAP和JSON所做的那样。)

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM