Академический Документы
Профессиональный Документы
Культура Документы
Session Prerequisites
Experience building Microsoft .NET Framework applications Basic understanding of WCF clients and services
Level 100
Agenda
Contracts and metadata Service contracts Complex type serialization
Serializable types, data contracts, known types, IXmlSerializable
Endpoint
Service
Endpoint
Host Process
Proxy
Endpoint
Endpoint
Service
Endpoint
Client Process
Host Process
Proxy
Endpoint
Endpoint
Service
Endpoint
Client Process
Host Process
Demonstration One
WCF Contracts
Service contracts Data contracts and other serializable types Message contracts
Agenda
Contracts and metadata Service contracts Complex type serialization
Serializable types, data contracts, known types, IXmlSerializable
Service Contracts
ServiceContractAttribute
Define service operations
OperationContractAttribute
Methods exposed as part of the public service contract
[ServiceContract] public interface IContentManagerService { [OperationContract] void SaveLink(LinkItem item); }
ServiceContractAttribute
Applied to interfaces or classes Prefer interface
Remove coupling to service implementation Services may implement >1 contract
OperationContractAttribute
All methods in a service contract should have OperationContractAttribute Can provide explicit Name, Action, ReplyAction
[ServiceContract(Name="ContentManagerContract", Namespace = "http://www.thatindigogirl.com/samples/2007/07")] public interface IContentManagerService { [OperationContract(Name="SaveLink", Action= "http://www.thatindigogirl.com/samples/2007/07/ ContentManagerContract/SaveLink", ReplyAction="http://www.thatindigogirl.com/samples/ 2007/07/ContentManagerContract/SaveLink")] void SaveLink(LinkItem item); }
MessageParameterAttribute
Use MessageParameterAttribute to control parameter or return naming
[ServiceContract(Name="ContentManagerContract, Namespace= "http://www.thatindigogirl.com/samples/2007/07")] public interface IContentManagerService { [OperationContract] void SaveLink([MessageParameter(Name="LinkItem")] LinkItem item); [OperationContract] [return: MessageParameter(Name="LinkItem")] LinkItem GetLink(string id); }
Agenda
Contracts and metadata Service contracts Complex type serialization
Serializable types, data contracts, known types, IXmlSerializable
DataContractSerializer
DataContractAttribute
IXmlSerializable
XML
DataContractSerializer
SerializableAttribute
XSD WSDL
SerializableAttribute
SerializableAttribute, Serializable
Data Contracts
DataContractAttribute
Translation between CLR types and schema
DataMemberAttribute
Opt-in members for serialization Applied to fields or properties
Demonstration Two
Data contracts
DataContractAttribute
Always provide a Namespace
Web service conventions use schemas prefix to service target namespace
DataMemberAttribute
Applied to fields or properties Always provide Order
Default order is alphabetical
[DataContract(Name="LinkItem",Namespace= "http://schemas.thatindigogirl.com/samples/2007/07")] public class LinkItem { // fields [DataMember(Name="DateStart", IsRequired = true, Order = 3)] public DateTime DateStart { get { return m_dateStart; } set { m_dateStart = value; } } [DataMember(Name="Description", IsRequired = true, Order = 2)] public string Description {} [DataMember(Name="Id", IsRequired = false, Order = 0)] public long Id {} [DataMember(Name="Title", IsRequired = true, Order = 1)] public string Title {} // other properties }
Known Types
Known types enable polymorphic behavior in service contracts Expose base types in service operations Associate known types
To the base types themselves To particular operations To the service contract as a whole
KnownTypeAttribute
[DataContract(Namespace = "http://schemas.thatindigogirl.com/samples/2007/07")] [KnownType(typeof(GigInfo))] [KnownType(typeof(PhotoLink))] [KnownType(typeof(MP3Link))] public class LinkItem [DataContract(Namespace = "http://schemas.thatindigogirl.com/samples/2007/07")] public class GigInfo: LinkItem [DataContract(Namespace = "http://schemas.thatindigogirl.com/samples/2007/07")] public class PhotoLink: LinkItem [DataContract(Namespace = "http://schemas.thatindigogirl.com/samples/2007/07")] public class MP3Link: LinkItem
Demonstration Three
Known types
ServiceKnownTypeAttribute
[ServiceContract(Name = "GigManagerContract", Namespace = "http://www.thatindigogirl.com/samples/2007/07")] [ServiceKnownType(typeof(GigInfo))] [ServiceKnownType(typeof(PhotoLink))] [ServiceKnownType(typeof(MP3Link))] public interface IGigManagerService { [OperationContract] void SaveLink(LinkItem item); [OperationContract] LinkItem GetLink(string id); }
ServiceKnownTypeAttribute
[ServiceContract(Name = "GigManagerContract", Namespace = "http://www.thatindigogirl.com/samples/2007/07")] public interface IGigManagerService { [OperationContract] [ServiceKnownType(typeof(GigInfo))] void SaveGig(LinkItem item); [OperationContract] [ServiceKnownType(typeof(GigInfo))] LinkItem GetGig(string id); }
<declaredTypes>
<system.runtime.serialization> <dataContractSerializer> <declaredTypes> <add type="ContentTypes.LinkItem, ContentTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"> <knownType type="ContentTypes.GigInfo, ContentTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> <knownType type="ContentTypes.MP3Link, ContentTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> <knownType type="ContentTypes.PhotoLink, ContentTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> </add> </declaredTypes> </dataContractSerializer> </system.runtime.serialization>
In reality:
May not own objects (cant make serializable) Business object serialization may not be appropriate May need to support pre-existing schemas
IXmlSerializable
IXmlSerializable types provide XSD schema to Web Services Description Language (WSDL) and metadata exchange (MEX)
Contract-first support
IXmlSerializable
IXmlSerializable
Valid in service contract (like data contract)
XmlSchemaProviderAttribute
Improvement on IXmlSerializable.GetSchema() Return schema for WSDL and MEX
Demonstration Four
IXmlSerializable
Agenda
Contracts and metadata Service contracts Complex type serialization
Serializable types, data contracts, known types, IXmlSerializable
Message Contracts
MessageContractAttribute
Greater control over message headers and body elements
Supporting attributes:
MessageHeaderAttribute MessageBodyMemberAttribute
Useful for:
Add custom headers Control message wrapping Control signing and encryption
MessageContractAttribute
Converts a type to a SOAP message
Type can contain header and body elements
MessageHeaderAttribute
Apply to fields or properties of message contract
Simple technique for creating custom headers
Can provide Name, Namespace, ProtectionLevel May set SOAP protocol settings: Relay, Actor, MustUnderstand
MessageBodyMemberAttribute
Apply to fields or properties of message contract Can have several body elements
Equivalent to multiple parameters in operation Only way to return multiple complex types
[MessageContract(IsWrapped=true, ProtectionLevel=ProtectionLevel.Sign)] public class SaveEventRequest { private string m_licenseKey; private LinkItem m_linkItem; [MessageHeader(ProtectionLevel = ProtectionLevel.EncryptAndSign)] public string LicenseKey { get { return m_licenseKey; } set { m_licenseKey = value; } } [MessageBodyMember] public LinkItem LinkItem { get { return m_linkItem; } set { m_linkItem = value; } } } [MessageContract] public class SaveEventResponse { }
Agenda
Contracts and metadata Service contracts Complex type serialization
Serializable types, data contracts, known types, IXmlSerializable
Today's webcast was presented using Microsoft Office Live Meeting. Get a free 14-day trial by visiting: www.microsoft.com/presentlive