MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

Embed Size (px)

Citation preview

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    1/50

    Windows CommunicationFoundation Top to Bottom

    (Part 02 of 15): Contracts

    Michele Leroux Bustamante

    Chief ArchitectIDesign Inc.

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    2/50

    About Michele LerouxBustamante

    IDesign Inc., www.idesign.net

    Microsoft regional director

    MVP Connected Systems

    BEA technical directorIASA - board member

    Published MSDN, CoDe, TheServerSide

    International speaker, International .NET

    Association (INETA)Program advisor, UCSD Extension

    Track chair, SD Expo

    www.dasblonde.net, www.thatindigogirl.com

    http://www.idesign.net/http://www.dasblonde.net/http://www.thatindigogirl.com/http://www.thatindigogirl.com/http://www.dasblonde.net/http://www.idesign.net/
  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    3/50

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    4/50

    Session Prerequisites

    Experience building Microsoft .NETFramework applications

    Basic understanding of WCF clients and

    services

    Level 100

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    5/50

    Agenda

    Contractsand metadata

    Service contracts

    Complex type serializationSerializable types, data contracts, known types,IXmlSerializable

    Message contracts

    Summary of approaches

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    6/50

    Host Process

    ServiceHost

    Endpoint

    EndpointService

    Clients and Services

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    7/50

    Client Process Host Process

    Client App ServiceHost

    Endpoint

    EndpointService

    Proxy

    Clients and Services

    Endpoint

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    8/50

    Client Process Host Process

    Client App ServiceHost

    Endpoint

    EndpointService

    Proxy

    Clients and Services

    Endpoint

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    9/50

    Demonstration One

    Contracts and metadata

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    10/50

    WCF Contracts

    Service contracts

    Data contracts and other serializable types

    Message contracts

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    11/50

    Agenda

    Contracts and metadata

    Service contracts

    Complex type serializationSerializable types, data contracts, known types,IXmlSerializable

    Message contracts

    Summary of approaches

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    12/50

    Service Contracts

    ServiceContractAttribute

    Define service operations

    OperationContractAttribute

    Methods exposed as part of the public servicecontract

    [ServiceContract]

    publicinterfaceIContentManagerService{[OperationContract]void SaveLink(LinkItem item);

    }

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    13/50

    ServiceContractAttribute

    Applied to interfaces or classes

    Prefer interface

    Remove coupling to service implementation

    Services may implement >1 contract

    Always provide meaningful Namespace

    Can provide explicit Name

    [ServiceContract(Name="ContentManagerContract",Namespace=

    "http://www.thatindigogirl.com /samples/2007/07")]publicinterfaceIContentManagerService{}

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    14/50

    OperationContractAttribute

    All methods in a service contract should haveOperationContractAttribute

    Can provide explicit Name, Action, ReplyAction

    [ServiceContract(Name="ContentManagerContract", Namespace ="http://www.thatindigogirl.com/samples/2007/07")]publicinterfaceIContentManagerService{[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);

    }

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    15/50

    MessageParameterAttribute

    UseMessageParameterAttribute tocontrol parameter or return naming

    [ServiceContract(Name="ContentManagerContract,

    Namespace="http://www.thatindigogirl.com/samples/2007/07")]publicinterfaceIContentManagerService{[OperationContract]void SaveLink([MessageParameter(Name="LinkItem")]

    LinkItem item);

    [OperationContract][return:MessageParameter(Name="LinkItem")]

    LinkItem GetLink(string id);}

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    16/50

    Agenda

    Contracts and metadata

    Service contracts

    Complex type serialization

    Serializable types, data contracts, known types,IXmlSerializable

    Message contracts

    Summary of approaches

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    17/50

    Complex Type Serialization

    Serializable types

    Data contracts

    Known types

    IXmlSerializable

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    18/50

    DataContractSerializer

    WSDL

    XSD

    XML DataContractSerializer

    DataContractAttribute

    IXmlSerializable

    SerializableAttribute

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    19/50

    SerializableAttribute

    SerializableAttribute, Serializable

    Many CLR types are serializable

    All fields are serialized

    Regardless of accessibility

    No control over naming conventions or data types[Serializable]publicclassLinkItem{

    privatelong m_id;privatestring m_title;privatestring m_description;privateDateTime m_dateStart;privateDateTime m_dateEnd;privatestring m_url;

    // property accessors

    }

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    20/50

    Data Contracts

    DataContractAttribute

    Translation between CLR types and schema

    DataMemberAttribute

    Opt-in members for serialization

    Applied to fields or properties

    Preferred way for DataContractSerializer towork with complex types

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    21/50

    Demonstration Two

    Data contracts

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    22/50

    DataContractAttribute

    Always provide a Namespace

    Web service conventions use schemas prefix toservice target namespace

    Can provide explicit Name

    [DataContract(Name="LinkItem,Namespace="http://schemas.thatindigogirl.com/samples/2007/07")]publicclassLinkItem{}

    [ServiceContract(Name="ContentManagerContract,Namespace="http://www.thatindigogirl.com/samples/2007/07")]publicinterfaceIContentManagerService{}

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    23/50

    DataMemberAttribute

    Applied to fields or properties

    Always provide Order

    Default order is alphabetical

    Can provide explicit Name, IsRequired

    [DataMember(Name = "Id", IsRequired = false, Order = 0)]publiclong Id{get { return m_id; }set { m_id = value; }

    }

    [DataMember(Name = "Id", IsRequired = false, Order = 0)]privatelong m_id;

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    24/50

    Data Contract SchemaAlphabetical Ordering (Default)

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    25/50

    [DataContract(Name="LinkItem",Namespace="http://schemas.thatindigogirl.com/samples/2007/07")]publicclassLinkItem{// fields

    [DataMember(Name="DateStart", IsRequired = true, Order = 3)]publicDateTime DateStart{get { return m_dateStart; }set { m_dateStart = value; }

    }

    [DataMember(Name="Description", IsRequired = true, Order = 2)]publicstring Description{}

    [DataMember(Name="Id", IsRequired = false, Order = 0)]publiclong Id

    {

    }

    [DataMember(Name="Title", IsRequired = true, Order = 1)]publicstring Title{}

    // other properties}

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    26/50

    Data Contract SchemaExplicit Ordering, Required Elements

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    27/50

    Known Types

    Known types enable polymorphic behavior inservice 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

    Use declarative attributes or configurationsettings

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    28/50

    KnownTypeAttribute

    [DataContract(Namespace ="http://schemas.thatindigogirl.com/samples/2007/07" )][KnownType(typeof(GigInfo))][KnownType(typeof(PhotoLink))][KnownType(typeof(MP3Link))]publicclassLinkItem

    [DataContract(Namespace ="http://schemas.thatindigogirl.com/samples/2007/07" )]publicclassGigInfo: LinkItem

    [DataContract(Namespace ="http://schemas.thatindigogirl.com/samples/2007/07" )]publicclassPhotoLink: LinkItem

    [DataContract(Namespace ="http://schemas.thatindigogirl.com/samples/2007/07" )]publicclassMP3Link: LinkItem

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    29/50

    Demonstration Three

    Known types

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    30/50

    ServiceKnownTypeAttribute

    [ServiceContract(Name = "GigManagerContract", Namespace ="http://www.thatindigogirl.com/samples/2007/07")][ServiceKnownType(typeof(GigInfo))][ServiceKnownType(typeof(PhotoLink))][ServiceKnownType(typeof(MP3Link))]publicinterfaceIGigManagerService

    {[OperationContract]void SaveLink(LinkItem item);

    [OperationContract]LinkItem GetLink(string id);

    }

    S

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    31/50

    ServiceKnownTypeAttribute

    [ServiceContract(Name = "GigManagerContract", Namespace ="http://www.thatindigogirl.com/samples/2007/07")]publicinterfaceIGigManagerService{

    [OperationContract][ServiceKnownType(typeof(GigInfo))]

    void SaveGig(LinkItem item);

    [OperationContract][ServiceKnownType(typeof(GigInfo))]LinkItem GetGig(string id);

    }

    d l dT

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    32/50

    C d Fi t C t t Fi t

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    33/50

    Code-First vs. Contract-First

    In a perfect WCF world:

    Create data contracts that also function asbusiness objects

    Expose in service contracts

    In reality:

    May not own objects (cant make serializable)

    Business object serialization may not beappropriate

    May need to support pre-existing schemas

    IX lS i li bl

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    34/50

    IXmlSerializable

    IXmlSerializable types provide XSD schema toWeb Services Description Language (WSDL) andmetadata exchange (MEX)

    Contract-first support

    You handle mapping between XML and businessobjects

    Requires knowledge of XML

    Provide appropriate validationLess overhead than using interim data contracts

    IX lS i li bl

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    35/50

    IXmlSerializable

    IXmlSerializable

    Valid in service contract (like data contract)

    Hook serialization process:

    IXmlSerializable.ReadXml()

    IXmlSerializable.WriteXml()

    XmlSchemaProviderAttribute

    Improvement onIXmlSerializable.GetSchema()

    Return schema for WSDL and MEX

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    36/50

    Demonstration Four

    IXmlSerializable

    A d

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    37/50

    Agenda

    Contracts and metadata

    Service contracts

    Complex type serialization

    Serializable types, data contracts, known types,IXmlSerializable

    Message contracts

    Summary of approaches

    M C t t

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    38/50

    Message Contracts

    MessageContractAttribute

    Greater control over message headers and bodyelements

    Supporting attributes:MessageHeaderAttribute

    MessageBodyMemberAttribute

    Useful for:

    Add custom headers

    Control message wrapping

    Control signing and encryption

    M C t tAtt ib t

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    39/50

    MessageContractAttribute

    Converts a type to a SOAP message

    Type can contain header and body elements

    Can set IsWrapped, ProtectionLevel

    May set explicit Name, Namespace

    [MessageContract(IsWrapped=true,ProtectionLevel=ProtectionLevel.Sign)]

    publicclassSaveLinkRequest

    {}

    [MessageContract]

    publicclassSaveLinkResponse{}

    M H d Att ib t

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    40/50

    MessageHeaderAttribute

    Apply to fields or properties of messagecontract

    Simple technique for creating custom headers

    Can provide Name, Namespace,ProtectionLevel

    May set SOAP protocol settings: Relay,

    Actor, MustUnderstand

    M B d M b Att ib t

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    41/50

    MessageBodyMemberAttribute

    Apply to fields or properties of messagecontract

    Can have several body elements

    Equivalent to multiple parameters in operation

    Only way to return multiple complex types

    Always supply Order

    Can set Name, Namespace, ProtectionLevel

    [MessageContract(IsWrapped=true,

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    42/50

    [MessageContract(IsWrapped true,ProtectionLevel=ProtectionLevel.Sign)]publicclassSaveEventRequest{privatestring m_licenseKey;privateLinkItem m_linkItem;

    [MessageHeader(ProtectionLevel =ProtectionLevel.EncryptAndSign)]publicstring LicenseKey{get { return m_licenseKey; }

    set { m_licenseKey = value; }}

    [MessageBodyMember]publicLinkItem LinkItem{get { return m_linkItem; }

    set { m_linkItem = value; }}

    }

    [MessageContract]publicclassSaveEventResponse

    {}

    A l i M C t t

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    43/50

    Applying Message Contracts

    Message contracts are supplied as operationparameter or return value

    Must contain serializable members

    Data contracts or serializable types

    [ServiceContract(Name = "ContentManagerContract",Namespace="http://www.thatindigogirl.com/samples/2007/07")]publicinterfaceIContentManagerService{

    [OperationContract]SaveLinkResponse SaveLink(SaveLinkRequestmessage);

    [OperationContract]GetLinkResponse GetLink(GetLinkRequest message);

    }

    Appl ing Message Contracts

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    44/50

    Applying Message Contracts

    In the generated client proxy, custommessage headers are added as parametersto operations

    Not optionalLinkItem item = newLinkItem();

    // initialize item

    string licenseKey = "XXX";m_proxy.SaveLink(licenseKey, item);

    Applying Message Contracts

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    45/50

    Applying Message Contracts

    At the service, message headers areprocessed as member of the messagecontract

    Message body elements also availablethrough message contract

    publicSaveLinkResponse SaveEvent(SaveLinkRequest message)

    { if (message.LicenseKey != "XXX") thrownewFaultException("Invalid license key.");

    m_linkItem = message.LinkItem;returnnewSaveLinkResponse();

    }

    Agenda

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    46/50

    Agenda

    Contracts and metadata

    Service contracts

    Complex type serialization

    Serializable types, data contracts, known types,IXmlSerializable

    Message contracts

    Summary of approaches

    WCF Contracts Summary

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    47/50

    WCF Contracts Summary

    Service contracts define available operationsand signatures

    Data contracts and other serializable types

    can be included in service contractKnown types enable polymorphic contracts

    Message contracts are useful for custom

    headers and other advanced requirements

    For More Information

  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    48/50

    For More Information

    My blog: www.dasblonde.net

    IDesign resources: www.idesign.net

    Learning WCF: A Hands-on Guideby Michele Bustamante (O'ReillyMedia, Inc., 2007)

    Book blog: www.thatindigogirl.com

    Questions and Answers

    http://www.dasblonde.net/http://www.idesign.net/http://www.thatindigogirl.com/http://www.thatindigogirl.com/http://www.idesign.net/http://www.dasblonde.net/
  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    49/50

    Questions and Answers

    Submit text questions using the Ask button.

    Dont forget to fill out the survey.

    For upcoming and previously live webcasts:

    www.microsoft.com/webcastsGot webcast content ideas? Contact us at:http://go.microsoft.com/fwlink/?LinkId=41781

    Today's webcast was presented using Microsoft

    Office Live Meeting. Get a free 14-day trial byvisiting: www.microsoft.com/presentlive

    http://www.microsoft.com/webcastshttp://go.microsoft.com/fwlink/?LinkId=41781http://www.microsoft.com/presentlivehttp://www.microsoft.com/presentlivehttp://go.microsoft.com/fwlink/?LinkId=41781http://www.microsoft.com/webcasts
  • 7/27/2019 MSDN Webcast- Windows Communication Foundation Top to Bottom (Part 02 of 15)- Contracts (Level 200)

    50/50