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