1 / 33

Top 10 Tricks & Tips for WCF

Top 10 Tricks & Tips for WCF. Barry Dorrans http://idunno.org. About the Speaker (the ego slide). MVP – Visual Tools, Security barryd@idunno.org. 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

doli
Télécharger la présentation

Top 10 Tricks & Tips for WCF

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Top 10 Tricks & Tips for WCF Barry Dorrans http://idunno.org

  2. About the Speaker(the ego slide) MVP – Visual Tools, Security barryd@idunno.org

  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. What is WCF? Indigo Replaces Web Services, WSE & Remoting Interoperability

  5. The ABCs of WCF Address Binding Contract

  6. Client Server Messaging Layer Endpoint Service Model Address Binding Channel Behaviour Behaviour Contract Binding Binding Channel Behaviour Behaviour Factory Listener Channel Address Address Channel *

  7. Address scheme:// <machineName>[:port]/path http://localhost:8080/Account/ net.tcp://localhost:8080/Account

  8. Binding How you communicate with a service A service may have multiple bindings Defines the shape; security etc.

  9. Contract The external interface Contracts for service, data and message

  10. The ABCs Address = WHERE Binding = HOW Contract = WHAT

  11. Service Contracts [ServiceContract()] public interface IMyService { [OperationContract] string MyOperation1(string myValue); [OperationContract] string MyOperation2(MyDataContract value); }

  12. Data Contracts [DataContract] public class MyDataContract { string _stuff; [DataMember] public string Stuff { get { return _stuff; } set { stuff = value; } } }

  13. 1 – The test harnesses

  14. 2 – Don’t use Using

  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. 3 - It’s all your fault

  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("value was negative")); In Client Code try { .... } catch (FaultException<NegativeNumberFault>) { .... }

  18. 4 – Migrating an ASMX

  19. Migrating an ASMX Change the protocol binding to basicHttpBinding Match your namespaces [ServiceContract(Namespace="http:/tempuri.org/")] Match your actions[OperationContract(Action = "http://tempuri.org/HelloWorld")] Match your message names (if you use them)[OperationContract(Action = "http://tempuri.org/HelloWorld", Name = "MyMessageName")] Change the serializer to use XmlSerializer[XmlSerializerAttribute] public class MyService

  20. Migrating an ASMX Make WCF answer ASMX<buildProviders> <remove extension=".asmx"/> <add extension=".asmx"type="System.ServiceModel.Activation.ServiceBuildProvider, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </buildProviders>

  21. 5 – Message Inspectors

  22. Message Inspectors Can be client or server side Messages can be modified – but you must copy the message then replace it. Apply the custom behaviour via config or via an attribute.

  23. 6 – Custom Authentication

  24. Custom Authentication Reference System.IdentityModel andSystem.IdentityModel.Selectors Implement a UsernamePasswordValidator Plug it in via config

  25. 7 – Port Sharing

  26. Port Sharing Vista / Windows 2008 – netsh http add urlaclurl=http://+:port/url/ user=\Everyone XP / Windows 2003 (Support Tools)–httpcfg set urlacl /uhttp://*:80/url//a D:(A;;GX;;;NS) XP does not port share with IIS.

  27. 8 – Callbacks

  28. Callbacks Publish / Subscribe model works best Declare a callback interfaceinterface IMessageCallback { [OperationContract(IsOneWay = true)] void OnMessageAdded(string message, DateTime timestamp); } Add the callback to the service contract.[ServiceContract(CallbackContract = typeof(IMessageCallback))] Implement callback interface on client.

  29. 9 - Logging

  30. 10 – RESTful services

  31. RESTful services Reference System.ServiceModel.Web Use WebHttpBinding and WebHttpBehavior Decorate contract with WebGet or WebInvoke[OperationContract][WebInvoke(Method = "POST", UriTemplate = "")]void AddMessage(Message message);[OperationContract][WebInvoke(Method = "DELETE", UriTemplate = "{id}")]void DeleteMessage(string id);[OperationContract][WebGet(UriTemplate = "{id}")]Message GetMessage(string id);[OperationContract][WebGet(UriTemplate = "")]List<Message> GetMessages();

  32. RESTful services Avoid the hassle and use the REST starter kithttp://msdn.microsoft.com/en-us/netframework/wcf/rest

  33. Questions?

More Related