WCF 4.0

3,380 views

Published on

What's new in WCF 4.0

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

No Downloads
Views
Total views
3,380
On SlideShare
0
From Embeds
0
Number of Embeds
36
Actions
Shares
0
Downloads
0
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

WCF 4.0

  1. 1. WCF 4.0 Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  2. 2. Agenda <ul><li>Simplified Configuration </li></ul><ul><li>Serialization Improvements </li></ul><ul><li>Discovery </li></ul><ul><li>Routing Services </li></ul><ul><li>Web Http Programming </li></ul>
  3. 3. Simplified Configuration
  4. 4. Simplified Configuration using ( var host = new ServiceHost ( typeof ( CalculatorService ) , new Uri ( &quot;http://localhost:1234/Service&quot; ) , new Uri ( &quot;net.tcp://localhost:4321/Service&quot; ) ) ) { host.Open(); Console .WriteLine( &quot;The service is ready.&quot; ); Console .WriteLine( &quot;Press <ENTER> to terminate service.&quot; ); Console .ReadKey(); host.Close(); } The service is open with default binding & behaviors
  5. 5. Explicitly & Default Endpoints using ( var host = new ServiceHost ( typeof ( CalculatorService ) ) ) { host.AddDefaultEndpoints(); host.Open(); } < services > < service name = &quot; CalculatorService &quot; > < host > < baseAddresses > < add baseAddress = &quot; http://localhost:1234/Service &quot; /> < add baseAddress = &quot; net.tcp://localhost:4321/Service &quot; /> </ baseAddresses > </ host > </ service > </ services > Config File Default Endpoint
  6. 6. Default Endpoint In WEB
  7. 7. Default Profile < bindings >    < basicHttpBinding >      < binding maxReceivedMessageSize = &quot; 9999999 &quot; >          < readerQuotas maxArrayLength = &quot; 9999999 &quot; />       </ binding >    </ basicHttpBinding >      </ bindings > < services > < service name = &quot; CalculatorService &quot; > < host > < baseAddresses > < add baseAddress = &quot; http://localhost:1234/Service &quot; /> < add baseAddress = &quot; net.tcp://localhost:4321/Service &quot; /> </ baseAddresses > </ host > </ service > </ services > No Name !!! Default profile for BasicHttpBinding
  8. 8. Protocol Mapping < protocolMapping >   < add scheme = &quot; http &quot; binding = &quot; basicHttpBinding &quot; />   < add scheme = &quot; net.tcp &quot; binding = &quot; netTcpBinding &quot; />   < add scheme = &quot; net.pipe &quot; binding = &quot; netNamedPipeBinding &quot; />   < add scheme = &quot; net.msmq &quot; binding = &quot; netMsmqBinding &quot; /> </ protocolMapping > < services > < service name = &quot; CalculatorService &quot; > < host > < baseAddresses > < add baseAddress = &quot; http://localhost:1234/Service &quot; /> < add baseAddress = &quot; net.tcp://localhost:4321/Service &quot; /> </ baseAddresses > </ host > </ service > </ services >
  9. 9. Protocol Mapping < protocolMapping >   < add scheme = &quot; http &quot; binding = &quot; wsHttpBinding &quot; bindingConfiguration = &quot; wsHttpBinding &quot; /> </ protocolMapping > < services > < service name = &quot; CalculatorService &quot; > < host > < baseAddresses > < add baseAddress = &quot; http://localhost:1234/Service &quot; /> < add baseAddress = &quot; net.tcp://localhost:4321/Service &quot; /> </ baseAddresses > </ host > </ service > </ services >
  10. 10. Simplified Configuration
  11. 11. File-less Activation (no .svc) <ul><li>It possible to define virtual service activation endpoints that map to your WCF service. </li></ul><ul><ul><li>ServiceHostingEnvironment is an application level config. </li></ul></ul><ul><ul><li>Supports activation over both http and non-http protocol. </li></ul></ul><ul><ul><li>Requires extensions in the relatativeAddress i.e. .svc. </li></ul></ul>< serviceHostingEnvironment >       < serviceActivations >         < add relativeAddress =&quot;~/E4D.svc&quot; service =&quot;E4d.Hello&quot;/>       </ serviceActivations >     </ serviceHostingEnvironment >
  12. 12. File-less Activation
  13. 13. Kind Attribute <ul><li>Set the type of the standard endpoint </li></ul><ul><li>Type of kinds: </li></ul><ul><ul><li>mexEndpoint </li></ul></ul><ul><ul><li>discoveryEndpoint </li></ul></ul><ul><ul><li>announcementEndpoint </li></ul></ul><ul><ul><li>udpDiscoveryEndpoint </li></ul></ul><ul><ul><li>udpAnnouncementEndpoint </li></ul></ul><ul><ul><li>webHttpEndpoint </li></ul></ul>< service name =&quot;HelloService&quot;>      ...      < endpoint binding =&quot;webHttpBinding&quot; contract = &quot;IService1&quot; kind = &quot;webHttpEndpoint&quot; /> < endpoint kind =&quot;mexEndpoint&quot; /> </ service > No need in bindingConfiguration endpointConfiguration
  14. 14. Standard Endpoint Setting < service name =&quot;HelloService&quot;>      < endpoint kind =&quot;udpDiscoveryEndpoint&quot; endpointConfiguration =&quot;MyEndpointSettings&quot; /> </ service > < standardEndpoints >    < udpDiscoveryEndpoint >        < standardEndpoint name =&quot;MyEndpointSettings&quot;              multicastAddress =&quot;soap.udp://239.255.255.252:3704&quot;              maxResponseDelay =&quot;00:00:02&quot;>             < transportSettings                   duplicateMessageHistoryLength =&quot;2048&quot;                   maxPendingMessageCount =&quot;5&quot;                   maxReceivedMessageSize =&quot;8192&quot;                   maxBufferPoolSize =&quot;262144&quot;/>            </ standardEndpoint >     </ udpDiscoveryEndpoint > </ standardEndpoints >
  15. 15. Custom Standard Endpoint <ul><li>Standard endpoint must be registered in the <endpointExtensions> section. </li></ul>< system.serviceModel > < service name =&quot;HelloService&quot;>      < endpoint kind = &quot; udpDiscoveryEndpoint &quot; /> </ service >   < extensions >       < endpointExtensions >         < add name = &quot; udpDiscoveryEndpoint &quot;   type = &quot; ... &quot; />       </ endpointExtensions >    </ extensions > </ system.serviceModel >
  16. 16. Standard Endpoint
  17. 17. Service Discovery
  18. 18. Why We Need Discovery?
  19. 19. Async WCF Services
  20. 20. WS – Discovery Service Opened Client Target Service Client Client Target Service Target Service Hello
  21. 21. WS – Discovery Client Look for Service Client Target Service Client Client Target Service Target Service Probe
  22. 22. WS – Discovery Service Return Match Message Client Target Service Client Client Target Service Target Service Probe Match
  23. 23. WS – Discovery Service Closed Client Target Service Client Client Target Service Target Service By
  24. 24. Multicast, Broadcast & Unicast Multicast Broadcast Unicast
  25. 25. WCF With Discovery Server Side Normal endpoints with EndpointDiscoveryBehavior Discovery endpoint with ServiceDiscoveryBehavior Target Service
  26. 26. WCF With Discovery Server Side < system.serviceModel > < service name =&quot;HelloService&quot;> < endpoint address = &quot;net.tcp://localhost/&quot; binding = &quot;netTcpBinding&quot; contract = &quot;ICalculator&quot; />      < endpoint kind = &quot; udpDiscoveryEndpoint &quot; /> </ service >   </ behaviors > < serviceBehaviors > < behavior > < serviceDiscovery /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel >
  27. 27. <ul><li>Create discovery client </li></ul><ul><li>Define the search Criteria for the client </li></ul><ul><li>Choose endpoint, from the search result. </li></ul>WCF With Discovery Client Side ( Two steps ) var client = new DiscoveryClient ( new UdpDiscoveryEndpoint () ); var criteria = new FindCriteria ( typeof ( ICalculatorService ) ); FindResponse findResponse = client.Find( criteria ); var address = findResponse.Endpoints[0].Address Binding? ListenUri? Scope?
  28. 28. Basic Discovery
  29. 29. How to Know About The Binding? <ul><li>Add MEX endpoint </li></ul><ul><li>Discovery the MEX endpoint </li></ul><ul><li>Use the MetadataResolver to resolve the binding. </li></ul>ServiceEndpointCollection endpoints = MetadataResolver .Resolve( typeof ( IHelloService ), mexAddress );
  30. 30. Dynamic Endpoint <ul><li>DiscoveryClientBindingElement is added to the binding you specify in the DynamicEndpoint constructor, if not already present. </li></ul>var dynamicEndpoint = new DynamicEndpoint ( ContractDescription .GetContract( typeof ( ICalc ) ), new NetTcpBinding () ); dynamicEndpoint.FindCriteria.Scopes.Add(...); var client = new CalcClient ( dynamicEndpoint );
  31. 31. Dynamic Endpoint
  32. 32. Discovery Announcements < service name =&quot;HelloService&quot;>      ...      < endpoint name =&quot;udpDiscovery“ kind =&quot;udpDiscoveryEndpoint&quot;/> </ service > ... < serviceBehaviors >    < behavior >       < serviceDiscovery >          < announcementEndpoints >             < endpoint name = &quot; udpAnnouncement &quot; kind = &quot; udpAnnouncementEndpoint &quot; />             </ announcementEndpoints >          </ serviceDiscovery >    </ behavior > </ serviceBehaviors >  Server Side:
  33. 33. Discovery Announcements var announcementService = new AnnouncementService (); announcementService.OnlineAnnouncementReceived += OnOnlineEvent; announcementService.OfflineAnnouncementReceived += OnOfflineEvent; using ( var host = new ServiceHost ( announcementService ) ) { host.AddServiceEndpoint( new UdpAnnouncementEndpoint () ); host.Open(); ... } Consumers Side:
  34. 34. Discovery Announcements
  35. 35. Announcement Data
  36. 36. Discovery Proxy <ul><li>Managed discovery allows you to locate services no matter where they are, as long as they are registered with a discovery proxy. </li></ul>Client Target Service Client Client Target Service Target Service Discovery Proxy Hello
  37. 37. Discovery Proxy <ul><li>Managed discovery allows you to locate services no matter where they are, as long as they are registered with a discovery proxy. </li></ul>Client Target Service Client Client Target Service Target Service Discovery Proxy Probe
  38. 38. Discovery Proxy <ul><li>Managed discovery allows you to locate services no matter where they are, as long as they are registered with a discovery proxy. </li></ul>Client Target Service Client Client Target Service Target Service Discovery Proxy Probe Match
  39. 39. Discovery Proxy <ul><li>Managed discovery allows you to locate services no matter where they are, as long as they are registered with a discovery proxy. </li></ul>Client Target Service Client Client Target Service Target Service Discovery Proxy By
  40. 40. Discovery Proxy
  41. 41. Service Discovery <ul><li>When service is open he send to MDC a message ( Endpoint info). </li></ul><ul><li>When Service look for a service he ask the MDC. </li></ul>Unified Cache View
  42. 42. Solution Advantage <ul><li>No need to do multicast communication </li></ul><ul><li>The server endpoints info is available to all the services immediate after the server is open, ( no need to do communication ). </li></ul><ul><li>When the service is unavailable (down time) the endpoint info will still be available </li></ul>
  43. 43. Routing Services
  44. 44. Why a Router Service? <ul><li>Load balancing </li></ul><ul><li>Priority routing to application pools or servers according to message content. </li></ul><ul><li>Partitioning services so that messages targeting operations can be handled by different application pools or servers. </li></ul>
  45. 45. Router Service Architecture Server C Server C Server B Client Router Service Filter Tables Filters Service A Service B Service C
  46. 46. Router Endpoints & Contracts < service name = &quot; System.ServiceModel.Routing.RoutingService &quot; >    < endpoint name = &quot;reqReplyEndpoint &quot; address = &quot;&quot; binding = &quot; basicHttpBinding &quot; contract = &quot; System.ServiceModel.Routing.IRequestReplyRouter &quot; /> </ service >
  47. 47. Configuring Filters <ul><li>The RouterService evaluates filters to determine how to forward the message. </li></ul>< serviceBehaviors > < behavior > < routing filterTableName = &quot; MyRoutingTable &quot; /> </ behavior > </ serviceBehaviors > < routing > < filters > < filter name =&quot;MatchAllFilter1&quot; filterType =&quot;MatchAll&quot; /> </ filters > < filterTables > < filterTable name =&quot;MyroutingTable&quot;> < add filterName =&quot;MatchAllFilter1&quot; endpointName =&quot;CalculatorService&quot; /> </ filterTable > </ filterTables > </ routing > < client > < endpoint name =&quot;CalculatorService&quot; address =&quot;http://...&quot; binding =&quot;basicHtttpBinding&quot; contract =&quot;*&quot; > </ client >
  48. 48. Message Filter Compares the &quot;To&quot; & Headers address to the endpoint address Compares the incoming &quot;Action&quot; value against the actions on the contract
  49. 49. Message Filter Compares the &quot;To&quot; & Headers address to the endpoint address Compares the incoming &quot;Action&quot; value against the actions on the contract < filters > < filter name =&quot;addFilter&quot; filterType =&quot;XPath&quot;   filterData =&quot;...&quot; > < filter name =&quot;addFilter&quot; filterType =&quot;Action&quot;  filterData =&quot;...&quot; > </ filters >
  50. 50. Filtering architecture
  51. 51. Binding Configuration <ul><li>The router typically does not process protocol headers passed by the client. </li></ul><ul><ul><li>Security </li></ul></ul><ul><ul><li>Reliable sessions </li></ul></ul><ul><ul><li>Transactions </li></ul></ul><ul><ul><li>Other related protocols. </li></ul></ul>
  52. 52. Routing Services
  53. 53. Error Handing <ul><li>You can define different lists of alternate endpoints that will be used if communicating with the initial target endpoint results in an error. </li></ul>< routing > ... < filterTable name =&quot;MyroutingTable&quot;> < add filterName =&quot;f1&quot; endpointName =&quot;t1&quot; backupList =&quot;altEndpoints&quot; /> </ filterTable > ... < backupLists >         < backupList name =&quot;altEndpoints&quot;>             < add endpointName =&quot;CalculatorService2&quot;/>                   </ backupList >       </ backupLists > </ routing >
  54. 54. Multicast Routing Behavior <ul><li>Supports automatically routing a particular incoming message to multiple destinations in a “multicast” fashion. </li></ul>< routing > < filters > < filter name =&quot;Filter1&quot; filterType =&quot;MatchAll&quot; /> </ filters > < filterTables > < filterTable name =&quot;MyroutingTable&quot;> < add filterName =&quot;Filter1&quot; endpointName =&quot;Target1&quot; /> < add filterName =&quot;Filter1&quot; endpointName =&quot;Target2&quot; /> < add filterName =&quot;Filter1&quot; endpointName =&quot;Target3&quot; /> </ filterTable > </ filterTables > </ routing > “ Multicast”
  55. 55. Higher Default Throttling Settings <ul><li>MaxConcurrent Sessions 10  100 * CPU </li></ul><ul><li>MaxConcurrent Calls 16  16 * CPU </li></ul><ul><li>MaxConcurrent Instances 26  116 * CPU </li></ul>
  56. 56. Serialization Improvements
  57. 57. Serialization Improvements <ul><li>public UserInfo EchoUser ( int userId ); </li></ul>Encoding Deserialization Serialization Encoding Binding Transport
  58. 58. KnownType Attribute <ul><li>Make sure a type is included in the serialization stream </li></ul>[ ServieContract ] public intrface IMedia { [ OperationContract ] Media GetMedia( Media media ); } [ DataContract ] public class Media { ... } [ DataContract ] public class DVD : Media { ... } [ DataContract ] public class CD : Media { ... } < Envelope > < Header > < Action >E4D/IMedia/GetMedia</ Action > </ Header > < Body > < GetMedia > < media > < Name>Madona </ Name > < Type>CDWR </ Type > </ media > </ GetMedia > </ Body > </ Envelope >
  59. 59. [ ServieContract ] public intrface IMedia { [ OperationContract ] Media GetMedia( Media media ); } [ DataContract ] public class Media { ... } [ DataContract ] public class DVD : Media { ... } [ DataContract ] public class CD : Media { ... } < Envelope > < Header > < Action >E4D/IMedia/GetMedia</ Action > </ Header > < Body > < GetMedia > < media type =&quot;CD&quot;> < Name>Madona </ Name > < Type>CDWR </ Type > </ media > </ GetMedia > </ Body > </ Envelope >
  60. 60. KnownType Attribute <ul><li>Make sure a type is included in the serialization stream </li></ul>[ ServieContract ] public intrface IMedia { [ OperationContract ] Media GetMedia( Media media ); } [ DataContract ] [ KnownType ( typeof ( DVD ) )] [ KnownType ( typeof ( CD ) ) ] public class Media { ... } [ DataContract ] public class DVD : Media { ... } [ DataContract ] public class CD : Media { ... } < Envelope > < Header > < Action >E4D/IMedia/GetMedia</ Action > </ Header > < Body > < GetMedia > < media type =&quot;CD&quot;> < Name>Madona </ Name > < Type>CDWR </ Type > </ media > </ GetMedia > </ Body > </ Envelope > Known Type
  61. 61. The Serialization in WCF 3.5
  62. 62. The Serialization in WCF 4.0
  63. 63. Data Contract Resolver <ul><li>New types can be added at runtime using the xsi:type schema declarations. </li></ul><ul><ul><li>The ResolveType method is invoked only during serialization and is used to map any type to a new xsi:type name. </li></ul></ul><ul><ul><li>The ResolveName method is used only during deserialization and allows you to map any xsi:type information to a type. </li></ul></ul>
  64. 64. Data Contract Resolver
  65. 65. Web Http Programming
  66. 66. Enhanced REST Support <ul><li>Caching support in IIS/WAS </li></ul><ul><li>Dynamic content types </li></ul><ul><li>Improved exception support </li></ul><ul><li>JSONP support for AJAX Services </li></ul><ul><li>Automatically generated help page </li></ul><ul><li>ASP.NET Routing integration </li></ul><ul><li>Helper methods for creating content </li></ul>
  67. 67. Help Page < behaviors > < endpointBehaviors > < behavior name = &quot; HelpBehavior &quot; > < webHttp enableHelp = &quot; true &quot; /> </ behavior > </ endpointBehaviors > </ behaviors >
  68. 68. Caching <ul><li>< system.web > </li></ul><ul><ul><li>< caching > </li></ul></ul><ul><ul><ul><li>< outputCacheSettings > </li></ul></ul></ul><ul><ul><ul><ul><li>< outputCacheProfiles > </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>< add name = &quot; CacheFor60Seconds &quot; duration = &quot; 60 &quot; varyByParam = &quot; format &quot; /> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li></ outputCacheProfiles > </li></ul></ul></ul></ul><ul><ul><ul><li></ outputCacheSettings > </li></ul></ul></ul><ul><ul><li></ caching > </li></ul></ul><ul><li></ system.web > </li></ul>[ AspNetCacheProfile (&quot; CacheFor60Seconds &quot;)] [ WebGet ] [ OperationContract ] public Counter GetItem(){...}
  69. 69. Help Page
  70. 70. JSONP For AJAX Services <ul><li>Convention used to invoke cross-domain scripts by generating script tags in the current document. </li></ul><ul><li>Result is returned in a specified callback function <script src=&quot;http://...&quot;> can evaluate scripts from any domain. </li></ul>< serviceHostingEnvironment aspNetCompatibilityEnabled =&quot;true&quot;/> < standardEndpoints > < webScriptEndpoint > < standardEndpoint crossDomainScriptAccessEnabled =&quot;true&quot;/> </ webScriptEndpoint > </ standardEndpoints >
  71. 71. JSONP
  72. 72. Dynamic Content <ul><ul><li>WCF can automatically create response format based on Content-Type and Accepts headers. </li></ul></ul><ul><li>Helper methods for creating content </li></ul><ul><ul><li>Available through WebOperationContext </li></ul></ul><ul><ul><ul><li>public Message CreateAtom10Response(...); </li></ul></ul></ul><ul><ul><ul><li>public Message CreateJsonResponse<T>(...); </li></ul></ul></ul><ul><ul><ul><li>public Message CreateStreamResponse(...); </li></ul></ul></ul><ul><ul><ul><li>public Message CreateTextResponse(...); </li></ul></ul></ul><ul><ul><ul><li>public Message CreateXmlResponse(...); </li></ul></ul></ul><ul><ul><ul><li>public Message CreateXmlResponse<T>(...); </li></ul></ul></ul>
  73. 73. Dynamic Content
  74. 74. Resources <ul><li>http://blogs.microsoft.co.il/blogs/vardi/ </li></ul><ul><li>A Developer’s Introduction to WCF 4 Beta 1 </li></ul><ul><li>Service Configuration Improvements in .NET 4 </li></ul><ul><li>What’s New in WCF 4.0? </li></ul><ul><li>Jesus Rodriguez's WebLog </li></ul>

×