开发者

WCF data contracts, how do they fit into the picture

开发者 https://www.devze.com 2022-12-08 06:15 出处:网络
I coded in the data contract from code samples and my service is running, but I didn\'t explicitly set the data contract.

I coded in the data contract from code samples and my service is running, but I didn't explicitly set the data contract.

At which 开发者_JAVA百科bound is the data contract bound to the service contract?


Any complex type (i.e. more than an int or string) that is being used by any of the service operations (methods marked with [OperationContract]) needs to be a [DataContract].

The service operations / methods are being translated into SOAP actions in your WSDL (Web Service Description Language) file. Any data being passed around must be something that can be expressed in an XML schema (XSD file). For int, string etc., there are basic pre-defined XSD types, but anything beyond that - a Customer type or whatever it is you're dealing with, must be defined in a way that can be transformed into an XSD schema.

Remember: after all WCF services are designed to be interoperable with other systems like Java, PHP and whatever else you might dream up. So everything in the service contracts - all the operations and all the data being operated on - must be in a format that can be expressed in a WSDL (operations) and XSD (data) file which are widely accepted industry standards.

For now, WCF in .NET 3.5 is very explicit - you have to define classes and mark them with the [DataContract] attribute, and any fields or properties in those classes that you want to expose to the WSDL/XSD files (and thus to your service definition) must be explicitly marked with a [DataMember] attribute.

Does that make things a bit clearer?

MArc


The DataContractSerializer is what's used by default to serialize operation parameters and return values. For example, if you have this on the server:

[OperationContract]
SomeType DoSomething(SomeOtherType x);

... then the DataContractSerializer will be used to deserialize SomeOtherType (convert it from XML), and then to serialize SomeType (convert it to XML)

The DataContractSerializer understands many different types, but the recommended way to create a type that the DataContractSerializer understands is to mark it with the [DataContract] attribute.

More info here: http://msdn.microsoft.com/en-us/library/ms731923.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号