RoutingSection.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.ServiceModel;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel.Configuration;
  9. using System.ServiceModel.Description;
  10. using System.ServiceModel.Dispatcher;
  11. namespace System.ServiceModel.Routing.Configuration
  12. {
  13. static class RoutingConfigurationExtension
  14. {
  15. public static ServiceEndpoint CreateEndpoint (this ChannelEndpointElement el)
  16. {
  17. // depends on System.ServiceModel internals (by InternalVisibleTo).
  18. // FIXME: is IRequestReplyRouter okay for every case?
  19. return new ServiceEndpoint (ContractDescription.GetContract (typeof (IRequestReplyRouter)), ConfigUtil.CreateBinding (el.Binding, el.BindingConfiguration), new EndpointAddress (el.Address));
  20. }
  21. public static MessageFilter CreateFilter (this FilterElement el, RoutingSection sec)
  22. {
  23. switch (el.FilterType) {
  24. case FilterType.Action:
  25. return new ActionMessageFilter (el.FilterData);
  26. case FilterType.EndpointAddress:
  27. return new EndpointAddressMessageFilter (new EndpointAddress (el.FilterData), false);
  28. case FilterType.PrefixEndpointAddress:
  29. return new PrefixEndpointAddressMessageFilter (new EndpointAddress (el.FilterData), false);
  30. case FilterType.And:
  31. var fe1 = (FilterElement) sec.Filters [el.Filter1];
  32. var fe2 = (FilterElement) sec.Filters [el.Filter2];
  33. return new StrictAndMessageFilter (fe1.CreateFilter (sec), fe2.CreateFilter (sec));
  34. case FilterType.Custom:
  35. return (MessageFilter) Activator.CreateInstance (Type.GetType (el.CustomType));
  36. case FilterType.EndpointName:
  37. return new EndpointNameMessageFilter (el.FilterData);
  38. case FilterType.MatchAll:
  39. return new MatchAllMessageFilter ();
  40. case FilterType.XPath:
  41. return new XPathMessageFilter (el.FilterData);
  42. default:
  43. throw new ArgumentOutOfRangeException ("FilterType");
  44. }
  45. }
  46. }
  47. public class RoutingSection : ConfigurationSection
  48. {
  49. static ServiceEndpoint CreateServiceEndpoint (string name)
  50. {
  51. // FIXME: might be service endpoints.
  52. var endpoints = ConfigUtil.ClientSection.Endpoints;
  53. foreach (ChannelEndpointElement ep in endpoints)
  54. if (ep.Name == name)
  55. return ep.CreateEndpoint ();
  56. throw new KeyNotFoundException (String.Format ("client endpoint '{0}' not found", name));
  57. }
  58. [MonoTODO]
  59. public static MessageFilterTable<IEnumerable<ServiceEndpoint>> CreateFilterTable (string name)
  60. {
  61. var sec = (RoutingSection) ConfigurationManager.GetSection ("system.serviceModel/routing");
  62. var table = new MessageFilterTable<IEnumerable<ServiceEndpoint>> ();
  63. var ftec = (FilterTableEntryCollection) sec.FilterTables [name];
  64. foreach (FilterTableEntryElement fte in ftec) {
  65. var filterElement = (FilterElement) sec.Filters [fte.FilterName];
  66. MessageFilter filter = filterElement.CreateFilter (sec);
  67. table.Add (filter, new List<ServiceEndpoint> (), fte.Priority);
  68. var list = (List<ServiceEndpoint>) table [filter];
  69. list.Add (CreateServiceEndpoint (fte.EndpointName));
  70. var bec = (BackupEndpointCollection) sec.BackupLists [fte.BackupList];
  71. if (bec != null)
  72. foreach (BackupEndpointElement bee in bec)
  73. list.Add (CreateServiceEndpoint (bee.EndpointName));
  74. }
  75. return table;
  76. }
  77. public RoutingSection ()
  78. {
  79. //BackupLists = new BackupListCollection ();
  80. //Filters = new FilterElementCollection ();
  81. //FilterTables = new FilterTableCollection ();
  82. //NamespaceTable = new NamespaceElementCollection ();
  83. }
  84. [ConfigurationProperty ("backupLists", Options = ConfigurationPropertyOptions.None)]
  85. public BackupListCollection BackupLists {
  86. get { return (BackupListCollection) base ["backupLists"]; }
  87. private set { base ["backupLists"] = value; }
  88. }
  89. [ConfigurationProperty ("filters", Options = ConfigurationPropertyOptions.None)]
  90. public FilterElementCollection Filters {
  91. get { return (FilterElementCollection) base ["filters"]; }
  92. private set { base ["filters"] = value; }
  93. }
  94. [ConfigurationProperty ("filterTables", Options = ConfigurationPropertyOptions.None)]
  95. public FilterTableCollection FilterTables {
  96. get { return (FilterTableCollection) base ["filterTables"]; }
  97. private set { base ["filterTables"] = value; }
  98. }
  99. [ConfigurationProperty ("namespaceTable", Options = ConfigurationPropertyOptions.None)]
  100. public NamespaceElementCollection NamespaceTable {
  101. get { return (NamespaceElementCollection) base ["namespaceTable"]; }
  102. private set { base ["namespaceTable"] = value; }
  103. }
  104. }
  105. }