Windows Communication Foundation 101

3,539 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,539
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
167
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • Windows Communication Foundation 101

    1. 1. Windows Communication Foundation 101 Lars Wilhelmsen Miles Oslo Session January 15th 2009
    2. 2. Agenda <ul><li>Introduction </li></ul><ul><li>The ABC of WCF </li></ul><ul><li>The Service Model </li></ul><ul><li>Message Exchange Patterns </li></ul><ul><li>Hosting </li></ul><ul><li>Client proxies </li></ul><ul><li>Behaviors </li></ul><ul><li>Security </li></ul><ul><li>Interoperability </li></ul>
    3. 3. Introduction <ul><li>Originally dubbed ”Codename Indigo” </li></ul><ul><ul><li>A part of WinFx – later named .NET 3.0 (together with WPF, WF & CardSpace) </li></ul></ul><ul><li>Unified Communication Model </li></ul><ul><ul><li>Not only ”Windows SOA Foundation” or ”Windows Web Services Foundation” </li></ul></ul><ul><ul><li>Superseeds ASMX, WSE, Remoting, Enterprise Services, MSMQ in many ways </li></ul></ul><ul><li>Enhanched features in .NET 3.5 – REST capabilities, stronger WF integration etc. </li></ul><ul><li>Anticipating the release of WCF+WF 4.0 – first public drops released at PDC’08 in LA. </li></ul>
    4. 4. Mom, I’ve got WCF! <ul><li>Played with the Indigo bits since the first public drops back in 2005 (?) </li></ul><ul><li>Applied WCF in different projects </li></ul><ul><ul><li>Gained experience – what works – what doesn’t </li></ul></ul><ul><li>Attended Bustamante & Löwy’s Master classes (2x 5 days) </li></ul><ul><li>Frequent Answerer & moderator in the MSDN/WCF forum. </li></ul><ul><li>Done previous presentation on the topic </li></ul><ul><li>Connected Systems MVP 2009  </li></ul>
    5. 5. The fundamental WCF mantra ABC = E
    6. 6. Address <ul><li>The ”where” </li></ul><ul><li>Transport-specific URL </li></ul><ul><ul><li>http://server/App/Service.svc </li></ul></ul><ul><ul><li>https://server/App/Service.svc </li></ul></ul><ul><ul><li>net.tcp://server/App/Service.svc </li></ul></ul><ul><ul><li>net.msmq://server/private/QueuedService </li></ul></ul><ul><ul><li>net.pipe://localhost/pipe </li></ul></ul>
    7. 7. Binding <ul><li>The ”How” </li></ul><ul><li>System-provided bindings </li></ul><ul><ul><li>BasicHttpBinding – The compatible one </li></ul></ul><ul><ul><li>WSHttpBinding – The best for WCF <-> WCF scenarios (Internet) </li></ul></ul><ul><ul><li>WSDualHttpBinding – HTTP/SOAP version that supports callbacks </li></ul></ul><ul><ul><li>WSFederationHttpBinding – Federated security </li></ul></ul><ul><ul><li>NetTcpBinding – The best for WCF <-> WCF scenarios (Intranet) </li></ul></ul><ul><ul><li>NetNamedPipeBinding – Best for IPC on same computer </li></ul></ul><ul><ul><li>NetMsmqBinding – One-way, async. queueing </li></ul></ul><ul><ul><li>NetPeerTcpBinding – P2P – built on top of the Window PNRP & P2P stack </li></ul></ul><ul><ul><li>WebHttpBinding - REST </li></ul></ul><ul><ul><li>+ Context-enabled bindings – For WF scenarios. </li></ul></ul><ul><li>Custom bindings </li></ul><ul><ul><li>Cook your own – more fine-grained control over existing channels / combinations </li></ul></ul>
    8. 8. Contract <ul><li>The ”What” </li></ul><ul><ul><li>Service contracts: Operations </li></ul></ul><ul><ul><li>Data contracts: The messages </li></ul></ul><ul><ul><li>Message contracts: Fine-tune OOB headers etc. </li></ul></ul><ul><ul><li>Fault Contracts: Control exception/fault flow </li></ul></ul>
    9. 9. Service Contract [ ServiceContract (Namespace = &quot;http://services.miles.no/TestService/2009/01/&quot; )] public interface IMilesService { [ OperationContract ] void InviteToSession( SessionInitiationInfo request); [ OperationContract ] void AddAttendee( CustomMessage request); }
    10. 10. Data Contract [ DataContract ] public class SessionInitiationInfo : IExtensibleDataObject { [ DataMember (Order = 0 , IsRequired = true )] public string Presenter { get ; set ;} [ DataMember (Order = 1 , IsRequired = true )] public string Topic { get ; set ; } [ DataMember (Order = 2 , IsRequired = true )] public DateTime StartTime { get ; set ;} [ DataMember (Order = 3 , IsRequired = true )] public DateTime EndTime { get ; set ;} [ DataMember (Order = 4 , IsRequired = true )] public AttendeeInfo [] Attendees { get ; set ; } #region IExtensibleDataObject Members public ExtensionDataObject ExtensionData { get ; set ; } #endregion }
    11. 11. Data Contract #2 [ DataContract ] public class AttendeeInfo : IExtensibleDataObject { [ DataMember (Order = 0 , IsRequired = true )] public string FirstName { get ; set ; } [ DataMember (Order = 1 , IsRequired = true )] public string LastName { get ; set ; } [ DataMember (Order = 2 , IsRequired = false )] public string EmailAddress { get ; set ; } #region IExtensibleDataObject Members public ExtensionDataObject ExtensionData { get ; set ; } #endregion }
    12. 12. Message Contract [ MessageContract ] public class CustomMessage { [ MessageHeader ] public string CustomHeader { get ; set ; } [ MessageBody ] public AttendeeInfo Attendee { get ; set ; } } <ul><li>MessageContract </li></ul><ul><ul><li>HasProtectionLevel, ProtectionLevel, IsWrapped, WrapperName, WrapperNamespace </li></ul></ul><ul><li>MessageHeader </li></ul>
    13. 13. Fault Contract [ DataContract ] public class NoSuchAttendeeFault { public string Name { get ; set ; } } Create a data contract for contain the fault payload [ OperationContract ] [ FaultContract ( typeof ( NoSuchAttendeeFault ))] AttendeeInfo GetAttendeeById( int id); Decorate the operation with the [FaultContract] Attribute, and specify the payload type.
    14. 14. Service Model Client Server Messaging Layer Behaviour Behaviour Behaviour Behaviour Contract Binding Factory Channel Address Channel Channel Address Listener Binding  Address Binding Channel Endpoint (c) Barry Dorrans - http://idunno.org
    15. 15. MEPs <ul><li>Message Exchange Patterns </li></ul><ul><li>WCF supports three different kinds </li></ul><ul><ul><ul><li>With or without session support </li></ul></ul></ul><ul><ul><li>Request-Response </li></ul></ul><ul><ul><li>OneWay </li></ul></ul><ul><ul><li>Duplex </li></ul></ul>
    16. 16. Hosting <ul><li>ServiceHost </li></ul><ul><li>Hosting models </li></ul><ul><ul><li>IIS / WAS </li></ul></ul><ul><ul><li>NT Service </li></ul></ul><ul><ul><li>Windows Forms, Console, WPF </li></ul></ul>
    17. 17. Hosting #2 public class SelfHost { public static void Main() { var baseUri = new Uri ( &quot;http://localhost:1234/&quot; ); var serviceHost = new ServiceHost ( typeof ( MilesService ), baseUri); var binding = new BasicHttpBinding ( BasicHttpSecurityMode . None); serviceHost . AddServiceEndpoint( typeof ( IMilesService ), binding, string . Empty); serviceHost . Open(); } }
    18. 18. Hosting Outlook: Codename ”Dublin” <ul><li>Microsoft’s Distributed Application Server </li></ul><ul><li>Evolution of IIS/WAS hosting of services </li></ul><ul><li>Will be the preferred way to host Services & Workflows </li></ul><ul><li>Better deployment & management features </li></ul><ul><li>Will be shipped independent of .NET 4.0 </li></ul><ul><li>Will work on Windows Server 2008 </li></ul>
    19. 19. Client Proxy <ul><li>The easy way </li></ul><ul><ul><li>Generate from WSDL/Metadata in Visual Studio </li></ul></ul><ul><ul><ul><li>Add Service Reference... on project context menu. </li></ul></ul></ul><ul><ul><li>Generated client: flawed design. </li></ul></ul><ul><ul><ul><li>Doesn’t support the IDisposable pattern properly. </li></ul></ul></ul><ul><ul><ul><li>Luckily the class is marked as partial – a fix can be implemented ”on the side” </li></ul></ul></ul><ul><ul><ul><li>Generated proxies derives from ClientBase<T> or DuplexClientBase<T> (for duplex proxies) </li></ul></ul></ul><ul><li>Run-time generation </li></ul><ul><ul><li>More flexible, but a bit more overhead </li></ul></ul><ul><ul><ul><li>Often my preferred method </li></ul></ul></ul>
    20. 20. Client Proxy #2 var ep = new EndpointAddress (baseUri); var proxy = ChannelFactory < IMilesService >. CreateChannel(binding, ep); proxy . InviteToSession( new SessionInitiationInfo { Presenter = &quot;Lars Wilhelmsen&quot; , Topic = &quot;WCF 101&quot; // ... }); // Optionally: Explicitly close the proxy channel (( ICommunicationObject )proxy) . Close();
    21. 21. Some lines about behaviors <ul><li>Used to modify the normal operational pattern of a service, endpoint or operation </li></ul><ul><li>Multiple ”levels” </li></ul><ul><ul><li>Service behaviors </li></ul></ul><ul><ul><li>Endpoint behaviors </li></ul></ul><ul><ul><li>Operation behaviors </li></ul></ul><ul><li>Service & Endpoint behaviors can be set from configuration – operation behaviors cannot (must be done programmatically). </li></ul>
    22. 22. Commonly used Service Behaviors <ul><li>ServiceDebugBehavior </li></ul><ul><ul><li>Turn on debug information – optionally send it back to the client </li></ul></ul><ul><li>ServiceMetadataBehavior </li></ul><ul><ul><li>WSDL / Metadata generation </li></ul></ul><ul><li>ServiceAuthorizationBehavior </li></ul><ul><ul><li>Configure service authorization methods and settings </li></ul></ul><ul><li>ServiceThrottlingBehavior </li></ul><ul><ul><li>Throttling – to tune performance and mitigate possible DoS attacks </li></ul></ul>
    23. 23. Commonly used Endpoint Behaviors <ul><li>WebHttpBehavior </li></ul><ul><ul><li>To be used in tandem with the WebHttpBinding </li></ul></ul><ul><ul><ul><li>REST </li></ul></ul></ul><ul><li>EnableWebScriptBehavior </li></ul><ul><ul><li>Easy REST/JSON generation </li></ul></ul><ul><li>DataContractSerializerBehavior </li></ul><ul><ul><li>Tune the underlying data contract serializer </li></ul></ul>
    24. 24. Security <ul><li>Big topic </li></ul><ul><ul><li>If you can – go for the standard solution </li></ul></ul><ul><ul><ul><li>But if you can’t, the extensibility model is great </li></ul></ul></ul><ul><li>Transport & Message Security </li></ul><ul><li>Authentication </li></ul><ul><ul><li>Windows, Kerberos, x509, U/P, Federation, Custom dev. </li></ul></ul><ul><li>Authorization </li></ul><ul><ul><li>Windows Domain Groups, ASP.NET Role Provider, AzMan, Custom dev. </li></ul></ul>
    25. 25. Security Outlook: Codename ”Geneva” <ul><li>The next-generation framework for claims-based security </li></ul><ul><li>Secure Token Service (STS) </li></ul><ul><ul><li>Runs on Windows Server </li></ul></ul><ul><ul><li>Let’s you expose data from AD and other sources as claims </li></ul></ul><ul><li>Client frameworks – web/desktop </li></ul><ul><li>Next-gen version of CardSpace </li></ul><ul><li>Will be compatible / plugs into WCF </li></ul>
    26. 26. Interoperability <ul><li>BasicHttpBinding – backwards compatible with ASMX (Old skool .NET Web Services) </li></ul><ul><li>Adheres to the WS-BasicProfile 1.1 standard </li></ul><ul><li>Normally goes well together with Java-based services. </li></ul><ul><li>The standard DataContractSerializer has some limitations – may fallback to the legacy XmlSerializer </li></ul>
    27. 27. REST <ul><li>New in .NET 3.5 </li></ul><ul><ul><li>Additional features in SP1 </li></ul></ul><ul><li>HTTP Verbs – POX - JSON </li></ul><ul><li>System.ServiceModel.Web assembly </li></ul><ul><li>[WebInvoke] & [WebGet] </li></ul><ul><li>UriTemplate </li></ul><ul><li>WebOperationContext </li></ul><ul><li>WCF REST Starter Kit </li></ul><ul><ul><li>Will be incorporated in WCF 4.0 </li></ul></ul>
    28. 28. REST #2 <ul><li>System.ServiceModel.Syndication </li></ul><ul><ul><li>ATOM & RSS Feeds </li></ul></ul><ul><ul><li>Atom Publishing Protocol </li></ul></ul>
    29. 29. Questions? Next up: workshopping, pair-programming, geeking
    30. 30. Domo arigato!

    ×