- Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
- Steven Cheng
- 495字
- 2021-04-13 17:04:01
In the contract-first development approach, one of the most important steps is to generate the data types used in the service from XML Schemas, which represent the contract. As a unified distributed communication development platform, it is quite common to support such kind of DataContract
generation in WCF development.
If you are not yet familiar with the contract-first development approach, you can get a quick overview of it from Aaron Skonnard's MSDN article Contract-First Service Development at http://msdn.microsoft.com/en-us/magazine/cc163800.aspx.
- Compose the XML schema that represents the
DataContract
types that will be used in our WCF service. The next screenshot shows a simple sample schema that contains a simple enum and a complex data type definition: - Use WCF ServiceModel Metadata Utility Tool (Svcutil.exe) to generate
DataContract
type source code based on the XML Schema composed in step 1. Following is the sample command on how to use the Svcutil.exe tool:svcutil.exe /target:code /dataContractOnly /serializer:DataContractSerializer /importXmlTypesTestDataContractSchema.xsd
The generated
DataContract
is as follows:public enum LevelEnum : int { [System.Runtime.Serialization.EnumMemberAttribute()] Low = 2, ……………. } ………….. [System.Runtime.Serialization.DataContractAttribute(Name="TestData", Namespace="http://wcftest.org/datacontract")] public partial class TestData : object, System.Runtime.Serialization.IExtensibleDataObject { …………………. [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, Order=2)] public wcftest.org.datacontract.LevelEnum EnumProperty { } }
- Use the generated
DataContract
in our WCF service as operation parameters or return type.
The contract-first development approach is contract/schema driven; the developers need to author the metadata/contract of the service/data. For the previous example, TestDataContractSchema.xsd
provides the contract definition of two types that will be used in our WCF service.
Svcutil.exe is a very powerful tool provided in the .NET 3.5 SDK. If you're familiar with ASP .NET ASMX Web Service development, you will find it similar to the wsdl.exe tool. You can generate a WCF client proxy and export metadata from service code. Here we just use it to generate serialization code from the given XML Schema. In the previous sample, we specify DataContractSerializer
as the serialization type (you can also use XMLSerializer
instead, if you prefer XML serialization-oriented code).
By capturing the service operation's underlying SOAP message on wire (refer to the next screenshot), we can find that the return value's XML payload conform to the XML Schema we provided as the generation source (TestDataContractSchema.xsd
):

The DataContract
we generated here includes two typical class types—a composite type and a simple enum type. In most scenarios, people will define much more complicated data types in their services, and WCF DataContractSerializer
does provide enough support for mapping between an XML Schema-based contract and .NET code-based types. You can get more information on type mapping in the MSDN document Data Contract Schema Reference, available at:
- Creating a typed service client in Chapter 4
- Complete source code for this recipe can be found in the
\Chapter 1\recipe3\
folder
- ANSYS 14熱力學·電磁學·耦合場分析自學手冊
- After Effects CC影視后期制作實戰從入門到精通
- Scratch 1.4: Beginner’s Guide
- EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g
- Illustrator CS6核心應用案例教程(全彩慕課版)
- Zenoss Core Network and System Monitoring
- 中文版Maya 2022完全自學教程
- Android從入門到精通
- AutoCAD 2022中文版建筑設計從入門到精通
- 正則表達式必知必會(修訂版)
- 3dsMax 2018動畫制作基礎教程(第4版)
- 音樂制作7天速成:Cubase編曲教程
- ICEfaces 1.8: Next Generation Enterprise Web Development
- CorelDRAW X6平面設計與制作案例教程
- 好用,Excel數據處理高手