10 Tricks and Tips for WCF

14,002 views

Published on

10 Tricks and Tips for WCF

(Presented at DDD, Dec 08)

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

No Downloads
Views
Total views
14,002
On SlideShare
0
From Embeds
0
Number of Embeds
219
Actions
Shares
0
Downloads
482
Comments
0
Likes
15
Embeds 0
No embeds

No notes for slide

10 Tricks and Tips for WCF

  1. 1. Top 10 Tricks & Tips for WCF Barry Dorrans http://idunno.org
  2. 2. About the Speaker (the ego slide) MVP – Visual Tools, Security barryd@idunno.org
  3. 3. Agenda 1) The test harnesses 2) Don’t use Using 3) It’s all your fault 4) Migrating an ASMX 5) Message Inspectors 6) Custom Authentication 7) Port sharing 8) Callbacks 9) Logging 10) RESTful services
  4. 4. What is WCF? Indigo Replaces Web Services, WSE & Remoting Interoperability
  5. 5. The ABCs of WCF Address Binding Contract
  6. 6. Service Model Client Server Messaging Layer Endpoint Address Binding Channel Behaviour Behaviour Contract Binding Binding Channel Behaviour Behaviour Factory Listener Channel Address Address Channel *
  7. 7. Address scheme:// <machineName>[:port]/path http://localhost:8080/Account/ net.tcp://localhost:8080/Account
  8. 8. Binding How you communicate with a service A service may have multiple bindings Defines the shape; security etc.
  9. 9. Contract The external interface Contracts for service, data and message
  10. 10. The ABCs Address = WHERE Binding = HOW Contract = WHAT
  11. 11. Service Contracts [ServiceContract()] public interface IMyService { [OperationContract] string MyOperation1(string myValue); [OperationContract] string MyOperation2(MyDataContract value); }
  12. 12. Data Contracts [DataContract] public class MyDataContract { string _stuff; [DataMember] public string Stuff { get { return _stuff; } set { stuff = value; } } }
  13. 13. 1 – The test harnesses
  14. 14. 2 – Don’t use Using
  15. 15. Disposing the right way Service1Client proxy = null; try { proxy = new Service1Client(....); } catch (...) finally { if (proxy != null) { try { if (proxy.State == CommunicationState.Opened) proxy.Close(); else if (proxy.State == CommunicationState.Faulted) proxy.Abort(); } catch (CommunicationException) { proxy.Abort(); } catch (TimeoutException) { proxy.Abort(); } } }
  16. 16. 3 - It’s all your fault
  17. 17. Faults the right way In Contract Code [ServiceContract] public interface IService1 { [OperationContract] [FaultContract(typeof(NegativeNumberFault))] string GetDataWithFault(int value); } [DataContract] public class NegativeNumberFault { } In Service Code throw new FaultException<NegativeNumberFault>( new NegativeNumberFault(), new FaultReason(quot;value was negativequot;)); In Client Code try { .... } catch (FaultException<NegativeNumberFault>) { .... }
  18. 18. 4 – Migrating an ASMX
  19. 19. Migrating an ASMX 1. Change the protocol binding to basicHttpBinding 2. Match your namespaces [ServiceContract(Namespace=quot;http:/tempuri.org/quot;)] 3. Match your actions [OperationContract(Action = quot;http://tempuri.org/HelloWorldquot;)] 4. Match your message names (if you use them) [OperationContract(Action = quot;http://tempuri.org/HelloWorldquot;, Name = quot;MyMessageNamequot;)] 5. Change the serializer to use XmlSerializer [XmlSerializerAttribute] public class MyService
  20. 20. Migrating an ASMX 6. Make WCF answer ASMX <buildProviders> <remove extension=quot;.asmxquot;/> <add extension=quot;.asmxquot; type=quot;System.ServiceModel.Activation.ServiceBuildProvider, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089quot;/> </buildProviders>
  21. 21. 5 – Message Inspectors
  22. 22. Message Inspectors 1. Can be client or server side 2. Messages can be modified – but you must copy the message then replace it. 3. Apply the custom behaviour via config or via an attribute.
  23. 23. 6 – Custom Authentication
  24. 24. Custom Authentication 1. Reference System.IdentityModel and System.IdentityModel.Selectors 2. Implement a UsernamePasswordValidator 3. Plug it in via config
  25. 25. 7 – Port Sharing
  26. 26. Port Sharing 1. Vista / Windows 2008 – netsh http add urlacl url=http://+:port/url/ user=Everyone 2. XP / Windows 2003 (Support Tools)– httpcfg set urlacl /u http://*:80/url/ /a D:(A;;GX;;;NS) 3. XP does not port share with IIS.
  27. 27. 8 – Callbacks
  28. 28. Callbacks 1. Publish / Subscribe model works best 2. Declare a callback interface interface IMessageCallback { [OperationContract(IsOneWay = true)] void OnMessageAdded(string message, DateTime timestamp); } 3. Add the callback to the service contract. [ServiceContract(CallbackContract = typeof(IMessageCallback))] 4. Implement callback interface on client.
  29. 29. 9 - Logging
  30. 30. 10 – RESTful services
  31. 31. RESTful services 1. Reference System.ServiceModel.Web 2. Use WebHttpBinding and WebHttpBehavior 3. Decorate contract with WebGet or WebInvoke [OperationContract] [WebInvoke(Method = quot;POSTquot;, UriTemplate = quot;quot;)] void AddMessage(Message message); [OperationContract] [WebInvoke(Method = quot;DELETEquot;, UriTemplate = quot;{id}quot;)] void DeleteMessage(string id); [OperationContract] [WebGet(UriTemplate = quot;{id}quot;)] Message GetMessage(string id); [OperationContract] [WebGet(UriTemplate = quot;quot;)] List<Message> GetMessages();
  32. 32. RESTful services 4. Avoid the hassle and use the REST starter kit http://msdn.microsoft.com/en-us/netframework/wcf/rest
  33. 33. Questions?

×